The Embarrassed Cryptographer--高精度

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.

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 和最小的那个质数。

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

代码:

#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;
}

哎,哎,哎,哎,哎,哎,哎,哎,哎,哎,哎,哎,哎,哎


  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值