posts - 74,  comments - 33,  trackbacks - 0
The Embarrassed Cryptographer
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 4062 Accepted: 804

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
 
题目的大意是:给一个很大的数K,和一个普通的整数L,问K有没有小于L的质因子,有则输出“BAD 那个因子”,否则输出“GOOD”。
首先,明显当然要打一个素数表了。
接下来就是关键部分了,读入K,把K转成千进制。把数字往大进制转换能够加快运算效率。千进制的性质与十进制相似。例如,1234567890转成千进制,就变成了:[1][234][567][890]。

然后再从小到大枚举每一个素数,并对其进行高精度求余就行了。
下面是关键部分的一些代码:
int divide(int div) //高精度求余。 
{
    int i,ans=0;
    for(i=la-1;i>=0;i--)
        ans=(ans*1000+a[i])%div;
    return ans;    
}

void makeprime() //建立素数表。 
{
    int i,j,isprime,k,p=3;
    for(i=6;i<N;i++)
    {
        isprime=1;
        k=(int)sqrt(i);    
        for(j=0;j<p;j++)
        {
            if(prime[j]>k+1)
                break;
            if(i%prime[j]==0)
            {
                isprime=0;
                break;
            }    
        }
        if(isprime) prime[p++]=i;    
    }    
}

        len=strlen(s);
        for(i=0;i<len;i++) //转化为千进制。 
        {
            t=(len-i+2)/3-1;
            a[t]=a[t]*10+s[i]-'0';
        }
        la=(len+2)/3;
posted on 2008-12-24 21:51 KNIGHT 阅读(323) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


<2024年11月>
272829303112
3456789
10111213141516
17181920212223
24252627282930
1234567

常用链接

留言簿(8)

随笔档案

文章档案

Friends

OJ

搜索

  •  

最新评论

阅读排行榜

评论排行榜