您的位置:首页 > 其它

Sicily1231. The Embarrassed Cryptography

2015-03-18 23:07 309 查看


1231. The Embarrassed Cryptography


Constraints

Time Limit: 2 secs, Memory Limit: 32 MB


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

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
// Problem#: 1231
// Submission#: 3294427
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/ // All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include <stdio.h>
#include <iostream>
#include <vector>
#include <string>
#include <stack>
#include <iomanip>
#include <algorithm>
#include <queue>
#include <functional>
#include <map>
#include <string.h>
using namespace std;

const int primeNum = 1000005;

bool isNotPrime[primeNum];
int prime[100000];
int pNum = 0;

void makePrime() {

    for (int i = 2; i < primeNum; i++) {
        if (!isNotPrime[i]) {
            prime[pNum++] = i;
            for (int j = 2 * i; j < primeNum; j += i) {
                isNotPrime[j] = true;
            }
        }
    }
}

bool isMod(string & key, int p) {
    int r = 0, s = key.size();
    for (int i = 0; i < s; i++) {
        r = (r * 10 + key[i] - '0') % p;
    }
    return r == 0;
}

int main() {

    std::ios::sync_with_stdio(false);

    makePrime();

    while (1) {

        string key;
        int least, i;

        cin >> key >> least;
        if (key == "0" && least == 0) break;

        for (i = 0; i < pNum && prime[i] < least; i++) {
            if (isMod(key, prime[i])) {
                cout << "BAD " << prime[i] << endl;
                break;
            }
        }
        if (prime[i] >= least) cout << "GOOD" << endl;
    }

    return 0;
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: