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 Descriptionalexandra has a little brother. He is new to programming. One day he's solving the following problem:given an positive integer n, and 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 I Nteger M, such that n/m is prime. If such M doesn ' t exist, output 0. Help him!
Inputthere is multiple test cases (no more than 1,000). Each case contains only one positive integer N.N≤1 , A.. Number of cases with N>1,than, are No more than 100.
Outputfor each case, output the requested M, or output 0 if no solution exists.
Sample Input3456
Sample Output1212
The puzzle: Let the smallest positive integer m make n/m a prime number;
Water violence, but complete violence will be super, think of binary find, first find this smallest positive integer, if not to find prime; all find sqrt (N) end, so save a lot of time;
Code:
#include <iostream>#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>using namespacestd;BOOLIs_prim (intx) { if(x = =1)return false; for(inti =2; I <= sqrt (x); i++){ if(x% i = =0)return false; } return true;}intMain () {intN; while(~SCANF ("%d",&N)) { if(N = =0|| N = =1) {puts ("0");Continue; } inti; intAns =0; for(i =1; I <= sqrt (N); i++){ if(N% i = =0&& Is_prim (n/i)) {ans=i; Break; } } if(ans) {printf ("%d\n", ans);Continue; } for(intj = sqrt (N); J >1; j--){ if(N% J = =0&&Is_prim (j)) {ans= N/J; Break; }} printf ("%d\n", ans); } return 0;}
Alexandra and Prime Numbers (thinking)