poj2635The Embarrassed Cryptographer(数论)(解题报告)
2012-09-17 18:05
477 查看
http://poj.org/problem?id=2635
题意:给出k和L,,要求K是由两个素因子组成,且K是由两个素因子乘积形成,要求其中一个素因子小于L
思路:高精度取模+同余定理。
1.构造素数表格,这里可用奇偶法,和线性素数筛选法
2,由于给出K非常大,因此可将K化为千进制存到一组数组中,以提高高精度运算的效率
小结:对于素数的筛选和构造中,可用三种方法:
1,用时最短的是线性筛选法,但耗空间;
2.奇偶法筛选选素数:省空间,但与线性筛选法相比,比较耗时。
3.暴力枚举,超时!!!
Language: The Embarrassed Cryptographer
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 Source Nordic 2005 |
思路:高精度取模+同余定理。
1.构造素数表格,这里可用奇偶法,和线性素数筛选法
2,由于给出K非常大,因此可将K化为千进制存到一组数组中,以提高高精度运算的效率
//Memory 480K Time1266MS length1207B #include<stdio.h> #include<stdlib.h> #include<string.h> #include<iostream> using namespace std; const int maxn=1001000; int len,t2,total; int prime[maxn+1]; int k[40]; char ch[105]; void makeprime()//构造素数表 { total=0; int flag; prime[total++]=2; for(int i=3;i<=maxn;i+=2) {flag=1; for(int j=0;prime[j]*prime[j]<=i;j++) {if(i%prime[j]==0) { flag=0; break; } } if(flag) prime[total++]=i; } }
void change()//把字符串转化为千进制 { int t1=len%3; t2=len/3; if(t1==0) t2--; int j=t2; memset(k,0,sizeof(k)); for(int i=len-1;i>=0;i-=3) if(j>=0) {for(int t=(i-2>0? i-2:0);t<=i;t++) k[j]=k[j]*10+ch[t]-'0'; j--; } } int Mod(int d)//大数取模运算 { int ans=0; for(int i=0;i<=t2;i++) ans=(ans*1000+k[i])%d; return ans; } int main() { makeprime(); int L; while(~scanf("%s%d",ch,&L)&&L) { len=strlen(ch); int ok=1; change(); for(int i=0;prime[i]<L&&i<total;i++) if(Mod(prime[i])==0) { printf("BAD %d\n",prime[i]); ok=0; break; } if(ok) printf("GOOD\n"); } system("pause"); return 0; }
小结:对于素数的筛选和构造中,可用三种方法:
1,用时最短的是线性筛选法,但耗空间;
2.奇偶法筛选选素数:省空间,但与线性筛选法相比,比较耗时。
3.暴力枚举,超时!!!
相关文章推荐
- 蓝桥杯 历届试题 买不到的数目 解题报告(完全背包,数论)
- Codeforces Round #146 (Div. 1) LCM Challenge 解题报告 数论 找规律
- 解题报告:HDU_3944 DP? 数论
- [数论]中南大学 2013 校赛 G 题解题报告
- 一中OJ #1176 立方数分解[尼科梅彻斯定理] | 数论 + 枚举 | 解题报告
- 数论小模版解题报告
- 【数论】MMT数解题报告
- POJ-1730 Perfect Pth Powers 解题报告(数论) 最大开方数
- OPJ-1067 取石子游戏 解题报告(数论) 取石子游戏,betty定理
- codeforces 158D 解题报告 数论+暴力
- 解题报告:N.密码破解 (数论 含推导) “盛大游戏杯”第15届上海大学程序设计联赛夏季赛
- HDU 1796 How many integers can you find 解题报告(数论)
- POJ 1006(数论:同余式) 解题报告
- 【解题报告】 NYOJ 541 最强DE 战斗力 -- 数论
- 解题报告:HDU4549 M斐波那契数列 数论三大基础(快速幂+矩阵快速幂+费马小定理)
- POJ 1006(数论:同余式) 解题报告
- Spoj数论专场解题报告
- HDU 1018 Big Number数论(解题报告)
- BZOJ 2005 [Noi 2010] 数论 解题报告
- [解题报告]最强DE战斗力-第五届程序设计大赛(数论)