Alexandra has a little brother. He is new to programming. One day he is solving the following problem: Given an positive integer N, judge whether N is prime. The problem above is quite easy, so Alexandra gave him a new task: Given a positive integer N, find the minimal positive integer M, such that N/M is prime. If such M doesn‘t exist, output 0. Help him!
Input
There are multiple test cases (no more than 1,000). Each case contains only one positive integer N. $N \leq 1,000,000,000$. Number of cases with $N > 1,000,000$ is no more than 100.
Output
For each case, output the requested M, or output 0 if no solution exists.
Sample Input
3 4 5 6
Sample Output
1 2 1 2
这个题呢,我很纠结,因为理解错题了,首题目让找一个 最小的数使n/m,必须能被整除,而且使n/m的值是个素数。
当时我呢,没有找必须被整除的数,一直,时间超限,而且答案是错误的。
结果我问了我的师傅,才知道那儿错了。首先任何一个大于1的数x,都能写成x=a1^(n1)×a2^(n2)×a3^(n3)×……×an^(nn);
这里a1,a2,a3,……,an,都是奇数。
比如当n=450;那么n=2×3×3×5×5;只能保留到最后一个,最大的,素数,
即m=n/5;
所以,
#include<stdio.h>
#include<math.h>
#include<algorithm>
using namespace std;
int fun(int n)
{
int i,j=0,k;
k=n;
for(i=2;i<=sqrt(n);i++) //不能用i<=n因为会超时
{
while(k%i==0)
{
k=k/i;
j=max(j,i);
}
}
j=max(j,k); // 比如当n=10时
if(n==1)printf("0\n");
else
{
printf("%d\n",j==0?1:n/j);
}
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
fun(n);
}
return 0;
}