Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 14151 | Accepted: 3868 |
Description
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
Output
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
題意:給出一個大數K,K是兩個素數的乘積
再給出一個小於等於100W的L
問K的兩個素數因子較小的那一個是否小於L
小於輸出這個數,否則輸出GOOD (如果在100W內沒有找到因子,也輸出GOOD)
分析:首先我們先把100W的素數求出來(打表素數篩)
然後根據同餘模定理,比如 123對3取餘
就是1%3=1
(1*10+2)%3=0
(0*10+3)%3=0
最後的結果就是0
這裏需要把K轉爲千進制的,因爲十進制的一位一位的取餘會超時
假設 K = 1234567891,轉成千進制的就是 [ 1 ] [ 123 ] [ 567 ] [ 891 ] ,用個數組存每一位就行
這樣把上面的乘10改成乘1000就可以了
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
using namespace std;
const int Max=1100000;
int pre[Max+10],pr[Max+10],top;
void paly_table() //素數篩
{
int i,j,t;
memset(pre,0,sizeof(pre));
pre[2]=1;
for(i=3;i<=Max;i++)
{
if(i&1)
pre[i]=1;
}
t=sqrt(Max);
for(i=0;i<=t;i++)
{
if(pre[i])
{
for(j=i*i;j<=Max;j+=2*i)
{
pre[j]=0;
}
}
}
top=0;
for(i=0;i<=1000010;i++)
{
if(pre[i])
{
pr[top++]=i;
}
}
// for(i=0;i<10;i++)
// {
// printf("%d ",pr[top-1]);
// }
}
int main()
{
char s[200];
int a[200],l,mod,n,m,i,j;
paly_table();
while(~scanf("%s",s))
{
scanf("%d",&mod);
if(s[0]=='0' && mod==0)
break;
l=strlen(s);
n=0;
memset(a,0,sizeof(a));
m=l%3;
if(m==1) //轉千進制,用的方法好笨
{
a[0]=s[0]-'0';
n++;
}
else if(m==2)
{
a[0]=(s[0]-'0')*10+(s[1]-'0');
n++;
}
for(i=m;i<l;)
{
int ct=0;
while(ct<3)
{
a[n]=a[n]*10+(s[i]-'0');
i++;ct++;
}
n++;
}
// for(i=0;i<n;i++)
// {
// printf("%d ",a[i]);
// }
// printf("\n");
int k=0;
int flag=0;
for(i=0;i<top;i++)
{
k=0;
for(j=0;j<=n-1;j++)
{
k=(k*1000+a[j])%pr[i]; //取餘
//printf("%d\n",k);
}
if(k==0)
{
flag=1;
m=pr[i];
break;
}
}
if(flag && m<mod) //能找到因子,且比給的數小
{
printf("BAD %d\n",m);
}
else
{
printf("GOOD\n");
}
}
return 0;
}