POJ 2536 大数取模运算
The Embarrassed Cryptographer 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. 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. 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. 143 10 GOOD Nordic 2005 以下运算都在mod M的条件下进行 = a*(1000^3)%M + b*(1000^2)%M + c*(1000^1)%M + d*(1000^0)%M = 所以我们有: #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#include <queue>
#include <vector>
#include <sstream>
#define f(i,a,b) for(i = a;i<=b;i++)
#define fi(i,b) for(i = a;i>=b;i--)
using namespace std;
int p[1000100];
int a[200];
char s[200];
void init(){
memset(p,0,sizeof(p));
p[1] = 1;
int k = 2,i;
for(;k<=1000100;k++){
if(p[k]==0){
for(i = k*2;i<=1000100;i+=k){
p[i] = 1;
}
}
}
}
int cnt(int k,int len){
int i;
int tem = a[len-1]%k;
fi(i,len-2,0){
tem = (tem*1000+a[i])%k;
}
return tem;
}
int main()
{
init();
int b;
while(~scanf("%s %d",s,&b)&&(s[0]!='0'||b!=0)){
int len = strlen(s);
int i,j = 0;
for(i = len-1;i>=0;i-=3){
int tem;
if(i-2>=0)
tem = (s[i-2]-'0')*100+(s[i-1]-'0')*10+s[i]-'0';
else if(i-1>=0)
tem = (s[i-1]-'0')*10+s[i]-'0';
else if(i>=0)
tem = s[i]-'0';
a[j++] = tem;
}
int ok = 0;
f(i,2,b-1){
if(p[i]==0){
if(cnt(i,j)==0){
ok = 1;
break;
}
}
}
if(ok)
printf("BAD %dn",i);
else
printf("GOODn");
}
return 0;
}
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |