poj 2635 素数打表+同余求模定理+千进制

来源:互联网 发布:交通大数据的优缺点 编辑:程序博客网 时间:2024/06/04 18:05

The Embarrassed Cryptographer
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 13831 Accepted: 3762

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,K是两个大素数的乘积的值。

再给定一个int内的数L

问这两个大素数中最小的一个是否小于L,如果小于则输出这个素数。



千进制:

把数字往大进制转换能够加快运算效率。若用十进制则耗费很多时间,会TLE。

千进制的性质与十进制相似。

例如,把K=1234567890转成千进制,就变成了:Kt=[  1][234][567][890]。

为了方便处理,我的程序是按“局部有序,全局倒序”模式存放Kt

即Kt=[890][567][234][1  ]  (一个中括号代表一个数组元素)




注意:

这里为什么要局部有序,全局倒序呢???

因为每一次都要乘以一个1000   要是不这样:

那么2734  这个数字就会  273 * 1000 +4   这样是不对的,只能是  2*1000 + 734



同余求模定理:

同余定理具有可加、可减、可乘、乘方性:  

(A + B) % C = (A % C + B % C) % C;

(A - B) % C = (A % C - B % C) % C;

(A * B) % C = (A % C * B % C) % C;

(A ^ B) % C = (A^N + B^N) % C;


例如m==1234时看,取模n:

((((1*10)%n+2%n)%n*10%n+3%n)%n*10%n+4%10)%n


要是大整数的话,可以用千进制或者万进制或者更大,只需要把 *10  编程 *1000或者*10000诸如此类的


模板

[cpp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. #include<stdio.h>  
  2.   
  3. char a[1000];//大整数  
  4. int n;//除数  
  5. int ans;//答案  
  6.   
  7. int main()  
  8. {  
  9.     while(scanf("%s%d",a,&n)!=EOF)  
  10.     {  
  11.         ans=0;  
  12.   
  13.         //下面是主要式子,  
  14.         for(int i=0;a[i]!='\0';i++)  
  15.             ans=((ans*10)%n+(a[i]-'0')%n)%n;  
  16.   
  17.         printf("%d\n",ans);  
  18.     }  
  19.   
  20.     return 0;  
  21. }  




AC代码:

#include<stdio.h>#include<algorithm>#include<string.h>using namespace std;const int maxx=1000100;int isprimer[maxx+1];char str[10000];int L,a[10000],len;//-------------------------------------//void primer(){    int p=0;    isprimer[p++]=2;    for(int i=3;i<=maxx;i+=2){//奇偶法        bool flag=true;        for(int j=0;isprimer[j]*isprimer[j]<=i;j++){            if(i%isprimer[j]==0){//根号法,递归法                flag=false;                break;            }        }        if(flag)            isprimer[p++]=i;    }}//-------------------------------------//void translate(){    memset(a,0,sizeof(a));    len=strlen(str);    for(int i=0;i<len;i++){        int pos=(len+2-i)/3-1;//因为是从a【0】开始存        a[pos]=a[pos]*10+(str[i]-'0');//不用考虑边界问题    }    len=(len+2)/3;//注意这里的妙用}//-------------------------------------//bool mod(int num,int len){    int ans=0;    for(int i=len-1;i>=0;i--)        ans=(ans*1000+a[i])%num;    if(ans==0)        return false;    return true;}//-------------------------------------//int main(){    primer();    while(scanf("%s%d",str,&L))    {        if(strcmp(str,"0")==0&&L==0)            return 0;        translate();        bool flag=true;        int p=0;        while(isprimer[p]<L)        {            if(!mod(isprimer[p],len)){                flag=false;                printf("BAD %d\n",isprimer[p]);                break;            }            p++;        }        if(flag)            printf("GOOD\n");    }    return 0;}


0 0
原创粉丝点击