首页 > ACM题库 > HDU-杭电 > hdu 2303 The Embarrassed Cryptographer-数论-[解题报告]C++
2014
01-05

hdu 2303 The Embarrassed Cryptographer-数论-[解题报告]C++

The Embarrassed Cryptographer

问题描述 :

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.

输入:

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.

输出:

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.

样例输入:

143 10
143 20
667 20
667 30
2573 30
2573 40
0 0

样例输出:

GOOD
BAD 11
GOOD
BAD 23
GOOD
BAD 31

进制转化+筛选法求素数。

题意:

给你两个数m,n;m是两个素数的乘。如果这两个素数中最小的那个小于(是小于!!!!)n的话,就输出BAD 那个数;否则输出GOOD;

做法:

先用素数筛把小于1100000的素数都找出来。

然后把m转化为千进制。

对于一个m,把i从2到n遍历一遍,如果i为素数&&m%i==0,说明m可以整除i;

#include<iostream>
#include<stdio.h>
#include<math.h>
#include<string.h>
using namespace std;
#define Max 1100000
int isp[Max];
void isprim()
{
    int i,j;
    isp[0]=isp[1]=0;
    isp[2]=1;
    for(i=3;i<Max;i++)
        isp[i]=i%2;
    int ns;
    ns=(int)sqrt(Max*1.0);
    for(i=3;i<=ns;i++)
    {
        if(isp[i])
        {
            for(j=i*2;j<Max;j+=i)
                isp[j]=0;
        }
    }
}
int main()
{
    int len,n,i,j,k,ks;
    int num,nums;
    char str[1000];
    isprim();
    while(scanf("%s%d%*c",str,&n)&&(!(str[0]=='0'&&n==0)))
    {
        len=strlen(str);
        for(k=2;k<n;k++)
        {
            if(isp[k]==0)
            {
                continue;
            }
            num=0;
            for(i=0;i<len;i+=3)
            {
                nums=0;
                ks=1;
                for(j=i;j<i+3&&j<len;j++)
                {
                    ks=ks*10;
                    nums=nums*10+(str[j]-'0');
                }
                num=num*ks+nums;
                num=num%k;
            }
         //   printf("num=%d,k=%d\n",num,k);
            if(num==0)
            {
                printf("BAD %d\n",k);
                break;
            }
        }
        if(k==n)printf("GOOD\n");
    }
    return 0;
}

解题转自:http://blog.csdn.net/rowanhaoa/article/details/8589705