The Embarrassed Cryptographer
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 14106 | Accepted: 3858 |
Description

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 参考思路 解题思路:给定两个数,一个数S是由两个素数 a,b相乘得到的数,另一个数L随便,问 min(a,b) 是否小于L。
解题思路:素数打表,然后从前往后遍历看S是否能被当前素数整除,如果整除,判断是否小于L。
注意:给定的S是大数,只能用字符数组保存。
一个数a判断能不能整除b,只要判断 a%b是否等于0就可以了。
同余定理:
(a+b)%c=(a%c+b%c)%c;
(a*b)%c=(a%c+b%c)%c;
对大数取余
模板: 大数字符串形式 a[1000];
char a[1000];
int m=0;
for(int i=0;a[i]!='\0';i++)
m=((m*10)%n+(a[i]-'0')%n)%n;//也可以写成 m=(m*10+a[i]-'0')%n
m为所求的余数
本题大数求余的方法为: 把字符串从后面起 三位三位的保存在一个int类型数组中,比如 12345 存在int类型数组里面为 12 345 然后按照前面的方法求:
bool mod(int n) { int m=0; for(int i=0;i<=k-1;i++) m=(m*1000+num[i])%n; if(m==0) return true; return false; } //另一个 <p>求一个10^100 的K 对 小于 10^6的 L 取余,问2-L-1 之间能否有能整除K的素数。</p><p>设k=a0a1a2a3a4a5a6a7a8a9a10.</p><p>k % m =a0a1a2a3a4a5a6*1000 %m + a7a8a9a10%m</p><p> =(a0a1a2*1000 %m + a3a4a5a6)*1000 %m + a7a8a9a10%m</p><p>于是很明显可以递归求解,k%m</p><p>于是枚举m,每次求解k就好了。。。。</p><p>注意素数打表的时候用j=i+i!!!</p><p>//</p><p>一开始害怕K太大存不下来而没有去细算直接转化为万进制,结果一直WA,后来转化为千进制就过了,</p><p>原因是 for循环时每次取余的时候,余数最大可达10^6,然后再乘10000就会爆int,然后又改成了 long long,转化为万进制,也是可以过的 </p> <pre name="code" class="cpp">#include<stdio.h> #include<string.h> #define maxn 1000005 char s[1100]; int isprime[maxn]; int prime[maxn]; int num[maxn]; int lenprime; int len; int k; void play_table() { int i,j; for(i=1;i<=maxn;i++) isprime[i]=1; isprime[0]=0; isprime[1]=0; lenprime = 0; for(i = 2; i <=maxn; i++) { if(isprime[i]) { prime[lenprime++]=i; for(j=i*2;j<=maxn;j+=i) isprime[j] = 0; } } } void change() { k = 0; int a = len/3; int b = len%3; int temp = 0; int i,j; if(b) { for(i=0;i<b;i++) temp=(temp*10+s[i]-'0'); num[k++] = temp; } for(i=b; i <len; i+=3) { num[k++] = (s[i]-'0')*100+(s[i+1]-'0')*10+s[i+2]-'0'; } } int mod(int n) { int i,j; int m = 0; for(i = 0; i <= k-1; i++) { m=(m*1000+num[i])%n; } if(m==0) return 1; else return 0; } int main() { int l; int i,j; int ok; int ans; play_table(); while(~scanf("%s%d",s,&l)) { ok = 0; len = strlen(s); if(l+s[0]-'0'==0) break; change(); for(i=0;i<lenprime;i++) { if(mod(prime[i])&&prime[i]<l) { ok = 1; ans = prime[i]; break; } } if(!ok) printf("GOOD\n"); else printf("BAD %d\n",ans); } return 0; }