POJ2635——The Embarrassed Cryptographer(高精度取模+筛选取素数)
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(高精度取模+筛选取素数)的更多相关文章
- HDU-2303 The Embarrassed Cryptographer 高精度算法(大数取模)
题目链接:https://cn.vjudge.net/problem/HDU-2303 题意 给一个大数K,和一个整数L,其中K是两个素数的乘积 问K的是否存在小于L的素数因子 思路 枚举素数,大数取 ...
- (POJ2635)The Embarrassed Cryptographer(大数取模)
The Embarrassed Cryptographer Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13041 Accep ...
- The Embarrassed Cryptographer(高精度取模+同余模定理)
Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 11435 Accepted: 3040 Description The ...
- [ACM] POJ 2635 The Embarrassed Cryptographer (同余定理,素数打表)
The Embarrassed Cryptographer Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 11978 A ...
- matlab取模与取余
mod函数采用floor,rem函数采用fix函数.那么什么是floor和fix? fix(x):截尾取整.如: >> fix([3.4 , -3.4]) ans = 3 -3 floor ...
- POJ 2635 The Embarrassed Cryptographer 高精度
题目地址: http://poj.org/problem?id=2635 题意:给出一个n和L,一直n一定可以分解成两个素数相乘. 让你判断,如果这两个素数都大于等于L,则输出GOOD,否则输出最小的 ...
- UVA 10006 - Carmichael Numbers 数论(快速幂取模 + 筛法求素数)
Carmichael Numbers An important topic nowadays in computer science is cryptography. Some people e ...
- HDU 2303 The Embarrassed Cryptographer
The Embarrassed Cryptographer 题意 给一个两个素数乘积(1e100)K, 给以个数L(1e6), 判断K的两个素数是不是都大于L 题解 对于这么大的范围,素数肯定是要打表 ...
- HDU1013,1163 ,2035九余数定理 快速幂取模
1.HDU1013求一个positive integer的digital root,即不停的求数位和,直到数位和为一位数即为数根. 一开始,以为integer嘛,指整型就行吧= =(too young ...
随机推荐
- 函数 sort,unique,stable_sort,count_if,谓词
bool isShorter(const string &s1,const string &s2) { return s1.size() < s2.size(); } bool ...
- 【C#高级编程(学习与理解)】1.1 C#与.NET的关系
1.C#语言不能孤立使用,而必须和.NET Framework一起考虑.C#编译器专门用于.NET,这表示用C#编写的所有代码总是在.NET Framework中运行. 2.C#就其本身而言只是一种语 ...
- 关于C#正则表达式MatchCollection类的总结,正则表达式的应用
认识MatchCollection 类 表示通过以迭代方式将正则表达式模式应用于输入字符串所找到的成功匹配的集合. 命名空间: System.Text.RegularExpressions 属性:C ...
- [DevExpress]GridControl 同步列头checkbox与列中checkbox状态
关键代码: /// <summary> /// 同步列头checkbox与列中checkbox状态 /// </summary> /// <param name=&quo ...
- Object-C - 类的定义
http://www.cnblogs.com/zhangweia/archive/2011/11/01/2231549.html 1. 文件分为.h:定义接口,及其属性,方法说明. .m :是实现类. ...
- JS中的!=、== 、!==、===的用法和区别。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 var num = 1; var str = '1'; var test = 1; t ...
- 长安铃木经销商爬取(解析xml、post提交、python中使用js代码)
1.通过火狐浏览器,查找大长安铃木官网中关于经销商的信息主要在两个网页中 http://www.changansuzuki.com/khfw/xml/pro.xml 地域信息 http://www. ...
- Oracle全表扫描
优化器在形成执行计划时需要做的一个重要选择——如何从数据库查询出需要的数据.对于SQL语句存取的任何表中的任何行,可能存在许多存取路径(存取方法),通过它们可以定位和查询出需要的数据.优化器选择其中自 ...
- Java 动态代理(转载)
JAVA的动态代理 代理模式 代理模式是常用的java设计模式,他的特征是代理类与委托类有同样的接口,代理类主要负责为委托类预处理消息.过滤消息.把消息转发给委托类,以及事后 处理消息等.代理类与 ...
- (转载)Unity3d摄像机Camera参数详解
1. Clear Flags:清除标记.决定屏幕的哪部分将被清除.一般用户使用对台摄像机来描绘不同游戏对象的情况,有3中模式选择: Skybox:天空盒.默认模式.在屏幕中的空白部分将显示当前摄像机的 ...