The Embarrassed Cryptographer POJ 2635 数论之高精度求模(同余模定理+千进制)

来源:互联网 发布:asp微信支付v3源码 编辑:程序博客网 时间:2024/06/02 18:08
The Embarrassed Cryptographer
Time Limit: 2000MSMemory Limit: 65536KTotal Submissions: 11344Accepted: 3022

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

Source

Nordic 2005
 
 
 
 
 
这个题目我觉得是非常有挑战性的,本来我看到第二个数是10^6,还是挺开心的,可以
 
不过我提交了就立刻呵呵了,第一个数太大了,longlong在取模是会出错的,所以我们
 
要用高精度求模的办法!!!
 
 
高精度求模:
 
123456是10进制的,因为每一位就一个数,那我们要计算它%t时,用同余模定理时
 
我们要1%t得出的sa,(sa*10+2)%t=sa;然后接着循环最后一位就是我们最终求的余数,
 
122%11=1,1%11=1;(1*10+2)%11=1;(1*10+2)%11=1;与我们最终求的结果相同,
 
但是我们的数太大了,会浪费很多时间,所以我们要用千进制来做,也就是说这必须
 
【123】【456】,但是我们求的时候用数组求必须注意次序,这个好说,这个求千进制
 
程序是很经典的,我也是借鉴了大神的思路!!!
 
 
#include<iostream>#include<cstdio>#include<cstring>#define M 10000040using namespace std;int prime[M];bool visit[M];int count;int q[100000];void table(){    memset(visit,true,sizeof(visit));int num = 0;int i;int j;for (int i = 2; i <= M; ++i){if (visit[i] == true){num++;prime[num] = i;}for (int j = 1; ((j <= num) && (i * prime[j] <= M));  ++j){visit[i * prime[j]] = false;if (i % prime[j] == 0) break;}}for(i=0;i<=M;i++){    if(prime[i])    count++;}}int  mod(int q[],int t,int len){    int sq=0;    int i;    for(i=len-1;i>=0;i--)    sq=(sq*1000+q[i])%t;    if(!sq)    return 0;    return 1;}int main(){    int i,j,k,t;    char n[100000];    int m;    table();    int flag=0;    while(cin>>n>>m)    {        if(m==0)        break;        flag=0;        int a;        int len=strlen(n);        memset(q,0,sizeof(q));        for(i=0;i<len;i++)        {            j=(len+2-i)/3-1;            q[j]=q[j]*10+(n[i]-'0');        }        len=(len+2)/3;        for(i=1;prime[i]<m;i++)        {           if(!mod(q,prime[i],len))           {               flag=1;               a=prime[i];               break;           }        }        if(flag==0)        printf("GOOD\n");        else        {            printf("BAD %d\n",a);        }    }    return 0;}

 
 
原创粉丝点击