您的位置:首页 > 其它

poj 2635(The Embarrassed Cryptographer(把…译成密码) 素数打表的最优方法+10进制转换成1000进制,大数取模

2016-08-22 18:31 323 查看
The Embarrassed Cryptographer(把…译成密码)
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 13831 Accepted: 3762
DescriptionThe young and very promising cryptographer Odd Even has implemented(实施) thesecurity module of a large system with thousands of users, which is now in use in his company. Thecryptographic(关于暗号的) keys are createdfrom the product of two primes(初期), and are believed to be secure(安全的) becausethere is no known method for factoring(做代理商) such a producteffectively(有效地).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(不顾一切的)attemptnot 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.InputThe 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 thewanted minimum(最小的) size of the factors(因素) inthe key. Theinput(投入) set is terminated(终止) bya case where K = 0 and L = 0.OutputFor each number K, if one of its factors are strictly less than the required L, your program should output(输出) "BAD p", where p isthe smallest factor in K. Otherwise, it should output "GOOD". Cases should be separated by a line-break.Sample Input
143 10
143 20
667 20
667 30
2573 30
2573 40
0 0
Sample Output
GOOD
BAD 11
GOOD
BAD 23
GOOD
BAD 31、
题意:判断K是否可以由小于L的两个素数相乘得到,不能输出GOOD,否则输出BAD 与最小的素数
#include <stdio.h>#include <string.h>#include <math.h>#include <algorithm>const int ma=1000100;int a[ma+1];void sushu()            //素数打表最优方法{int s=0;a[++s]=2;for(int i=3; i<ma; i=i+2){int t=0;for(int j=1;; j++){if(a[j]*a[j]>i)break;if(i%a[j]==0){t=1;break;}}if(!t)a[++s]=i;}}char c[1012];int n;int b[1234];int change(char* c,int l)     //将10进制转换成1000进制{int k=l%3;int j=0;if(k){int si=0;for(int i=0; i<k; i++){si=(si*10+c[i]-'0');}b[++j]=si;}if(l/3){for(int i=k; i<l; i=i+3){int si=(c[i]-'0')*100+(c[i+1]-'0')*10+(c[i+2]-'0');b[++j]=si;}}return j;}int main(){sushu();while(~scanf("%s %d",c,&n)){if(c[0]=='0'&&n==0)break;int l=strlen(c);int t=0;int p=change(c,l);for(int k=1; a[k]<n; k++){int s=0;for(int i=1; i<=p; i++){s=(s*1000+b[i])%a[k];}if(!s){t=a[k];break;}}if(t)printf("BAD %d\n",t);else printf("GOOD\n");}return 0;}

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: