B - Alexandra and Prime Numbers

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;

}

时间: 2024-10-31 12:25:06

B - Alexandra and Prime Numbers的相关文章

Alexandra and Prime Numbers(思维)

Alexandra and Prime Numbers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1658    Accepted Submission(s): 565 Problem Description Alexandra has a little brother. He is new to programming. One

hdu 5108 Alexandra and Prime Numbers (水题,暴力)

Problem Description 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

BestCoder18 1001.Alexandra and Prime Numbers(hdu 5108) 解题报告

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5108 题目意思:给出一个数正整数 N,N <= 1e9,现在需要找出一个最少的正整数 M,使得 N/M 是素数.如果找不到就输出0. 一开始有想过将所有 <= 1e9 的素数求出来的,不过绝对超时就放弃了:然后就开始从题目中挖掘简便的处理方法.受到求素数的方法启发,枚举的因子 i 如果在 i * i <= N 之内都没有找到符合条件的素数,那么那些 > N 的因子就更不可能了.于是时间

hdu 5108 Alexandra and Prime Numbers

http://acm.hdu.edu.cn/showproblem.php?pid=5108 找出最大质因子就可以. 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #define ll long long 5 #define N 100000 6 using namespace std; 7 8 ll n; 9 10 11 int main() 12 { 13 while(scanf(

hdu5108Alexandra and Prime Numbers(素数的性质)

题目链接: huangjing 思路:每一个数都可以表示成若干个素数的乘积,那么可以对N从2一直枚举到sqrt(N),然后对每个数都能除到不能取余为止,那么后面的合数就不会除了,所以最后得到的数就是最大的质因子,然后直接N/最大的质因子,还有就是N=1的时候没有存在的数  . 题目: Alexandra and Prime Numbers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth

HDU-5108-Alexandra and Prime Numbers (BestCoder Round #19)

Alexandra and Prime Numbers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 319    Accepted Submission(s): 120 Problem Description Alexandra has a little brother. He is new to programming. One

hdu-5108-Alexandra and Prime Numbers(求最大质因数) (BestCoder Round #19)

Alexandra and Prime Numbers                                        Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Description Alexandra has a little brother. He is new to programming. One day he is solving

Sum of Consecutive Prime Numbers POJ - 2739

Some positive integers can be represented by a sum of one or more consecutive prime numbers. How many such representations does a given positive integer have? For example, the integer 53 has two representations 5 + 7 + 11 + 13 + 17 and 53. The intege

POJ 2739 Sum of Consecutive Prime Numbers(水题)

Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20560   Accepted: 11243 Description Some positive integers can be represented by a sum of one or more consecutive prime numbers. How many such representatio