The Embarrassed Cryptographer
Description ![]() 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(4<=k<=10^100),一个整数l(2<=l<=10^6),请判断如果组成大数的最小素数大于等于l,输出"GOOD",否则输出"BAD a",a为两个素数中最小的那一个。
思路:
就是对2~l位置后面一个素数的范围整数内的素数打表,之后从小到大枚举素数,判断结果。不过数据比较大,java比较方便。
不用java需要利用同余模定理,并且还要化作千进制去取余,不然还是会超时,素数打表用根号试除同样也会TLE。
详细解析去戳它吧(主要是我不会啊)
示例程序
java:
Source Code
Problem: 2635 Code Length: 1110B
Memory: 5592K Time: 1782MS(4750MS) //括号里是用试除法打表的方式的运行时间
Language: Java Result: Accepted
import java.math.*;
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int l,a[]=new int[78500],i,i1,top;
BigInteger k;
a[0]=2;
top=1;
for(i=3;1000005>=i;i=i+2) //新技能get√
{
for(i1=0;i>=a[i1]*a[i1];i1++)
{
if(i%a[i1]==0)
{
break;
}
}
if(i<a[i1]*a[i1])
{
a[top]=i;
top++;
}
}
k=in.nextBigInteger();
l=in.nextInt();
while(k.compareTo(BigInteger.ZERO)!=0||l!=0)
{
for(i=0;l>a[i];i++)
{
if(k.mod(BigInteger.valueOf(a[i])).equals(BigInteger.ZERO)==true) //一直哇的原因就是用c里面的方式判断余数为0,没用equals()
{
break;
}
}
if(i==78500||a[i]>=l) //比c中的数组多开一个,不然会RE
{
System.out.println("GOOD");
}
else
{
System.out.println("BAD "+a[i]);
}
k=in.nextBigInteger();
l=in.nextInt();
}
}
}
c:
Source Code
Problem: 2635 Code Length: 1107B
Memory: 704K Time: 1235MS
Language: GCC Result: Accepted
#include <stdio.h>
#include <string.h>
int main()
{
int a[78499],l,i,i1,k[10000],num,pos,top;
char x[102];
a[0]=2;
top=1;
for(i=3;1000005>=i;i=i+2)
{
for(i1=0;i>=a[i1]*a[i1];i1++)
{
if(i%a[i1]==0)
{
break;
}
}
if(i<a[i1]*a[i1])
{
a[top]=i;
top++;
}
}
scanf("%s %d",x,&l);
while(x[0]!='0'||l!=0)
{
memset(k,0,sizeof(k));
top=strlen(x);
for(i=0;top>i;i++) //千进制换算
{
pos=(top+2-i)/3-1;
k[pos]=k[pos]*10+(x[i]-'0');
}
top=(top+2)/3;
for(i=0;l>a[i];i++)
{
num=0;
for(i1=top-1;i1>=0;i1--)
{
num=(num*1000+k[i1])%a[i]; //取出来取余,同余模定理
}
if(num==0)
{
break;
}
}
if(i==78499||a[i]>=l)
{
printf("GOOD\n");
}
else
{
printf("BAD %d\n",a[i]);
}
scanf("%s %d",x,&l);
}
return 0;
}