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 ...
随机推荐
- 浅谈css中的position属性
我觉得吧,css如果不考虑浏览器的兼容问题的话,最让人头疼的应该就是position了,反正我是这么觉得的,为了能基本上搞清楚position的几种情况,我找了一些资料,做了一个小实验,下面是实验的过 ...
- UML静态类图
0,主要分为类.接口.协作.关系,这四种元素.作用:a,显示类.接口以及他们之间的静态结构和关系:b,用于描述系统的结构化设计. 1,类 CStudent +m_strName : string +S ...
- java进阶一之jdk8新特性
1.官方发布的jdk8新特性 2.51CTO相关专题
- bzoj 3626 LCA
这一道题咋一看只觉得是离线,可以求出所有的f(1,i,z), 答案就等于f(1,r,z)-f(1,l-1,z).但是没有具体的做法,但是求LCA的深度和有一个非常巧妙的做法,每加一个点,就把这个点到根 ...
- Poj 3030 Nasty Hacks
1.Link: http://poj.org/problem?id=3030 2.Content: Nasty Hacks Time Limit: 1000MS Memory Limit: 655 ...
- C/C++易错小记录
一:对字符串的 strlen() 与 sizeof #include <string.h> #include <stdio.h> int main(void) { char* ...
- shell脚本编程之for语句、if语句使用介绍
介绍了shell脚本编程之for语句.if语句的使用方法. 上部: 面向过程: 顺序执行 选择执行: if, case 循环执行: for, while, until 一.for语句 格式: ...
- Spark Streaming揭秘 Day34 解析UI监听模式
Spark Streaming揭秘 Day34 解析UI监听模式 今天分享下SparkStreaming中的UI部分,和所有的UI系统一样,SparkStreaming中的UI系统使用的是监听器模式. ...
- Gazebo Ros入门
教程代码 First step with gazebo and ros • setup a ROS workspace • create projects for your simulated rob ...
- 扩展ServiceHost<T>类
public class ServiceHost<T> : ServiceHost { public void EnableMetadataExchange(bool enableHttp ...