poj_2635 The Embarrassed Cryptographer(高精度求模)

来源:互联网 发布:推广软件拦截 编辑:程序博客网 时间:2024/04/30 03:16
The Embarrassed Cryptographer
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 14367 Accepted: 3926

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 10143 20667 20667 302573 302573 400 0

Sample Output

GOODBAD 11GOODBAD 23GOODBAD 31
题目输入的整数数值达到了10^100,得用数组来保存,十进制保存的话会超时,用千进制可以减少运算时间,即将数字1234567890保存为
a[] = {890, 567, 234, 1}。之后高精度求模。
#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <cmath>#include <stack>#include <bitset>#include <queue>#include <set>#include <map>#include <string>#include <algorithm>#define FOP freopen("data.txt","r",stdin)#define FOP2 freopen("data1.txt","w",stdout)#define inf 0x3f3f3f3f#define maxn 1000010#define mod 1000000007#define PI acos(-1.0)#define LL long longusing namespace std;bool vis[maxn];int prime[maxn];int cot;void sieve(int n){    int m = (int)sqrt(n+0.5);    for(int i = 2; i <= m; i++) if(!vis[i])            for(int j = i*i; j <= n; j += i) vis[j] = 1;    for(int i = 2; i <= n ; i++) if(!vis[i]) prime[cot++] = i;}char s[110];int a[40];int n;int b;bool judge(int *a, int n, int b){    int res = 0;    for(int i = n-1; i >= 0; i--)    {        res = (res * 1000 + a[i]) % b;    }    return res == 0;}int main(){    //FOP;    sieve(1000010);    while(~scanf("%s%d", s+1, &b) && b)    {        n = 0;        memset(a, 0, sizeof(a));        int len = strlen(s+1), i;        for(i = len - 2; i >= 1; i -= 3)        {            a[n++] = (s[i] - '0') * 100 + (s[i+1] - '0') * 10 + s[i+2] - '0';        }        i += 2;        for(int j = 1; j <= i; j++)        {            a[n] = a[n] * 10 + s[j] - '0';            if(j == i) n++;        }        int ans = 0;        for(i = 0; prime[i] < b; i++)        {            if(judge(a, n, prime[i]))            {                ans = prime[i];                break;            }        }        if(ans == 0) printf("GOOD\n");        else printf("BAD %d\n", ans);    }    return 0;}


0 0