POJ 2635 The Embarrassed Cryptographer (千进制,素数筛,同余定理)
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 15767 | Accepted: 4337 |
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
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
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
这个题最大的亮点就是利用千进制,100位只能这样。
很有意思的推论,利用同余定理,记住原理吧,这个规律挺神奇的,所以数学还挺好玩的
同余公式也有许多我们常见的定律,比如相等律,结合律,交换律,传递律….如下面的表示:
• 1)a≡a(modd)
• 2)a≡b(modd)→b≡a(mod d)
• 3)(a≡b(modd),b≡c(mod d))→a≡c(mod d)
• 如果a≡x(modd),b≡m(mod d),则
• 4)a+b≡x+m (mod d)
• 5)a-b≡x-m(mod d)
• 6)a*b≡x*m(mod d )
•
应用:
• (a+b)%c=(a%c+b%c)%c;
• (a*b)%c=(a%c*b%c)%c;
• 对于大数的求余,联想到进制转换时的方法,得到
• 举例如下,设大数 m=1234,模n
• 就等于((((1*10)%n+2%n)%n*10%n+3%n)%n*10%n+4%n)%n
大数求余的简单模板:
• #include<stdio.h>//大数求余,其中n(除数)不是大数
char a[1000];
int main()
int i,j,k,m,n;
{
while(scanf("%s%d",a,&n)!=EOF)
{
m=0;
for(i=0;a[i]!='\0';i++)
m=((m*10)%n+(a[i]-'0')%n)%n;
printf("%d\n",m);
}
return 0;
}
同时我是真的手残啊。。。小bug太多了。。真的是在写bug
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include<iostream>
#include <cmath>
#include<string>
#define ll long long
#define dscan(a) scanf("%d",&a)
#define mem(a,b) memset(a,b,sizeof a)
using namespace std;
#define MAXL 1105
#define Endl endl
#define maxn 1000055
inline ll read()
{
ll x=,f=;char ch=getchar();
while(ch<''||ch>'') {if(ch=='-') f=-;ch=getchar();}
while(ch>=''&&ch<='') {x=*x+ch-'';ch=getchar();}
return x*f;
}
int isp[maxn],p[maxn],cnt;
void getp()
{ int cnt=;
for(int i=;i<=maxn;++i)
{
if(!isp[i]) p[cnt++]=i;
for(int j=;j<cnt&&p[j]*i<=maxn;++j){
isp[i*p[j]]=p[j];
if(i%p[j]==) break;
}
}
}
int num,nums,ks;
int main()
{
string s;
int n;
getp();
//for(int i=0;i<=10;++i) cout<<p[i]<<" ";
while(cin>>s>>n&&(s[]!=''&&n!=))
{
//cout<<s<<endl;
int len=s.length();
int i;
int flag=;
for(i=;p[i]<n;++i)
{
num=;
for(int j=;j<len;j+=)
{
nums=;
ks=;
for(int k=j;k<j+&&k<len;k++)
{
ks*=;
nums=nums*+s[k]-'';
}
num=num*ks+nums;
//cout<<"num="<<num<<endl;
num%=p[i];
//cout<<"num="<<num<<endl;
}
if(num==) {printf("BAD %d\n",p[i]);flag=;break;}
}
if(flag) printf("GOOD\n");
}
return ;
}
POJ 2635 The Embarrassed Cryptographer (千进制,素数筛,同余定理)的更多相关文章
- POJ - 2635 The Embarrassed Cryptographer(千进制+同余模)
http://poj.org/problem?id=2635 题意 给一个大数K,K一定为两个素数的乘积.现给出一个L,若K的两个因子有小于L的,就输出BAD,并输出较小的因子.否则输出GOOD 分析 ...
- 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
大数取MOD... The Embarrassed Cryptographer Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 1 ...
- POJ 2635 The Embarrassed Cryptographer(大数求余)
题意:给出一个大数,这个大数由两个素数相乘得到,让我们判断是否其中一个素数比L要小,如果两个都小,输出较小的那个. 分析:大数求余的方法:针对题目中的样例,143 11,我们可以这样算,1 % 11 ...
- [ACM] POJ 2635 The Embarrassed Cryptographer (同余定理,素数打表)
The Embarrassed Cryptographer Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 11978 A ...
- POJ 2635 The Embarrassed Cryptographer 高精度
题目地址: http://poj.org/problem?id=2635 题意:给出一个n和L,一直n一定可以分解成两个素数相乘. 让你判断,如果这两个素数都大于等于L,则输出GOOD,否则输出最小的 ...
- poj 2635 千进制
转自:http://www.cnblogs.com/kuangbin/archive/2012/04/01/2429463.html 大致题意: 给定一个大数K,K是两个大素数的乘积的值. 再给定一个 ...
- 【阔别许久的博】【我要开始攻数学和几何啦】【高精度取模+同余模定理,*】POJ 2365 The Embarrassed Cryptographer
题意:给出一大数K(4 <= K <= 10^100)与一整数L(2 <= L <= 106),K为两个素数的乘积(The cryptographic keys are cre ...
随机推荐
- sigqueue与kill详解及实例
/*********************************************************************************************** 相关函 ...
- python之2.x与3.x区别(仅限于基础)
因为看的是python2.x的书籍.用的是python 3.7.所以先把两者的区别记录一下,仅限于基础. 1.input python3.0之后,不区分input()和raw_input(),统一为i ...
- thinkphp3.2.3如何只改变地址url中的某一个分隔符,其它保持不变
今天教大家一个关于使用thinkphp3.2.3改变只改变地址url中的某一个分隔符的方法,首先大家来看看这个地址! 它的原始地址应该是/Home/Index/index/page/2.html,那我 ...
- [Bzoj2588]Count on a tree(主席树+LCA)
Description 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第K小的点权.其中lastans是上一个询问的答案,初始 ...
- Linux命令之---nl
命令简介 nl命令在linux系统中用来计算文件中行号.nl 可以将输出的文件内容自动的加上行号!其默认的结果与 cat -n 有点不太一样, nl 可以将行号做比较多的显示设计,包括位数与是否自动补 ...
- Codeforces 653G Move by Prime 组合数学
题意: 有一个长度为\(n\)的正整数序列\(a\),有这样一种操作: 每次可以选序列中的某一个数乘上或除以某一个素数. 求对于每一个子序列使其所有元素相等的最少操作次数之和. 分析: 因为两个素数之 ...
- Redis实现之字符串
简单动态字符串 Redis中的字符串并不是传统的C语言字符串(即字符数组,以下简称C字符串),而是自己构建了一种简单动态字符串(simple dynamic string,SDS),并将SDS作为Re ...
- 精简Docker镜像的五种通用方法
http://dockone.io/article/8163 精简Docker镜像的好处很多,不仅可以节省存储空间和带宽,还能减少安全隐患.优化镜像大小的手段多种多样,因服务所使用的基础开发语言不同而 ...
- 排查和处理一台被攻击的linux系统及其事后分析
11:40 2018/3/16 发现最近几天服务器流量异常的大,检查了系统命令发现命令最近的修改时间很近,检查dns配置也发现了异常的dns服务器地址. 考虑到事态的严重性,铲掉这个系统重新搭建. 事 ...
- linux环境搭建系列之tomcat安装步骤
前提: Linux centOS 64位 JDK 1.7 安装包从官网上下载 安装Tomcat之前要先安装JDK. 我的JDK是1.7版本的,所以Tomcat版本也选了7的 1.新建目录tomcat ...