The Embarrassed Cryptographer--POJ 2635
2010-07-27 17:01
351 查看
1、解题思路:数论,打表素数,大数表示,大数除法。
2、注意事项:利用1000进制除法,如果用100、10000进制将会超时。
3、实现方法:(C++)
4、程序实现:(Java)
2、注意事项:利用1000进制除法,如果用100、10000进制将会超时。
3、实现方法:(C++)
#include<iostream> #include<string> #include<cmath> using namespace std; bool prime[1000010]; int ans[40]; void SetPrime() { memset(prime,1,sizeof(prime)); for(int i=2;i<=1000;i++) { for(int j=i;j*i<1000001;j++) prime[i*j]=false; } } int Translate(char ch[]) { int len=strlen(ch); int tmp,k; if(len%3==0) tmp=len/3; else tmp=len/3+1; k=tmp; for(int i=len-1;i>=0;i-=3) { k--; ans[k]=0; int t=0; for(int j=i;j>=i-2&&j>=0;j--) { ans[k]+=(ch[j]-'0')*(int)pow((double)10,t++); } } return tmp; } int Div(int prm,int len) { int tmp=0; int pre=ans[0]; for(int i=0;i<len;i++) { tmp=pre%prm; pre=tmp*1000+ans[i+1]; } return tmp; } int main() { int L; char K[110]; SetPrime(); while(cin>>K>>L) { if(K[0]=='0'&&L==0) break; int tmp=Translate(K); int j=2; while(1) { if(j>=L) { cout<<"GOOD"<<endl; break; } else { if(prime[j]) { if(Div(j,tmp)==0) { cout<<"BAD "<<j<<endl; break; } } } j++; } } return 0; }
4、程序实现:(Java)
import java.math.*; import java.util.*; public class Main { public static void main(String args[]) { Solve slv = new Solve(); slv.Do(); } } class Solve { final int N = 1000000; boolean[] tag=new boolean ; int[] prime=new int[N/2]; String n; int ans,l,cnt; Scanner cin = new Scanner(System.in); void Init() { int i,j; for(i=2;i<N;i++) tag[i]=false; for(i=2;i<N/2;i++) { for(j=2;i*j<N;j++) { tag[i*j]=true; } } cnt=0; for(i=2;i<N;i++) { if(tag[i]==false) { cnt++; prime[cnt]=i; } } } boolean Check() { BigInteger bint = new BigInteger(n); int t; for(t=1;t<=cnt && prime[t]<l;t++) { if(bint.remainder(BigInteger.valueOf(prime[t])).intValue()==0) { ans=prime[t]; return false; } } return true; } void Do() { Init(); while(true) { n = cin.next(); l = cin.nextInt(); if(l==0) break; if(Check()==false) System.out.println("BAD "+ans); else System.out.println("GOOD"); } } }
相关文章推荐
- poj 2635 The Embarrassed Cryptographer
- POJ 2635 The Embarrassed Cryptographer(素数筛+高精度求模+同余模)
- POJ 2635 The Embarrassed Cryptographer [高精度求余+同余模定理]【数论】
- POJ 2635 The Embarrassed Cryptographer 高精度
- POJ 2635 The Embarrassed Cryptographer
- poj 2635 The Embarrassed Cryptographer
- POJ 2635-The Embarrassed Cryptographer(高精度求模+同余模定理)
- poj2635 The Embarrassed Cryptographer
- POJ2635 The Embarrassed Cryptographer 简单数论
- POJ 2635 The Embarrassed Cryptographer
- E - The Embarrassed Cryptographer POJ - 2635
- POJ 2635 The Embarrassed Cryptographer
- poj2635 The Embarrassed Cryptographer
- poj 2635 The Embarrassed Cryptographer(同余模运算)
- POJ-2635 The Embarrassed Cryptographer 大数取模
- poj2635 The Embarrassed Cryptographer
- POJ 2635 The Embarrassed Cryptographer尴尬的译解密码者
- poj 2635 The Embarrassed Cryptographer (素数筛选优化+同余定理)
- POJ 2635 The Embarrassed Cryptographer (大整数求模/高精度求模 + 同余模定理 +素数打表)
- poj 2635 The Embarrassed Cryptographer