hdu 2635

The Embarrassed Cryptographer
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions:16088   Accepted: 4410

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 <= 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


题目链接:http://poj.org/problem?id=2635

第一次敲高精度的题,好伐,看了题解,不过自己竟然看了后能一遍就AC,这让我很是迷啊。

题目的意思是说输入两个数n,k,n一定是两个质数的乘积,注意范围,如果这两个质数都大于k,则输出GOOD,否则输出BAD 和最小的那个质数。

高精度问题,加上同余取模定理,简单数论题,看代码就可以懂了。

--------------------- 作者:bless295 来源:CSDN 原文:https://blog.csdn.net/bless924295/article/details/52138758?utm_source=copy 版权声明:本文为博主原创文章,转载请附上博文链接!
#include<stdio.h>
#include<string.h>
#include<math.h>
int a[2333333];
char k[1000+5];
int prime[2333333],top;
int fen[1000];
void get_prime()
{
    int i,j;
    top=0;
    for(i=2;i<=1000000;i++)
    {
        if(a[i])
           continue;
        prime[top++]=i;
        for(j=i*2;j<=1000000;j+=i)//i*2不可是i*i,会越界
            a[j]=1; 
    }    
}
int main()
{
    int l;
    get_prime();
    while(scanf("%s%d",k,&l)!=EOF)
    {
        if(k[0]=='0'&&l==0)
           break;
        int len=strlen(k);
        int temp=0,num=0,num1=0;
        for(int i=len-1;i>=0;i--)//倒着分段存,一定先从最后一个开始,目的:取余时方便
        {
            temp+=(k[i]-'0')*pow(10,num);
            num++;
            if(num==3)
            {
                fen[num1]=temp;
                temp=num=0;
                num1++;
            }
        }
        if(temp)
           fen[num1++]=temp;
        int flag=0;
        for(int i=0;i<top;i++)
        {
            if(prime[i]>=l)//注意一定为>=
               break;
            int k1=0;
            for(int j=num1-1;j>=0;j--)
                k1=(k1*1000+fen[j])%prime[i];
            if(!k1)
            {
              printf("BAD %d\n",prime[i]);
              flag=1;
              break;
            }
        }
        if(!flag) 
           printf("GOOD\n");  
    }
    return 0;
}
View Code

猜你喜欢

转载自www.cnblogs.com/shenyuling/p/9769011.html