首页 > 专题系列 > Java解POJ > POJ 2635 The Embarrassed Cryptographer [解题报告] Java
2013
11-11

POJ 2635 The Embarrassed Cryptographer [解题报告] Java

The Embarrassed Cryptographer

问题描述 :

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.

输入:

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.

输出:

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.

样例输入:

143 10
143 20
667 20
667 30
2573 30
2573 40
0 0

样例输出:

GOOD
BAD 11
GOOD
BAD 23
GOOD
BAD 31

解题代码:

//* @author 
import java.io.*;
import java.util.*;
import java.math.*;
public class Main
{
    static boolean h[];
    public static void main(String[] args)
    {
        int m,i,j=2;
        h=new boolean[1000005];
        BigInteger n,p,q,nn;
        for(i=1;i<=1000001;i++)
            h[i]=true;
        for(i=2;i<=1000001;i+=2)
            h[i]=false;
        h[1]=false;h[2]=true;    
        for(i=3;i<=1000;i+=2)
        {
          if(h[i]==true)
          {
              j=2;
              while(i*j<=1000000)
              {
                  h[i*j]=false;
                  j++;
              }
          }
        }
        Scanner cin=new Scanner(System.in);
        while(cin.hasNext())
        {
            j=1000100;
            n=cin.nextBigInteger();
            m=cin.nextInt();
            nn=BigInteger.ZERO;
            if(n.compareTo(nn)==0&&m==0)
                break;
          for(i=2;i<=1000000;i++)
          {
          if(h[i]==true)
          {
            p=n.divide(BigInteger.valueOf(i));
            q=p.multiply(BigInteger.valueOf(i));
            if(q.compareTo(n)==0)
            {
                j=i;
                break;
            }
          }
         }
            if(j< m)
                System.out.println("BAD "+j);
            else
                System.out.println("GOOD");
        }
    }
}

  1. 一开始就规定不相邻节点颜色相同,可能得不到最优解。我想个类似的算法,也不确定是否总能得到最优解:先着一个点,随机挑一个相邻点,着第二色,继续随机选一个点,但必须至少有一个边和已着点相邻,着上不同色,当然尽量不增加新色,直到完成。我还找不到反例验证他的错误。。希望LZ也帮想想, 有想法欢迎来邮件。谢谢

  2. Thanks for taking the time to examine this, I really feel strongly about it and love studying a lot more on this topic. If possible, as you acquire experience

  3. #include <cstdio>
    #include <cstring>

    const int MAXSIZE=256;
    //char store[MAXSIZE];
    char str1[MAXSIZE];
    /*
    void init(char *store) {
    int i;
    store['A']=’V', store['B']=’W',store['C']=’X',store['D']=’Y',store['E']=’Z';
    for(i=’F';i<=’Z';++i) store =i-5;
    }
    */
    int main() {
    //freopen("input.txt","r",stdin);
    //init(store);
    char *p;
    while(fgets(str1,MAXSIZE,stdin) && strcmp(str1,"STARTn")==0) {
    if(p=fgets(str1,MAXSIZE,stdin)) {
    for(;*p;++p) {
    //*p=store[*p]
    if(*p<’A’ || *p>’Z') continue;
    if(*p>’E') *p=*p-5;
    else *p=*p+21;
    }
    printf("%s",str1);
    }
    fgets(str1,MAXSIZE,stdin);
    }
    return 0;
    }