POJ 2635 The Embarrassed Cryptographer 高精度取余数

时间:2021-03-19 12:18:53

K是两个素数的积,如果他们小于L,则输出bad p,p为最小的质因数。 否则输出good。

高精度模板一套,注意for (c=0,i=a[0];i;c=c*DEPTH+a[i],a[i]=c/b,c%=b,i--);中b为10^6, int32能装下10^9,那么只能压3位,囧。

素数要用筛法打表存好,不然超时。。。

 

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 <= 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

Source

 

 

#include <iostream>
#include <math.h>
#include <string.h>
using namespace std;

#define DIGIT	3
#define DEPTH	1000
#define MAX     35
typedef int bignum_t[MAX+1];
int read(bignum_t a,istream& is=cin){
	char buf[MAX*DIGIT+1],ch;
	int i,j;
	memset((void*)a,0,sizeof(bignum_t));
	if (!(is>>buf))	return 0;
	for (a[0]=strlen(buf),i=a[0]/2-1;i>=0;i--)
		ch=buf[i],buf[i]=buf[a[0]-1-i],buf[a[0]-1-i]=ch;
	for (a[0]=(a[0]+DIGIT-1)/DIGIT,j=strlen(buf);j<a[0]*DIGIT;buf[j++]='0');
	for (i=1;i<=a[0];i++)
		for (a[i]=0,j=0;j<DIGIT;j++)
			a[i]=a[i]*10+buf[i*DIGIT-1-j]-'0';
	for (;!a[a[0]]&&a[0]>1;a[0]--);
	return 1;
}

void write(const bignum_t a,ostream& os=cout){
	int i,j;
	for (os<<a[i=a[0]],i--;i;i--)
		for (j=DEPTH/10;j;j/=10)
			os<<a[i]/j%10;
}
void div(bignum_t a,const int b,int& c){
	int i;
	for (c=0,i=a[0];i;c=c*DEPTH+a[i],a[i]=c/b,c%=b,i--);
	for (;!a[a[0]]&&a[0]>1;a[0]--);
}


int prime[1000200],pi[1000000],pre_=2;
int isprime(int n){
    int tmp=sqrt(n+0.0);
    for (int i = 1; pi[i] <= tmp; i++){
        if (n%pi[i]==0) return 0;
    }
    pi[++pre_]=n;
    return 1;
}
int main(){
    bignum_t k,tmp;
    int l,sgn,yu,i;
    pi[1]=2;pi[2]=3;
    prime[2]=prime[3]=1;
    for (i=5;i<=1000000;i++) prime[i]=isprime(i);
   // for (i=1;i<100;i++) if (prime[i]) cout<<i<<endl;return 0;
    while (read(k),cin>>l,l!=0){
        memcpy(tmp,k,sizeof(k));
        for (i=2;i<l;i++){
            if (prime[i]==0) continue;
            yu=0;
            memcpy(k,tmp,sizeof(k));
            div(k,i,yu);
//            write(k);cout<<"  ||   yu=";
//            cout<<yu<<endl;
            if (yu==0) {
                cout<<"BAD "<<i<<endl;
                break;
            }
        }
        if (i==l)  cout<<"GOOD"<<endl;
    }
    return 0;
}