POJ 2635-The Embarrassed Cryptographer(高精度求模+同余模定理)

The Embarrassed Cryptographer

Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Submit Status Practice POJ
2635

Appoint description: 
System Crawler  (2015-05-28)

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

题意:给定一个大数str,str是两个大素数的乘积的值,再给定一个int内的数n,问这两个大素数中最小的一个是否小于n,如果小于则输出GOOD,否则输出BAD和两个素数中最小的。

思路:先用素数筛把小于1100000的素数都找出来。然后把str转化为一堆千进制。对于每一个千进制,把i从2到n遍历一遍即可。

PS:然而学习了一下高精度求模,原来可以这么搞,Orz。

然而中学生的高精度取模也是醉了点击打开链接

高精度求模。

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

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

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

具体做法是:

先求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就可以了。

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <set>
#include <queue>
#include <stack>
#include <map>
using namespace std;
typedef long long LL;
const int inf=0x3f3f3f3f;
const double eps=1e-10;
const double pi= acos(-1.0);
const int MAXN=1e6+10;
int prime[MAXN]={2,3,5};
int qk[1010];
//素数筛
void is_prime()
{
    int i,j;
    int flag=0;
    int gcd=2;
    int k=3;
    for(i=7;i<MAXN;i+=gcd){
        flag=0;
        gcd=6-gcd;
        for(j=0;prime[j]*prime[j]<=i;j++){
            if(i%prime[j]==0){
                flag=1;
                break;
            }
        }
        if(!flag)
            prime[k++]=i;
    }
}
//高精度K对p求模,因数检查(整除)
int mod(int *K,int m,int len)
{
   int res=0;
   for(int i=len-1;i>=0;i--)//千进制K是逆序存放,因为计算起来方便
    res=(res*1000+K[i])%m; //同余模定理
   if(!res) return 0; //K被整除
   else  return 1;
}

int main()
{
    char str[1010];
    int n,i,j;
    int flag;
    is_prime();
    while(~scanf("%s %d",str,&n)){
        if(str[0]=='0'&&n==0) break;
        memset(qk,0,sizeof(qk));
        int len=strlen(str);
        int ii=0;
        for(int i=len-1;i>=0;i-=3){//把str转换为千进制qk,其中qk局部顺序,全局倒序
                                   //如str=1234567=[  1][234][567] ,则qk=[567][234][1  ]
            if(i>=2)
            qk[ii]=(str[i]-'0')+(str[i-1]-'0')*10+(str[i-2]-'0')*100;
            else if(i==1)
            qk[ii]=(str[i]-'0')+(str[i-1]-'0')*10;
            else if(i==0)
            qk[ii]=(str[i]-'0');
            ii++;
        }
        int len_qk=(len+2)/3;
        int qMin=0;//能整除qk且比n小的在prime中的最小素数下标
        flag=1;
        while(prime[qMin]<n){//枚举prime中比L小的素数
            if(!mod(qk,prime[qMin],len_qk)){
                flag=0;
                printf("BAD %d\n",prime[qMin]);
                break;
            }
            qMin++;
        }
        if(flag)
            puts("GOOD");
    }
    return 0;
}
时间: 2024-10-13 02:16:41

POJ 2635-The Embarrassed Cryptographer(高精度求模+同余模定理)的相关文章

poj 2635 The Embarrassed Cryptographer

题目链接:http://poj.org/problem?id=2635 思路:当看到K的最大值为 10100 的第一想法就是用java打大数,建立一个素数表,然后再在素数表中去找,看是否有符合条件的. code: import java.math.*; import java.util.*; public class ggg { public static void main(String[] args) { int len=0; Scanner cin=new Scanner(System.i

POJ 2635 The Embarrassed Cryptographer(高精度取模 + 同余模定理)

The Embarrassed Cryptographer Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 12905   Accepted: 3472 Description The young and very promising cryptographer Odd Even has implemented the security module of a large system with thousands of

