[ACM] POJ 2635 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 users, which is now in use in his company. The cryptographic keys arecreated 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
set is terminated by a case where K = 0 and L = 0.
Output
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
解题思路:
给定两个数。一个数S是由两个素数 a,b相乘得到的数。还有一个数L随便,问 min(a,b) 是否小于L。
解题思路:素数打表,然后从前往后遍历看S能否被当前素数整除。假设整除,推断是否小于L。
注意:给定的S是大数。仅仅能用字符数组保存。
一个数a推断能不能整除b,仅仅要推断 a%b是否等于0就能够了。
同余定理:
(a+b)%c=(a%c+b%c)%c;
(a*b)%c=(a%c+b%c)%c;
对大数取余
模板: 大数字符串形式 a[1000];
char a[1000];
int m=0;
for(int i=0;a[i]!='\0';i++)
m=((m*10)%n+(a[i]-'0')%n)%n;//也能够写成 m=(m*10+a[i]-'0')%n
m为所求的余数
本题大数求余的方法为: 把字符串从后面起 三位三位的保存在一个int类型数组中,比方 12345 存在int类型数组里面为 12 345 然后依照前面的方法求:
bool mod(int n)
{
int m=0;//气死我了!! ! !!! for(int i=0;i<=k-1;i++)
m=(m*1000+num[i])%n;
if(m==0)
return true;
return false;
}
做题时出了个BUG弄得头昏脑胀。半天后才发现原来int m=0; 一開始没有赋值为0.......哭死...
代码:
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
const int maxn=1000005;
bool isprime[maxn+1];
int prime[maxn+1];
int primelen=0;//素数表的长度
int slen;//输入大数的长度
char s[10000];int l;
int num[600];int k;//转换后的长度 void sieve(int n)//筛法求素数
{
for(int i=0;i<=n;i++)
isprime[i]=1;
isprime[0]=isprime[1]=0;
for(int i=2;i<=n;i++)
{
if(isprime[i])
{
prime[primelen++]=i;
for(int j=2*i;j<=n;j+=i)
isprime[j]=0;
}
}
} void change()//将字符串每三位(从后数)放在int数组中,比方 12345 , 12 345
{
k=0;
int a=slen/3;
int b=slen%3;
int temp=0;
if(b)//不能被3整除
{
for(int i=0;i<b;i++)
temp=temp*10+s[i]-'0';
num[k++]=temp;
}
int cnt=0;
for(int i=1;i<=a;i++)
{
temp=0;
int start=b+3*cnt;
for(int j=0;j<3;j++)
temp=temp*10+s[start+j]-'0';
num[k++]=temp;
cnt++;
}
} bool mod(int n)
{
int m=0;//气死我了。!。!!! for(int i=0;i<=k-1;i++)
m=(m*1000+num[i])%n;
if(m==0)
return true;
return false;
} int main()
{
sieve(maxn);
while(cin>>s>>l&&(s[0]!='0'||l!=0))
{
slen=strlen(s);
change();
bool ok=1;
int ans;
for(int i=0;i<primelen;i++)
{
if(mod(prime[i])&&prime[i]<l)
{
ok=0;
ans=prime[i];
break;
}
if(prime[i]>=l)
break;
}
if(ok)
cout<<"GOOD"<<endl;
else
cout<<"BAD "<<ans<<endl;
}
return 0;
}
[ACM] POJ 2635 The Embarrassed Cryptographer (同余定理,素数打表)的更多相关文章
- POJ 2635 The Embarrassed Cryptographer (千进制,素数筛,同余定理)
The Embarrassed Cryptographer Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 15767 A ...
- POJ 2635 The Embarrassed Cryptographer
大数取MOD... The Embarrassed Cryptographer Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 1 ...
- POJ 2635 The Embarrassed Cryptographer(大数求余)
题意:给出一个大数,这个大数由两个素数相乘得到,让我们判断是否其中一个素数比L要小,如果两个都小,输出较小的那个. 分析:大数求余的方法:针对题目中的样例,143 11,我们可以这样算,1 % 11 ...
- poj 2635 The Embarrassed Cryptographer(数论)
题目:http://poj.org/problem?id=2635 高精度求模 同余模定理. 题意: 给定一个大数K,K是两个大素数的乘积的值.再给定一个int内的数L 问这两个大素数中最小的一个是 ...
- POJ 2635 The Embarrassed Cryptographer 大数模
题目: http://poj.org/problem?id=2635 利用同余模定理大数拆分取模,但是耗时,需要转化为高进制,这样位数少,循环少,这里转化为1000进制的,如果转化为10000进制,需 ...
- POJ 2635 The Embarrassed Cryptographer 高精度
题目地址: http://poj.org/problem?id=2635 题意:给出一个n和L,一直n一定可以分解成两个素数相乘. 让你判断,如果这两个素数都大于等于L,则输出GOOD,否则输出最小的 ...
- POJ - 2635 The Embarrassed Cryptographer(千进制+同余模)
http://poj.org/problem?id=2635 题意 给一个大数K,K一定为两个素数的乘积.现给出一个L,若K的两个因子有小于L的,就输出BAD,并输出较小的因子.否则输出GOOD 分析 ...
- POJ 1465 Multiple (BFS,同余定理)
id=1465">http://poj.org/problem?id=1465 Multiple Time Limit: 1000MS Memory Limit: 32768K T ...
- POJ 1595 Prime Cuts (ZOJ 1312) 素数打表
ZOJ:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=312 POJ:http://poj.org/problem?id=159 ...
随机推荐
- Webpack 入门教程
Webpack 是一个前端资源加载/打包工具.它将根据模块的依赖关系进行静态分析,然后将这些模块按照指定的规则生成对应的静态资源. 本章节基于 Webpack3.0 测试通过. 从图中我们可以看出,W ...
- javascript获取年月日
javascript获取年月日代码片段 function getNowDate() { var date = new Date(); var split = "-"; var ye ...
- FreeRTOS 移植到WIN10
背景 标题表述的不准确,大意是移植到WIN10的PC机,Intel I5. 最近因为项目接触了FreeRTOS 实时操作系统,想对这个操作系统有一个更深入的了解,所以决定下载源码看看,下面这个链接的随 ...
- 微信小程序的网络设置,及网络请求:wx.request(OBJECT)
Md2All 一个Markdown在线转换工具 网址:http://md.aclickall.com 微信公众号:颜家大少本文所用排版工具:http://md.aclickall.com 支持通用的M ...
- Animation-list,帧动画+属性动画,做出Flash般的效果
我们会用到PS,即使不会也不要怂,只需要几步傻瓜式操作即可. 属性动画可以看看我另一篇文章:属性动画详解 效果图 相信机智的各位,看完之后一定能发挥创意,做出更酷更炫的效果 图层获取 首先你需要找一张 ...
- ASP.NET没有魔法——ASP.NET Identity 的“多重”身份验证
ASP.NET Identity除了提供基于Cookie的身份验证外,还提供了一些高级功能,如多次输入错误账户信息后会锁定用户禁止登录.集成第三方验证.账户的二次验证等,并且ASP.NET MVC的默 ...
- 多服务器操作利器 - Polysh
多台服务器下的痛苦人生 分布式架构下的系统,可以说每个服务都是分别部署在多台服务器上的,有的甚至还需要多机房,在这种架构下可以说可以很好的做到了易扩展.容灾等功能.推荐的服务部署为一服务多机器.一机器 ...
- linux环境变量的配置
1.全局环境变量 vi /etc/profile JAVA_HOME=/opt/modules/jdk1.8.0_11/ export PATH=$PATH:$JAVA_HOME/bin:$JAV ...
- redis配置文件之复制
主从复制使用slaveof将Redis实例作为另一个Redis服务器的副本. 1) Redis复制是异步的,master可以配置成如果它连接的slave没有达到给定的数量,就停止接受写入.2) 如果断 ...
- MYSQL瓶颈
一般来说, 在使用的时候 性能从某个数值开始.突然大大下降,说明就到了瓶颈期. mysql 瓶颈有2种,一种是 cpu瓶颈 一种是 io瓶颈.cpu瓶颈多是由io引起. 而io可以通过 show ...