The Embarrassed Cryptographer
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 14480 | Accepted: 3954 |
Description
The young and very promising cryptographer Odd Even has implemented the security module of a large system with thousands of users, which is now in use in his company. The cryptographic keys are created from the product of two primes, and are believed to be secure because there is no known method for factoring such a product effectively.
What Odd Even did not think of, was that both factors in a key should be large, not just their product. It is now possible that some of the users of the system have weak keys. In a desperate attempt not to be fired, Odd Even secretly goes through all the users keys, to check if they are strong enough. He uses his very poweful Atari, and is especially careful when checking his boss' key.
What Odd Even did not think of, was that both factors in a key should be large, not just their product. It is now possible that some of the users of the system have weak keys. In a desperate attempt not to be fired, Odd Even secretly goes through all the users keys, to check if they are strong enough. He uses his very poweful Atari, and is especially careful when checking his boss' key.
Input
The input consists of no more than 20 test cases. Each test case is a line with the integers 4 <= K <= 10
100 and 2 <= L <= 10
6. K is the key itself, a product of two primes. L is the wanted minimum size of the factors in the key. The input set is terminated by a case where K = 0 and L = 0.
Output
For each number K, if one of its factors are strictly less than the required L, your program should output "BAD p", where p is the smallest factor in K. Otherwise, it should output "GOOD". Cases should be separated by a line-break.
Sample Input
143 10 143 20 667 20 667 30 2573 30 2573 40 0 0
Sample Output
GOOD BAD 11 GOOD BAD 23 GOOD BAD 31
题意很好理解
给你两个数你n,m ,求小于m的 最小的 n的质因子(有点乱) 有的话输出bad 质因子 没有的话就good
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
using namespace std;
const int maxn = 1000100;
bool vis[maxn] = {0};
char s[111];
int num[111];
int cnt;
void Oula_prime() ///素数筛 0 是素数
{
int i,j;
int k = sqrt(maxn+1); ///建议开方 防止爆炸
for(i=2; i<=k; i++)
{
if(vis[i] == 0)
{
for(j=i; i*j<=maxn; j++)
{
vis[i*j] = 1;
}
}
}
}
void Break_thousand() ///拆成千位算降低时间复杂度 逆序拆分 思路 1234567890 = [890][567][234][ 1]
{
cnt = 0;
int i,j,k=strlen(s);
int bits = 1;
int sum = 0;
for(i=k-1; i>=0; i--)
{
sum+=(s[i] -'0')*bits;
bits*=10;
if(bits == 1000)
{
bits = 1;
cnt++;
num[cnt] = sum;
sum = 0;
}
}
if(sum != 0) ///就是[ 1] 这情况 不到三还是需要加进来
{
cnt++;
num[cnt] = sum;
}
}
bool check(int mod)
{
int i;
int ans = 0;
for(i=cnt; i>=1; i--)
{
ans = (ans*1000 + num[i])%mod; ///类似ans*10...那是十进制
///ans = (ans*1000%mod + num[i]%mod)%mod; 看来cpu对除法(取余)还真是慢啊 换做这行在poj多了1000MS....
}
if(ans == 0)
{
return 0;
}
return 1;
}
int main()
{
int n,m,i;
int flag;
Oula_prime();
while(~scanf("%s%d",s,&n))
{
flag = 1;
if(s[0] == '0' && n==0)
{
return 0;
}
Break_thousand();
for(i=2; i<n; i++)
{
if(!vis[i])
{
if(check(i)==0)
{
printf("BAD %d\n",i);
flag = 0;
break;
}
}
}
if(flag)
{
printf("GOOD\n");
}
}
return 0;
}