poj 2635 素数打表+同余求模定理+千进制
来源:互联网 发布:交通大数据的优缺点 编辑:程序博客网 时间:2024/06/04 18:05
The Embarrassed Cryptographer
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 13831 Accepted: 3762
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.
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 10143 20667 20667 302573 302573 400 0
Sample Output
GOODBAD 11GOODBAD 23GOODBAD 31
大致题意:
给定一个大数K,K是两个大素数的乘积的值。
再给定一个int内的数L
问这两个大素数中最小的一个是否小于L,如果小于则输出这个素数。
千进制:
把数字往大进制转换能够加快运算效率。若用十进制则耗费很多时间,会TLE。
千进制的性质与十进制相似。
例如,把K=1234567890转成千进制,就变成了:Kt=[ 1][234][567][890]。
为了方便处理,我的程序是按“局部有序,全局倒序”模式存放Kt
即Kt=[890][567][234][1 ] (一个中括号代表一个数组元素)
注意:
这里为什么要局部有序,全局倒序呢???
因为每一次都要乘以一个1000 要是不这样:
那么2734 这个数字就会 273 * 1000 +4 这样是不对的,只能是 2*1000 + 734
同余求模定理:
同余定理具有可加、可减、可乘、乘方性:
(A + B) % C = (A % C + B % C) % C;
(A - B) % C = (A % C - B % C) % C;
(A * B) % C = (A % C * B % C) % C;
(A ^ B) % C = (A^N + B^N) % C;
例如m==1234时看,取模n:
((((1*10)%n+2%n)%n*10%n+3%n)%n*10%n+4%10)%n
要是大整数的话,可以用千进制或者万进制或者更大,只需要把 *10 编程 *1000或者*10000诸如此类的
模板
AC代码:
#include<stdio.h>#include<algorithm>#include<string.h>using namespace std;const int maxx=1000100;int isprimer[maxx+1];char str[10000];int L,a[10000],len;//-------------------------------------//void primer(){ int p=0; isprimer[p++]=2; for(int i=3;i<=maxx;i+=2){//奇偶法 bool flag=true; for(int j=0;isprimer[j]*isprimer[j]<=i;j++){ if(i%isprimer[j]==0){//根号法,递归法 flag=false; break; } } if(flag) isprimer[p++]=i; }}//-------------------------------------//void translate(){ memset(a,0,sizeof(a)); len=strlen(str); for(int i=0;i<len;i++){ int pos=(len+2-i)/3-1;//因为是从a【0】开始存 a[pos]=a[pos]*10+(str[i]-'0');//不用考虑边界问题 } len=(len+2)/3;//注意这里的妙用}//-------------------------------------//bool mod(int num,int len){ int ans=0; for(int i=len-1;i>=0;i--) ans=(ans*1000+a[i])%num; if(ans==0) return false; return true;}//-------------------------------------//int main(){ primer(); while(scanf("%s%d",str,&L)) { if(strcmp(str,"0")==0&&L==0) return 0; translate(); bool flag=true; int p=0; while(isprimer[p]<L) { if(!mod(isprimer[p],len)){ flag=false; printf("BAD %d\n",isprimer[p]); break; } p++; } if(flag) printf("GOOD\n"); } return 0;}
0 0
- poj 2635 素数打表+同余求模定理+千进制
- [ACM] POJ 2635 The Embarrassed Cryptographer (同余定理,素数打表)
- poj 2635(筛素数+同模余定理+缩位)
- poj 2635 The Embarrassed Cryptographer(素数筛 + 同余模定理)
- 素数打表及数学定理
- (POJ2635)The Embarrassed Cryptographer <高精度求模(千进制表示) + 同余模定理 + 素数打表>
- POJ 2739 素数打表
- poj 2635 同余模定理
- POJ 2635(同余定理)
- POJ 2689 素数打表再打表
- POJ 3518 简单素数打表
- POJ 2739 素数打表-----水题
- poj 2379 连续素数和 打表
- poj 3126(素数打表)
- poj 3126 (BFS + 素数打表)
- POJ 3126 bfs+素数打表
- poj2635 同余定理 + 素数筛法
- The Embarrassed Cryptographer POJ 2635 数论之高精度求模(同余模定理+千进制)
- 个性水彩人像效果
- android中java中的开方
- **AJAX** ajax的使用
- 计算机科学导论学习笔记
- const详谈
- poj 2635 素数打表+同余求模定理+千进制
- 游戏资源的制作和下载
- C#枚举和结构体
- Volley 源码解析
- Chromium Embedded Framework 中文文档(简介)
- MVP架构实现的Github客户端(2-搭建项目框架)
- 找出现奇数次的两个数
- CSDN博客常见问题
- linux安装yum源