POJ 2635 The Embarrassed Cryptographer

来源:互联网 发布:php 得到类型 编辑:程序博客网 时间:2024/05/17 23:59
The Embarrassed Cryptographer
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 14158 Accepted: 3872

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大数取模运算,这里的大数要转换成三位运算一次,否则会TLE,L的范围不大,可以先直接打表存下来。
#include<iostream>#include<cstdio>#include<cstring>#include<cmath>using namespace std;const int m=1000001;int prime[m],isprime[m],top=0;void getprime(){    memset(isprime,0,sizeof(isprime));    isprime[0]=isprime[1]=1;    for(int i=2;i<m;i++)    {        if(isprime[i]) continue;        prime[top++]=i;        if(i*i>=m) continue;        for(int j=i*2;j<m;j+=i) isprime[j]=1;    }}int main(){    char a[110];    int aa[110];    int l;    getprime();    while(cin>>a>>l)    {        if(strlen(a)==1&&a[0]=='0'&&!l) break;        int len=(strlen(a))/3,you=strlen(a)-1;        for(int i=1;i<=len;i++)        {            int sum=0;            for(int j=you-2;j<=you;j++) sum=(sum*10)+(a[j]-'0');            you-=3;            aa[i]=sum;        }        if(you>=0)        {            int sum=0;            for(int j=0;j<=you;j++) sum=(sum*10)+(a[j]-'0');            aa[++len]=sum;        }      //  cout<<len<<endl;     //  for(int i=1;i<=len;i++) cout<<aa[i];       //cout<<endl;        int flag=0,tt;        for(int i=0;i<top;i++)        {            if(prime[i]>=l) break;            int ans=0;            for(int j=len;j>=1;j--) ans=((ans*1000)+aa[j])%prime[i];            if(!ans)            {                flag=1;                tt=prime[i];                break;            }        }        if(flag) cout<<"BAD "<<tt<<endl;        else cout<<"GOOD"<<endl;    }    return 0;}


0 0
原创粉丝点击