# POJ 2635 The Embarrassed Cryptographer //大数求余 素数打表 同余法

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.
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 10
143 20
667 20
667 30
2573 30
2573 40
0 0

Sample Output

GOOD
BAD 11
GOOD
BAD 23
GOOD
BAD 31

Source

[Submit]   [Go Back]   [Status]   [Discuss]

We have carefully selected several similar problems for you:  2300 2308 2301 2305 2306

(a+b)%n=((a%n)+(b%n))%n;

(a*b)%n=((a%n)*(b%n))%n;

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<stdlib.h>
#include<string.h>
#include<math.h>
using namespace std;
int main()
{
char n[1000];
int m;
scanf("%s %d",n,&m);
int len=strlen(n);
int ans=0;
for(int i=0;i<len;i++)
ans=(int) (((long long )ans*10+n[i]-'0')%m);
printf("%d\n",ans);
return 0;
} 

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<stdlib.h>
#include<string.h>
#include<math.h>
using namespace std;
const int maxn=1000005;
int prime[maxn+1]; //存放素数
int isprime[maxn+1];//作为素数标记数组，注意开的数组的大小
int primelen=0;//素数数组的长度
int main()
{
for(int i=0;i<=maxn; i++) //筛选素数
{
isprime[i]=1;
}
isprime[0]=isprime[1]=0;
for(int i=2;i<=maxn;i++)
{
if(isprime[i])
{
prime[primelen++]=i;
//cout<<i<<endl;
for(int j=2*i;j<=maxn;j=j+i)
{
isprime[j]=0;
}
}
}
for(int i=0;i<21;i++)  //只输出了前21个素数
{
cout<<prime[i]<<" ";
}
cout<<endl;
for(int i=0;i<21;i++)
{
cout<<isprime[i]<<" ";
}
cout<<endl;
} 

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<stdlib.h>
#include<string.h>
#include<math.h>
using namespace std;
const int maxn=1000005;
int prime[maxn+1]; //存放素数
int isprime[maxn+1];//作为素数标记数组，注意开的数组的大小
int primelen=0;//素数数组的长度
int num[maxn];//存放大数 数组中的数据是倒着存的（每三位存放一个）
//例如 12345 存放在 数组中的数据就是 12  345 （倒着存的
char s[maxn];//输入的大数
int l;//输入的l 是任意的
int k;
int len; //大数的长度

void sushu(int n)  //筛选素数
{
for(int i=0;i<=n; i++)
{
isprime[i]=1;
}
isprime[0]=isprime[1]=0;
for(int i=2;i<=n;i++)
{
if(isprime[i])
{
prime[primelen++]=i;
//cout<<i<<endl;
for(int j=2*i;j<=n;j=j+i)
{
isprime[j]=0;
}
}
}
}

void change()  //用来往num数组中存放大数
{
k=0;
int a=len/3; //判断输入的大数的长度有个长度为3的
int b=len%3;//能否被3整除，不能整除的话剩下长度为多少的数据
//例如 12345 len=5 5%3=2   剩下长度为2（即 12   倒着来
int temp=0;
if(b) //能被3整除 b=0 就直接三位三位存放在num数组中，如果不能被整除，就要先把剩余的存放在num数组中
{
for(int i=0;i<b;i++)
temp=temp*10+s[i]-'0';
num[k++]=temp;//存放大数
}
int cnt=0;
//int start=b+3*cnt;
for(int i=1;i<=a;i++)
{
temp=0;
int start=b+3*cnt;//用来控制存放时的开始位置  从b位置开始  依次增加3
for(int j=0;j<3;j++)
{
temp=temp*10+s[start+j]-'0';
}
num[k++]=temp;//存放大数
cnt++;
}
}
bool mod(int n) //大数取余法   把大的数据逐步取模这样就会避免最后取模时因数据太大而超时
{
int m=0;   //m的初值要设置为0
for(int i=0;i<k;i++)
{
m=(m*1000+num[i])%n;
}
if(m==0)
{
return true;
}
else
{
return false;
}
}
int main()
{
sushu(maxn);

while(cin>>s>>l && s[0]!='\0' && l)
{
len=strlen(s);
change();
bool ok=1;
int ans=0;
for(int i=0;i<primelen;i++)
{
if(mod(prime[i]) && prime[i]<l) //如果能被整除 并且整除的素数因子小于l
{
ok=0;
ans=prime[i];
break;
}
if(prime[i]>=l) //如果素数因子 大于l 就退出 证明不会被整除
break;
}
if(ok)
{
cout<<"GOOD"<<endl;
}
else
{
cout<<"BAD "<<ans<<endl;
}
}
return 0;
}