poj 2635 The Embarrassed Cryptographer 筛素数+高精度除法

题意: 给K(<10^100),L(<10^6),求K小于L的最小素因子并输出,如果没有则输出GOOD. 分析: 枚举小于L的素数用高精度除法判断是否是因子,关键是怎么高效筛素数,先给一种比较慢的筛法: primes[max_prime_num],num=0; memset(vis,0,sizeof(vis)) for(int i=2;i<maxL;++i) if(vis[i]==0){ prime[num++]=i; for(int j=2*i;j<maxL;j+=i) vis[

【阔别许久的博】【我要开始攻数学和几何啦】【高精度取模+同余模定理,*】POJ 2365 The Embarrassed Cryptographer

题意:给出一大数K(4 <= K <= 10^100)与一整数L(2 <= L <= 106),K为两个素数的乘积(The cryptographic keys are created from the product of two primes) 问构成K的最小素数是否绝对小于L,若是,则输出BAD p,p为最小素数,否则输出GOOD; 分析:从小到大枚举1~10^6内的素数p,while(p<L)时,判断K是否能被p整除,若能则证明构成K的最小素数绝对小于L,反之则大于L

poj 2635 The Embarrassed Cryptographer (同余定理,筛选法)

链接:poj 2635 题意:给定一个大数k,k是两个大素数的乘积的值,再给定一个int内的数L 问这两个大素数中最小的一个是否小于L,如果小于则输出这个素数. 分析:因为k达到了10^100,只能用字符串读入,再转化为千进制,用int数组存储, 然后枚举小于L的素数,看是否能被整除,即判断k%L是否为0, 这样就得先用筛选法求素数打表,但是注意要打表到大于10^6 关于高精度取余,就需要用到同余定理 如 123456%N=(((123%N)*1000%N)+456)%N #include<st

[ACM] POJ 2635 The Embarrassed Cryptographer (同余定理,素数打表)

The Embarrassed Cryptographer Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 11978   Accepted: 3194 Description The young and very promising cryptographer Odd Even has implemented the security module of a large system with thousands of

POJ 2635 The Embarrassed Cryptographer (同余线性方程+素数筛)

题目地址:POJ 2635 先用素数筛把10^6万以内素数筛出来.然后把输入的那个大数转化成数组,并且每三位存成一个数,这样可以节约内存和时间,然后利用同余线性的原理,对那个小整数以内的所有素数枚举,然后判断是否整除,找到最小的能被整除的. 代码如下: #include <iostream> #include <string.h> #include <math.h> #include <queue> #include <algorithm> #i

POJ 2635 The Embarrassed Cryptographer 线性筛+高精度取模

题目大意:给两个数,第一个数的范文是10^100,第二个数10^6,第一个数是两个质数的乘积,问有没有不超过第二个数的数是第一个树的因子. 思路:10^6中只有7w+个素数,只要挨个判定能不能整除即可.然后这个题素数必须线性,不然就T.还有高精度一开始我压了4位,之后就wa,调了很长时间发现判断整除的过程中爆int了,换成long long就是T,最后十分生气,直接压到了7位,果断A了,好像时间还不慢.如果要用int的话,最多只能压3位,应该也能过. CODE: #include <cmath>

POJ 2635 The Embarrassed Cryptographer(大数求余)

题意:给出一个大数,这个大数由两个素数相乘得到,让我们判断是否其中一个素数比L要小,如果两个都小,输出较小的那个. 分析:大数求余的方法:针对题目中的样例,143 11,我们可以这样算,1 % 11 = 1:      1×10 + 4 % 11 = 3:      3×10 + 3 % 11 = 0;我们可以把大数拆成小数去计算,同余膜定理保证了这个算法的这正确性,而且我们将进制进行一定的扩大也是正确的. 注意:素数打标需要优化,否则超时.   进制需要适当,100和1000都可以,10进制超