题目链接:点击打开链接
Description
![](https://i-blog.csdnimg.cn/blog_migrate/a64f64479d9437bf747b1ad5425cdc17.jpeg)
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
基本思路:用到同于模定理的知识,我也是现学的,接触到了个新知识点,可以访问同余模定理文库学习相关知识.然后在再优化,将10进制的转化为1000进制的,避免在循环时超时,具体怎么做上面那个链接很清楚.
<span style="font-size:18px;">#include <iostream>
#include<cstring>
#include<cstdio>
#include<cstdlib>
using namespace std;
char s[1000];
int prime[1000100];
int top;
bool vis[1000100];
int big[1000];
///筛选素数
void chooseprime()
{
memset(vis,0,sizeof(vis));
for(int i=2;i<=1000015;i++)
{
if(!vis[i])
{
prime[++top]=i;
for(int j=i+i;j<=1000015;j=j+i)
{
if(j%i==0)vis[j]=1;
}
}
}
}
int main()
{
chooseprime();
int n;
while(scanf("%s%d",s,&n))
{
if(n==0&&s[0]=='0')break;
int sum=0,ans=0;
int len=strlen(s);
int t;
int k=0;
///转化为1000进制,注意从个位开始
for(int i=len-1;i>=0;i--)
{
t=s[i]-'0';
for(int j=1;j<=ans;j++)
t=t*10;
sum=sum+t;
ans++;
if(ans==3)
{
big[++k]=sum;
sum=ans=0;
}
}
if(sum!=0)big[++k]=sum;
int f=0;
int ant;
///查找素数
for(int i=1;i<=top;i++)
{
int m=prime[i];
if(m>=n)break;///因为是从小到大寻找素数,如果小的不符合,那么
///后面的素数一定不符合
int d=0;
for(int j=k;j>=1;j--)///从最高位进行同余模定理的应用
{
d=(d*1000+big[j])%m;
}
if(d==0)
{
f=1;
ant=m;
break;
}
}
if(f)
{
cout<<"BAD "<<ant<<endl;
}
else cout<<"GOOD\n";
}
return 0;
}
</span>