Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 13831 | Accepted: 3762 |
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
大致题意:
给定一个大数K,K是两个大素数的乘积的值。
再给定一个int内的数L
问这两个大素数中最小的一个是否小于L,如果小于则输出这个素数。
千进制:
把数字往大进制转换能够加快运算效率。若用十进制则耗费很多时间,会TLE。
千进制的性质与十进制相似。
例如,把K=1234567890转成千进制,就变成了:Kt=[ 1][234][567][890]。
为了方便处理,我的程序是按“局部有序,全局倒序”模式存放Kt
即Kt=[890][567][234][1 ] (一个中括号代表一个数组元素)
注意:
这里为什么要局部有序,全局倒序呢???
因为每一次都要乘以一个1000 要是不这样:
那么2734 这个数字就会 273 * 1000 +4 这样是不对的,只能是 2*1000 + 734
同余求模定理:
同余定理具有可加、可减、可乘、乘方性:
(A + B) % C = (A % C + B % C) % C;
(A - B) % C = (A % C - B % C) % C;
(A * B) % C = (A % C * B % C) % C;
(A ^ B) % C = (A^N + B^N) % C;
例如m==1234时看,取模n:
((((1*10)%n+2%n)%n*10%n+3%n)%n*10%n+4%10)%n
要是大整数的话,可以用千进制或者万进制或者更大,只需要把 *10 编程 *1000或者*10000诸如此类的
模板
AC代码:
#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
const int maxx=1000100;
int isprimer[maxx+1];
char str[10000];
int L,a[10000],len;
//-------------------------------------//
void primer()
{
int p=0;
isprimer[p++]=2;
for(int i=3;i<=maxx;i+=2){//奇偶法
bool flag=true;
for(int j=0;isprimer[j]*isprimer[j]<=i;j++){
if(i%isprimer[j]==0){//根号法,递归法
flag=false;
break;
}
}
if(flag)
isprimer[p++]=i;
}
}
//-------------------------------------//
void translate()
{
memset(a,0,sizeof(a));
len=strlen(str);
for(int i=0;i<len;i++){
int pos=(len+2-i)/3-1;//因为是从a【0】开始存
a[pos]=a[pos]*10+(str[i]-'0');//不用考虑边界问题
}
len=(len+2)/3;//注意这里的妙用
}
//-------------------------------------//
bool mod(int num,int len)
{
int ans=0;
for(int i=len-1;i>=0;i--)
ans=(ans*1000+a[i])%num;
if(ans==0)
return false;
return true;
}
//-------------------------------------//
int main()
{
primer();
while(scanf("%s%d",str,&L))
{
if(strcmp(str,"0")==0&&L==0)
return 0;
translate();
bool flag=true;
int p=0;
while(isprimer[p]<L)
{
if(!mod(isprimer[p],len)){
flag=false;
printf("BAD %d\n",isprimer[p]);
break;
}
p++;
}
if(flag)
printf("GOOD\n");
}
return 0;
}