Alexandra and Prime Numbers
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2291 Accepted Submission(s): 755
Total Submission(s): 2291 Accepted Submission(s): 755
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 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!
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≤1,000,000,000 .
Number of cases with N>1,000,000 is no more than 100.
N≤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(<=10Y),然后让找到一个最小的m使得n/m是一个素数.
思路:
先用sqrt(n)的时间把所有的因子都求出来,然后在排序,枚举,就行了
给一个n(<=10Y),然后让找到一个最小的m使得n/m是一个素数.
思路:
先用sqrt(n)的时间把所有的因子都求出来,然后在排序,枚举,就行了
#include <iostream>
#include <set>
#include <map>
#include <stack>
#include <cmath>
#include <queue>
#include <cstdio>
#include <bitset>
#include <string>
#include <vector>
#include <iomanip>
#include <cstring>
#include <algorithm>
#include <functional>
#include <ctime>
#define PI acos(-1)
#define eps 1e-8
#define inf 0x3f3f3f3f
#define debug(x) cout<<"---"<<x<<"---"<<endl
typedef long long ll;
using namespace std;
const int NP = 32000;
int ispri[NP] = {}, prime[NP], pcnt = 0;
void getprime()
{
ispri[0] = ispri[1] = 1;
for (long long i = 2; i < NP; i++)
if (ispri[i] == 0)
{
prime[++pcnt] = i;
for (long long j = i * i; j < NP; j += i)
{
ispri[j] = 1;
}
}
}
int a[1000000] = {}, icnt = 0;
void pdec(int n)
{
for (int i = 1; prime[i]*prime[i] <= n; i++)
if (n % prime[i] == 0)
{
a[++icnt] = prime[i];
n /= prime[i];
i--;
}
if (n != 1)
{
a[++icnt] = n;
}
}
int YZ[100000] , yzs;
void DB(int now)
{
yzs = 0;
int maxx = (int)sqrt(now);
for (int i = 1 ; i <= maxx ; i ++)
{
if (now % i == 0)
{
YZ[++yzs] = i;
YZ[++yzs] = now / i;
}
}
if (maxx * maxx == now)
{
yzs --;
}
}
int dd(int x)
{
if (x <= 1)
{
return 0;
}
int i, m = floor(sqrt(x) + 0.5);
for (i = 2; i <= m; i++)
if (x % i == 0)
{
return 0;
}
return 1;
}
int main()
{
int n;
while (cin >> n)
{
if (n <= 1)
{
printf("0\n");
continue;
}
int flag = 0;
DB(n);
sort(YZ + 1 , YZ + yzs + 1);
for (int i = 1; i <= yzs; i++)
{
int xx = n / YZ[i];
if (dd(xx))
{
cout << YZ[i] << endl;
flag = 1;
break;
}
}
if (flag == 0)
{
cout << "0" << endl;
}
}
return 0;
}
我们还是用另外一种方法去处理这道题,那就是质因数分解。
质因数分解:就是把一个合数分解成几个素数相乘的形式。例如:
48 = 2*2*2*3
58 = 2*3*3*3
由此我们可以将N一直除以一个比它自己本身小的质数,按照质因数分解的方法,不停的进行分解,我们要找到一个分解出的最大的素数P,因为只有这样,我们才能使得M最小。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <stack>
#include <queue>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <bitset>
#include <cassert>
using namespace std;
typedef long long LL;
const int N = 50;
int n;
void work()
{
int i , m , x = 0;
m = n;
for (i = 2 ; i * i <= n ; ++ i)
{
if (n % i == 0)
{
while (n % i == 0)
{
n /= i;
}
x = max(x , i);
}
}
if (n > 1)
{
x = max(x , n);
}
printf("%d\n" , x ? m / x : 0);
}
int main()
{
while (~scanf("%d", &n))
{
work();
}
return 0;
}