POJ The Embarrassed Cryptographer 2635 (數論)

                     The Embarrassed Cryptographer
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 14151   Accepted: 3868

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

Source


題意:給出一個大數K,K是兩個素數的乘積

            再給出一個小於等於100W的L

            問K的兩個素數因子較小的那一個是否小於L

            小於輸出這個數,否則輸出GOOD (如果在100W內沒有找到因子,也輸出GOOD)


分析:首先我們先把100W的素數求出來(打表素數篩)

   然後根據同餘模定理,比如 123對3取餘

   就是1%3=1

            (1*10+2)%3=0

            (0*10+3)%3=0

            最後的結果就是0

   這裏需要把K轉爲千進制的,因爲十進制的一位一位的取餘會超時

   假設 K = 1234567891,轉成千進制的就是 [ 1 ] [ 123 ] [ 567 ] [ 891 ] ,用個數組存每一位就行

   這樣把上面的乘10改成乘1000就可以了


#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
using namespace std;
const int Max=1100000;
int pre[Max+10],pr[Max+10],top;
void paly_table() //素數篩
{
    int i,j,t;
    memset(pre,0,sizeof(pre));
    pre[2]=1;
    for(i=3;i<=Max;i++)
    {
        if(i&1)
            pre[i]=1;
    }
    t=sqrt(Max);
    for(i=0;i<=t;i++)
    {
        if(pre[i])
        {
            for(j=i*i;j<=Max;j+=2*i)
            {
                pre[j]=0;
            }
        }
    }
    top=0;
    for(i=0;i<=1000010;i++)
    {
        if(pre[i])
        {
            pr[top++]=i;
        }
    }
//    for(i=0;i<10;i++)
//    {
//       printf("%d ",pr[top-1]);
//    }
}

int main()
{
    char s[200];
    int a[200],l,mod,n,m,i,j;
    paly_table();
    while(~scanf("%s",s))
    {
        scanf("%d",&mod);
        if(s[0]=='0' && mod==0)
            break;
        l=strlen(s);
        n=0;
        memset(a,0,sizeof(a));
        m=l%3;
        if(m==1)   //轉千進制,用的方法好笨
        {
            a[0]=s[0]-'0';
            n++;
        }
        else if(m==2)
        {
            a[0]=(s[0]-'0')*10+(s[1]-'0');
            n++;
        }
        for(i=m;i<l;)
        {
            int ct=0;
            while(ct<3)
            {
                a[n]=a[n]*10+(s[i]-'0');
                i++;ct++;
            }
            n++;
        }
//        for(i=0;i<n;i++)
//        {
//            printf("%d ",a[i]);
//        }
//        printf("\n");
        int k=0;
        int flag=0;
        for(i=0;i<top;i++)
        {
            k=0;
            for(j=0;j<=n-1;j++)
            {
                k=(k*1000+a[j])%pr[i]; //取餘
                //printf("%d\n",k);
            }
            if(k==0)
            {
                flag=1;
                m=pr[i];
                break;
            }
        }

        if(flag && m<mod)  //能找到因子,且比給的數小
        {
            printf("BAD %d\n",m);
        }
        else
        {
            printf("GOOD\n");
        }

    }
    return 0;
}



發佈了74 篇原創文章 · 獲贊 48 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章