E - The Embarrassed Cryptographer POJ - 2635
2017-12-04 19:59
405 查看
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 <= 10 100 and 2 <= L <= 10 6. 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
取模优化,将%的运算次数减少可提高程序的效率,另外感觉自己这个素数筛的真够猥琐的,要去学一学了。
#include <iostream> #include <cstdio> #include <string> #include <cmath> using namespace std; char str[105]; int primer[80000],l,len; bool prim[1000005]; bool isprim(int x){ int t = sqrt(x); for(int i = 2;i <= t;++i){ if(x % i == 0) return false; } return true; } void init(){ len = 0; for(int i = 2;i <= 1000;i++){ if(!prim[i]){ for(int j = i << 1;j <= 1000000;j += i){ prim[j] = true; } } } for(int i = 2;i <= 1000000;++i){ if(!prim[i]){ primer[len++] = i; } } // cout << len << endl; } bool isdiv(int x){ long long ans = 0; b1c7 for(int i = 0;str[i] != '\0';++i){ ans = ans * 10 + str[i] - '0'; if(ans >= 100000000000000000) ans = ans % x; } ans = ans % x; if(ans == 0){ return true; } else{ return false; } } int main(){ init(); while(~scanf("%s%d",str,&l)){ if(str[0] == '0' && l == 0){ break; } bool flag = false; int ans = 0; for(int i = 0;i < len;++i){ if(primer[i] >= l){ break; } //cout << primer[i] << endl; if(isdiv(primer[i])){ ans = primer[i]; flag = true; break; } } if(flag){ printf("BAD %d\n",ans); } else{ printf("GOOD\n"); } } return 0; }
相关文章推荐
- POJ 2635 The Embarrassed Cryptographer
- poj 2635 The Embarrassed Cryptographer
- POJ 2635 The Embarrassed Cryptographer
- POJ 2635 The Embarrassed Cryptographer (同余问题)
- POJ 2635 The Embarrassed Cryptographer(JAVA)
- poj2635--The Embarrassed Cryptographer(数论篇1,大数取模)
- poj2635 The Embarrassed Cryptographer
- poj2635——The Embarrassed Cryptographer(高精度取模)
- The Embarrassed Cryptographer--POJ 2635
- POJ - 2635 The Embarrassed Cryptographer
- poj 2635 The Embarrassed Cryptographer ——高精度取模
- poj 2635 The Embarrassed Cryptographer
- (大数取模)POJ2635-The Embarrassed Cryptographer
- [ACM] POJ 2635 The Embarrassed Cryptographer (同余定理,素数打表)
- poj 2635 The Embarrassed Cryptographer
- POJ -2635-The Embarrassed Cryptographer-数论
- poj 2635(The Embarrassed Cryptographer(把…译成密码) 素数打表的最优方法+10进制转换成1000进制,大数取模
- POJ 2635 The Embarrassed Cryptographer
- POJ 2635-The Embarrassed Cryptographer(高精度求模+同余模定理)
- POJ 2635 The Embarrassed Cryptographer