Time Limit: 2sec Memory Limit:32MB
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. 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
aaarticlea/jpeg;base64,/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL/wAARCAARABIDASIAAhEBAxEB/8QAGAABAAMBAAAAAAAAAAAAAAAAAAMFBwT/xAAlEAACAQQCAQMFAAAAAAAAAAABAgMABAURBiESIjFBMjZxdbP/xAAYAQACAwAAAAAAAAAAAAAAAAAAAwEEBf/EABsRAQEAAgMBAAAAAAAAAAAAAAEAAgMEEyFh/9oADAMBAAIRAxEAPwDQeRW+SyVnctBIkiiScOk87qm0ciP0aZWA8dkEDZA2fcGPCWPI+PXkUt3GIcQjkyQxTGdtMrAhUVQO5CraVd/UB1pa7cnHmbaW5hjxEktoZJJGulnjChWYsT4lvLoHvr3B1vommvuQYaSe/jGSxrW9yXEiCWIiTe9eWohvs/LH8n5ocDh9jlnsER+zt+9wDE9G0uKWO4hSaGRJIpFDI6MCrKewQR7ilVfFPs7B/r4P5rStB8ZJW9KUqIlKUoi//9k=" alt="" /> Copy sample input to clipboard
143 10 Sample Output
GOOD |
分析:因为是 ^,所以不能直接用内置数据类型,只能用大整数
判断是否能整除的一个方法是判断模值是否为 ,因为大整数求模比除法更简单,所以直接求模
#include <iostream>
#include <string>
using namespace std; bool is_mod(string num,int n) // 判断是否能够被整除
{
int fac = ;
for(int i = ; i < num.size(); i++)
fac = (fac * + (num[i] - '')) % n;
if(fac == )
return true;
return false;
} int main(int argc, char const *argv[])
{
int prime[];
int primeNum = ;
for (int i = ; i != ; ++i) {
prime[i] = ;
}
for (int i = ; i != ; ++i) { // 筛选法求素数
if (prime[i] == ) {
prime[primeNum++] = i; // 将素数存到数组
for (int j = * i; j < ; j += i)
prime[j] = ;
}
} string num;
int lowerBound;
while (cin >> num) {
bool flag = true;
int factor = ;
cin >> lowerBound;
if (num == "" && lowerBound == )
break;
for (int i = ; i != primeNum; ++i) {
if (prime[i] < lowerBound) { // 找到最小的能被整除且在下界内部的素数
if (is_mod(num, prime[i])) {
flag = false;
factor = prime[i];
break;
}
}
}
if (flag)
cout << "GOOD" << endl;
else
cout << "BAD " << factor << endl;
}
return ;
}