CF #356 div1 A. Bear and Prime 100

题目链接:http://codeforces.com/contest/679/problem/A

 

CF有史以来第一次出现交互式的题目,大致意思为选择2到100中某一个数字作为隐藏数,你可以询问最多20次问题,每一次询问一个数字x,如果预先选定的隐藏数是x的倍数,则回复"yes",否则回复"no",你只需要判那个数字是否为质数(不一定需要知道具体是多少)

 

如果一个数字是两个质数积的倍数,则这个数字一定是合数,另外需要注意,两个质数是可以相同的。即如果隐藏数是a*b的倍数(其中a和b为两个质数),则隐藏数是合数。由此,我们只需要枚举50以内的质数即可(超过50的质数乘以2也已经大于100了)。则2到50有15个质数,分别是2,3,5,7,11,13,17,19,23,29,31,37,41,43,47。

只需要判断隐藏数是否为其中至少两个数字的倍数即可,特别注意在2,3,5,7回复"yes后,需要再判断是否为4,9,25,49的倍数。

 

 1 import java.io.OutputStream;
 2 import java.io.IOException;
 3 import java.io.InputStream;
 4 import java.io.PrintWriter;
 5 import java.util.List;
 6 import java.util.StringTokenizer;
 7 import java.io.IOException;
 8 import java.io.BufferedReader;
 9 import java.io.InputStreamReader;
10 import java.util.ArrayList;
11 import java.io.InputStream;
12 
13 public class Main {
14     public static void main(String[] args) {
15         InputStream inputStream = System.in;
16         OutputStream outputStream = System.out;
17         InputReader in = new InputReader(inputStream);
18         PrintWriter out = new PrintWriter(outputStream);
19         TaskA solver = new TaskA();
20         solver.solve(1, in, out);
21         out.close();
22     }
23 
24     static class TaskA {
25         public void solve(int testNumber, InputReader in, PrintWriter out) {
26             List<Integer> primes = new ArrayList<>();
27             for (int i = 2; i <= 50; i++) {
28                 boolean isPrime = true;
29                 for (int j = 2; j * j <= i; j++) {
30                     if (i % j == 0) {
31                         isPrime = false;
32                     }
33                 }
34                 if (isPrime) {
35                     primes.add(i);
36                 }
37             }
38             int cnt = 0;
39             for (Integer prime : primes) {
40                 out.println(prime);
41                 out.flush();
42                 String reply = in.next();
43                 cnt += (reply.equals("yes") ? 1 : 0);
44                 if (prime * prime <= 100) {
45                     out.println(prime * prime);
46                     out.flush();
47                     reply = in.next();
48                     cnt += (reply.equals("yes") ? 1 : 0);
49                 }
50                 if (cnt > 1) {
51                     out.println("composite");
52                     return;
53                 }
54             }
55             out.println("prime");
56         }
57 
58     }
59 
60     static class InputReader {
61         private BufferedReader reader;
62         private StringTokenizer tokenizer;
63 
64         public InputReader(InputStream stream) {
65             reader = new BufferedReader(new InputStreamReader(stream));
66             tokenizer = null;
67         }
68 
69         public String next() {
70             while (tokenizer == null || !tokenizer.hasMoreTokens()) {
71                 try {
72                     tokenizer = new StringTokenizer(reader.readLine());
73                 } catch (IOException e) {
74                     throw new RuntimeException(e);
75                 }
76             }
77             return tokenizer.nextToken();
78         }
79 
80     }
81 }

 

posted @ 2016-06-09 19:56  活在夢裡  阅读(365)  评论(0编辑  收藏  举报