加载中...

POJ 2635 - The Embarrassed Cryptographer


0x00 POJ 原题

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

Source

Nordic 2005

0x10 题目概述

给定一个大数 K,K 是两个大素数的乘积的值。

再给定一个 int 内的数 L

问这两个大素数中最小的一个是否小于 L,如果小于则输出这个素数。

0x20 解题思路

首先对题目的插图表示无语。。。

高精度求模+同余模定理,解题步骤:

  1. Char 格式读入 K。把 K 转成千进制 Kt,同时变为 int 型。

把数字往大进制转换能够加快运算效率。若用十进制则耗费很多时间,会 TLE。

千进制的性质与十进制相似。

例如,把 K=1234567890 转成千进制,就变成了: Kt=[ 1][234][567][890]。

为了方便处理,我的程序是按“局部有序,全局倒序”模式存放 Kt

即 Kt=[890][567][234][1 ] (一个中括号代表一个数组元素)

  1. 素数打表,把 10^6 内的素数全部预打表,在求模时则枚举到小于 L 为止。

注意打表不能只打到 100W,要保证素数表中最大的素数必须大于 10^6,否则当 L=100W 且 K 为 GOOD 时,会因为数组越界而 RE,这是因为越界后 prime 都是负无穷的数,枚举的 while(prime[pMin]<L) 循环会陷入死循环

  1. 高精度求模。

主要利用 Kt 数组和同余模定理。

例如要验证 124 是否被 3 整除,只需求模 124%3

但当 124 是一个大数时,就不能直接求,只能通过同余模定理对大数“分块”间接求模

具体做法是:

  • 先求 1%3 = 1
  • 再求 (1*10+2)%3 = 0
  • 再求 (0*10+4)%3 = 1

那么就间接得到 124%3=1,这是显然正确的

而且不难发现, (1*10+2)*10+4 = 124

这是在 10 进制下的做法,千进制也同理,*10 改为 *1000 就可以了

0x30 测试数据

0x40 AC 源码

//Memory Time
//624K  1235MS 

#include<iostream>
#include<string.h>
using namespace std;

const int Range=1000100;  //打表不能只打到100W,素数表中最大的素数必须大于10^6

int Kt[10000];  //千进制的K
int L;
int prime[Range+1];

/*素数组打表*/
void PrimeTable(void)
{
    int pNum=0;
    prime[pNum++]=2;

    for(int i=3;i<=Range;i+=2)  //奇偶法
    {
        bool flag=true;
        for(int j=0;prime[j]*prime[j]<=i;j++)  //根号法+递归法
            if(!(i%prime[j]))
            {
                flag=false;
                break;
            }
        if(flag)
            prime[pNum++]=i;
    }
    return;
}

/*高精度K对p求模,因数检查(整除)*/
bool mod(const int* K,const int p,const int len)
{
    int sq=0;
    for(int i=len-1;i>=0;i--)  //千进制K是逆序存放
        sq=(sq*1000+K[i])%p;  //同余模定理

    if(!sq)   //K被整除
        return false;
    return true;
}

int main(void)
{
    PrimeTable();

    char K[10000];
    while(cin>>K>>L && L)
    {
        memset(Kt,0,sizeof(Kt));
        int lenK=strlen(K);
        for(int i=0;i<lenK;i++)  //把K转换为千进制Kt,其中Kt局部顺序,全局倒序
        {                      //如K=1234567=[  1][234][567] ,则Kt=[567][234][1  ]
            int pKt=(lenK+2-i)/3-1;
            Kt[pKt]=Kt[pKt]*10+(K[i]-'0');
        }
        int lenKt=(lenK+2)/3;

        bool flag=true;
        int pMin=0;  //能整除K且比L小的在prime中的最小素数下标
        while(prime[pMin]<L)  //枚举prime中比L小的素数
        {
            if(!mod(Kt,prime[pMin],lenKt))
            {
                flag=false;
                cout<<"BAD "<<prime[pMin]<<endl;
                break;
            }
            pMin++;
        }
        if(flag)
            cout<<"GOOD"<<endl;
    }
    return 0;
}

相关资料


文章作者: EXP
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 EXP !
赏
  目录