AMR11E - Distinct Primes (分解质因数)

AMR11E - Distinct Primes 是一道数学问题,涉及数论中的质数和幸运数。幸运数是指至少有三个不同质因数的正整数,例如30和42。题目要求根据输入的测试用例数量T和序号n,找到第n个幸运数。题目限制T和n的范围,并提供了样例输入和输出。解决方法可以通过分解质因数来实现,确保质因数不同,并从小到大排列找到第n个幸运数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

点击打开链接

AMR11E - Distinct Primes

#math #number-theory

 

Arithmancy is Draco Malfoy's favorite subject, but what spoils it for him is that Hermione Granger is in his class, and she is better than him at it. Prime numbers are of mystical importance in Arithmancy, and Lucky Numbers even more so. Lucky Numbers are those positive integers that have at least three distinct prime factors; 30 and 42 are the first two. Malfoy's teacher has given them a positive integer n, and has asked them to find the n-th lucky number. Malfoy would like to beat Hermione at this exercise, so although he is an evil git, please help him, just this once. After all, the know-it-all Hermione does need a lesson.

Input

The first line contains the number of test cases T. Each of the next T lines contains one integer n.

Output

Output T lines, containing the corresponding lucky number for that test case.

Constraints

1 <= T <= 20
1 <= n <= 1000

Example

Sample Input:
2
1
2

Sample Output:
30
42

题意:让我们找一个数,这个数的因数有三个以上是不同的质数,并找出从小到大1~1000个这样的数,并且输入一个n序号就能对应输出第n个(从小到大)的这个数。

分析:怎么做呢?我们可以用分解质因数来做,并且分解出来的质因数不同。由于一个数的质数一定比它小,而且循环的时候,我们是先找小的,我们可以利用一直除的方法,就能让循环时一定时一个质数。

话不多说,看代码(前面都是构造一个这样的序列,后面就是直接输入输出就行了):

#include<iostream>
using namespace std;
int ans[10005]={0,30,42};
int K=3;
void judge(int k)
{
	int T=k;
	int t=0;
	for(int i=2;i<=k;i++)
	{
		if(k%i==0) t++;
		while(k%i==0)//分解质因数的关键步骤 
			k/=i;
		if(t>=3)
		{
			ans[K]=T;
			K++;
			break;
		}
	}
}
int main()
{
	for(int i=43;i<=10000;i++)//我们已经知道42是第二个了,所以我们直接从43开始看。
	{
		judge(i);
	}
	int T;
	cin>>T;
	while(T--)
	{
		int n;
		cin>>n;
		cout<<ans[n]<<endl;
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值