POJ 2635, The Embarrassed Cryptographer
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 5907 Accepted: 1402
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.
Input
The input consists of no more than 20 test cases. Each test case is a line with the integers 4 <= K <= 10100 and 2 <= L <= 106. 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
Source
Nordic 2005
//
#include <iostream>
using namespace std;
bool check(int div,int KI[],int li)
{
int i = 0;
int remain = 0;
while (i < li)remain = (remain * 1000 + KI[i++]) % div;
return (remain == 0) ? false:true;
}
int main(int argc, char* argv[])
{
//create prime table
int const SIZE = 1000005;
bool isprime[SIZE];
memset(isprime, 1, sizeof(isprime));
int prime[80000];
memset(prime, 0, sizeof(prime));
isprime[0] = isprime[1] = false;
int j = 0;
for (int i = 2; i < SIZE; ++i)
if (isprime[i] == true)
{
prime[j++] = i;
int k = 2;
while(i * k < SIZE)isprime[i * k] = false, ++k;
};
int L,KI[40];
char K[105];
while (scanf("%s %d", &K, &L) != EOF && K[0] != '0' && L != 0)
{
//init 1000 base int array
int N = strlen(K);
int lowbound = 0, upbound = (N % 3 == 0) ? 3: N % 3;
int li = (N % 3 == 0)? N / 3: N /3 + 1;
int i = 0;
while (upbound <= N)
{
int num = 0;
for (int j = lowbound; j < upbound; ++j) num = num * 10 + (K[j] - '0');
KI[i++] = num;
lowbound = upbound;
upbound += 3;
}
//check good
i = 0;
bool good = true;
while(prime[i] < L)
{
if (check(prime[i], KI, li) == false)
{
good = false;
break;
}
++i;
}
if (good == true) cout <<"GOOD\n";
else cout << "BAD "<<prime[i]<<endl;
}
return 0;
}
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步