The Embarrassed Cryptographer
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 14151 | Accepted: 3868 |
Description
![](https://i-blog.csdnimg.cn/blog_migrate/a64f64479d9437bf747b1ad5425cdc17.jpeg)
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 <= 10
100
and 2 <= L <= 10
6. 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
题意:给出一个大数K,K是两个素数的乘积
再给出一个小于等于100W的L
问K的两个素数因子较小的那一个是否小于L
小于输出这个数,否则输出GOOD (如果在100W内没有找到因子,也输出GOOD)
分析:首先我们先把100W的素数求出来(打表素数筛)
然后根据同余模定理,比如 123对3取余
就是1%3=1
(1*10+2)%3=0
(0*10+3)%3=0
最后的结果就是0
这里需要把K转为千进制的,因为十进制的一位一位的取余会超时
假设 K = 1234567891,转成千进制的就是 [ 1 ] [ 123 ] [ 567 ] [ 891 ] ,用个数组存每一位就行
这样把上面的乘10改成乘1000就可以了
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
using namespace std;
const int Max=1100000;
int pre[Max+10],pr[Max+10],top;
void paly_table() //素数筛
{
int i,j,t;
memset(pre,0,sizeof(pre));
pre[2]=1;
for(i=3;i<=Max;i++)
{
if(i&1)
pre[i]=1;
}
t=sqrt(Max);
for(i=0;i<=t;i++)
{
if(pre[i])
{
for(j=i*i;j<=Max;j+=2*i)
{
pre[j]=0;
}
}
}
top=0;
for(i=0;i<=1000010;i++)
{
if(pre[i])
{
pr[top++]=i;
}
}
// for(i=0;i<10;i++)
// {
// printf("%d ",pr[top-1]);
// }
}
int main()
{
char s[200];
int a[200],l,mod,n,m,i,j;
paly_table();
while(~scanf("%s",s))
{
scanf("%d",&mod);
if(s[0]=='0' && mod==0)
break;
l=strlen(s);
n=0;
memset(a,0,sizeof(a));
m=l%3;
if(m==1) //转千进制,用的方法好笨
{
a[0]=s[0]-'0';
n++;
}
else if(m==2)
{
a[0]=(s[0]-'0')*10+(s[1]-'0');
n++;
}
for(i=m;i<l;)
{
int ct=0;
while(ct<3)
{
a[n]=a[n]*10+(s[i]-'0');
i++;ct++;
}
n++;
}
// for(i=0;i<n;i++)
// {
// printf("%d ",a[i]);
// }
// printf("\n");
int k=0;
int flag=0;
for(i=0;i<top;i++)
{
k=0;
for(j=0;j<=n-1;j++)
{
k=(k*1000+a[j])%pr[i]; //取余
//printf("%d\n",k);
}
if(k==0)
{
flag=1;
m=pr[i];
break;
}
}
if(flag && m<mod) //能找到因子,且比给的数小
{
printf("BAD %d\n",m);
}
else
{
printf("GOOD\n");
}
}
return 0;
}