The Embarrassed Cryptographer

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

题目大意:

    给定一个大数N,它是又两个素数a,b相乘生成的。在给定一个c,判断是否 c<min(a,b). 若c大于min(a,b),输出BAD min(a,b);否则输出GOOD

解题思路:

    给定的c小于等于10^6。故将10^6以内的素数用筛选法筛选出来,在判断是否存在N%prime==0。

    由于N的范围为10^100。不能直接判断。

    举个例子:123%6

    ->((1%6*10+2)%6*10+3)%6

    可以分步取余。

    PS:一位一位取余数会超时。三位三位的取也不会超int型,且效率不会超时。

Code:

 /*************************************************************************
> File Name: poj2635.cpp
> Author: Enumz
> Mail: 369372123@qq.com
> Created Time: 2014年10月29日 星期三 01时13分50秒
************************************************************************/ #include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<list>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
#include<cmath>
#include<bitset>
#include<climits>
#define MAXN 1000010
using namespace std;
bool is_prime[MAXN];
char num[];
int num_int[];
int N;
void init()
{
is_prime[]=is_prime[]=;
for (int i=;i<=MAXN-;i++)
if (!is_prime[i])
for (int j=i+i;j<=MAXN-;j+=i)
is_prime[j]=;
}
int main()
{
init();
while (cin>>num>>N)
{
int len=strlen(num);
if (len==&&num[]==''&&N==) break;
int k=;
for (int i=len-; i>=;)
{
int tmp=;
tmp+=num[i--]-'';
if (i>=) tmp+=*(num[i--]-'');
if (i>=) tmp+=*(num[i--]-'');
num_int[k++]=tmp;
}
k--;
int t=k;
bool ok=;
for (int i=; i<N; i++)
if (!is_prime[i])
{
k=t;
int ans=num_int[k--]%i;
while (k>=) ans=(ans*+num_int[k--])%i;
if (!ans) {ok=;printf("BAD %d\n",i);break;}
}
if (!ok)
printf("GOOD\n");
}
return ;
}

POJ2635——The Embarrassed Cryptographer(高精度取模+筛选取素数)的更多相关文章

  1. HDU-2303 The Embarrassed Cryptographer 高精度算法(大数取模)

    题目链接:https://cn.vjudge.net/problem/HDU-2303 题意 给一个大数K,和一个整数L,其中K是两个素数的乘积 问K的是否存在小于L的素数因子 思路 枚举素数,大数取 ...

  2. (POJ2635)The Embarrassed Cryptographer(大数取模)

    The Embarrassed Cryptographer Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13041 Accep ...

  3. The Embarrassed Cryptographer(高精度取模+同余模定理)

    Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 11435   Accepted: 3040 Description The ...

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

    The Embarrassed Cryptographer Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 11978   A ...

  5. matlab取模与取余

    mod函数采用floor,rem函数采用fix函数.那么什么是floor和fix? fix(x):截尾取整.如: >> fix([3.4 , -3.4]) ans = 3 -3 floor ...

  6. POJ 2635 The Embarrassed Cryptographer 高精度

    题目地址: http://poj.org/problem?id=2635 题意:给出一个n和L,一直n一定可以分解成两个素数相乘. 让你判断,如果这两个素数都大于等于L,则输出GOOD,否则输出最小的 ...

  7. UVA 10006 - Carmichael Numbers 数论(快速幂取模 + 筛法求素数)

      Carmichael Numbers  An important topic nowadays in computer science is cryptography. Some people e ...

  8. HDU 2303 The Embarrassed Cryptographer

    The Embarrassed Cryptographer 题意 给一个两个素数乘积(1e100)K, 给以个数L(1e6), 判断K的两个素数是不是都大于L 题解 对于这么大的范围,素数肯定是要打表 ...

  9. HDU1013,1163 ,2035九余数定理 快速幂取模

    1.HDU1013求一个positive integer的digital root,即不停的求数位和,直到数位和为一位数即为数根. 一开始,以为integer嘛,指整型就行吧= =(too young ...

随机推荐

  1. wp8 自定义相机+nokia滤镜+录制amr音频

    demo截图:      代码量有点多,就不贴出来了. 备注: 1.自定义相机主要横竖屏时,对相机进行旋转. 2.播放amr格式可以在页面中直接添加MediaElement控件进行播放,或者使用Bac ...

  2. C#委托的异步调用1

    本文将主要通过“同步调用”.“异步调用”.“异步回调”三个示例来讲解在用委托执行同一个“加法类”的时候的的区别和利弊. 首先,通过代码定义一个委托和下面三个示例将要调用的方法: /*添加的命名空间 u ...

  3. IE=edge,chrome=1的META信息详解

    这几天在玩 HTML5 ★ Boilerplate,注意到meta信息中有这么一句: 复制代码 代码如下: <meta http-equiv="X-UA-Compatible" ...

  4. http返回状态代码及含义

    “100″ : Continue(继续) 初始的请求已经接受,客户应当继续发送请求的其余部分.(HTTP 1.1新) “101″ : Switching Protocols(切换协议) 请求者已要求服 ...

  5. mysql 拷贝表插入新的表

    insert into table1 select * from table; insert into talble set name  = value;

  6. PHP开发环境和软件

    1/很方便的软件XAMMP集成了PHP+MYSQL+MYPHPADMIN等等软件 2/sublime text 程序员神器,都明白的 ps.如果装了vm虚拟机,80端口有时候会被占用,进程关闭就好.

  7. (转)MVC 3 数据验证 Model Validation 详解

    继续我们前面所说的知识点进行下一个知识点的分析,这一次我们来说明一下数据验证.其实这是个很容易理解并掌握的地方,但是这会浪费大家狠多的时间,所以我来总结整理一下,节约一下大家宝贵的时间. 在MVC 3 ...

  8. ubuntu find方法

    通用格式:find pathname -options [-print -exec -ok]例子:find / -name filename 再根目录里面搜索文件名为filename的文件find / ...

  9. String声明为NULL和""的区别

    代码虐我千百遍,我待代码如初恋. String 声明为 NULL 则声明了一个变量不指向任何一块地址,则 length()会出现错误. 声明为"",则是一个长度为0的字符串.

  10. EF当实体模型与数据库的架构不同时要删除数据库时的报错问题

    当使用的EF的时候,我们都知道EF当实体模型与数据库的架构不同时要删除数据库,这是会把错: 无法创建与 'master' 数据库之间的连接,这是因为已打开原始数据库连接,并且已从连接字符串中删除凭据. ...