poj 2635 The Embarrassed Cryptographer(同余模定理的应用)

来源:互联网 发布:如何在淘宝打开链接 编辑:程序博客网 时间:2024/06/05 05:53

题目链接:点击打开链接

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

题目大意:题意很简单,就是数大了点,先描述一下意思,给你一个数K<=10^100(肯定不是int),在给你一个数L,而且k是由两个素数相乘得到的,问这两个素数的大小,如果有一个素数小于L,就输出BAD和这个素数,否则就输出GOOD,

基本思路:用到同于模定理的知识,我也是现学的,接触到了个新知识点,可以访问同余模定理文库学习相关知识.然后在再优化,将10进制的转化为1000进制的,避免在循环时超时,具体怎么做上面那个链接很清楚.

<span style="font-size:18px;">#include <iostream>#include<cstring>#include<cstdio>#include<cstdlib>using namespace std;char s[1000];int prime[1000100];int top;bool vis[1000100];int big[1000];///筛选素数void chooseprime(){    memset(vis,0,sizeof(vis));    for(int i=2;i<=1000015;i++)    {        if(!vis[i])        {            prime[++top]=i;            for(int j=i+i;j<=1000015;j=j+i)            {                if(j%i==0)vis[j]=1;            }        }    }}int main(){    chooseprime();    int n;    while(scanf("%s%d",s,&n))    {        if(n==0&&s[0]=='0')break;        int sum=0,ans=0;        int len=strlen(s);        int t;        int k=0;        ///转化为1000进制,注意从个位开始        for(int i=len-1;i>=0;i--)        {            t=s[i]-'0';            for(int j=1;j<=ans;j++)                t=t*10;            sum=sum+t;            ans++;            if(ans==3)            {                big[++k]=sum;                sum=ans=0;            }        }        if(sum!=0)big[++k]=sum;        int f=0;        int ant;        ///查找素数        for(int i=1;i<=top;i++)        {            int m=prime[i];            if(m>=n)break;///因为是从小到大寻找素数,如果小的不符合,那么            ///后面的素数一定不符合            int d=0;            for(int j=k;j>=1;j--)///从最高位进行同余模定理的应用            {                d=(d*1000+big[j])%m;            }            if(d==0)            {                f=1;                ant=m;                break;            }        }        if(f)        {            cout<<"BAD "<<ant<<endl;        }        else cout<<"GOOD\n";    }    return 0;}</span>

1 0
原创粉丝点击