The Embarrassed Cryptographer
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 13999 | Accepted: 3819 |
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.
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
第一次敲高精度的题,好伐,看了题解,不过自己竟然看了后能一遍就AC,这让我很是迷啊。
题目的意思是说输入两个数n,k,n一定是两个质数的乘积,注意范围,如果这两个质数都大于k,则输出GOOD,否则输出BAD 和最小的那个质数。
高精度问题,加上同余取模定理,简单数论题,看代码就可以懂了。
代码:
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int a[2333333];
int b[2333333];
int fen[1000000];
int top=0;
void prime()//欧拉筛
{
int i,j;
top=0;
memset(a,0,sizeof(a));
for(i=2;i<=1000000;i++)
{
if(a[i])
continue;
b[top++]=i;
for(j=i*2;j<=1000000;j+=i)
{
a[j]=1;
}
}
}
int main()
{
char s[1000];
int k;
prime();
while(~scanf("%s",s)&&*s!='0')//高精度
{
scanf("%d",&k);
int len=strlen(s);
int ss=0;
int sum=0;
int num=0;
int i,j;
for(i=len-1;i>=0;i--)//四位一压或几位一压都无所谓的
{
int h=s[i]-'0';
for(j=1;j<=ss;j++)
{
h*=10;
}
sum+=h;
ss++;
if(ss==3)
{
fen[num++]=sum;//三位一分
ss=0;
sum=0;
}
}
if(sum!=0)//一定要有这个if
fen[num++]=sum;
int flag=0;//标记
int ans;
for(i=0;i<top;i++)
{
int h=b[i];
int kk=0;
if(h>=k)
break;
for(j=num-1;j>=0;j--)
{
kk=(kk*1000+fen[j])%h;//一开始余的k,脑残了
}
if(!kk)
{
ans=h;
flag=1;
break;
}
}
if(flag)
printf("BAD %d\n",ans);
else
printf("GOOD\n");
}
return 0;
}
哎,哎,哎,哎,哎,哎,哎,哎,哎,哎,哎,哎,哎,哎