codeforces 680C C. Bear and Prime 100(数论)

题目链接:

C. Bear and Prime 100

time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

This is an interactive problem. In the output section below you will see the information about flushing the output.

Bear Limak thinks of some hidden number — an integer from interval [2, 100]. Your task is to say if the hidden number is prime or composite.

Integer x > 1 is called prime if it has exactly two distinct divisors, 1 and x. If integer x > 1 is not prime, it's called composite.

You can ask up to 20 queries about divisors of the hidden number. In each query you should print an integer from interval [2, 100]. The system will answer "yes" if your integer is a divisor of the hidden number. Otherwise, the answer will be "no".

For example, if the hidden number is 14 then the system will answer "yes" only if you print 2, 7 or 14.

When you are done asking queries, print "prime" or "composite" and terminate your program.

You will get the Wrong Answer verdict if you ask more than 20 queries, or if you print an integer not from the range [2, 100]. Also, you will get the Wrong Answer verdict if the printed answer isn't correct.

You will get the Idleness Limit Exceeded verdict if you don't print anything (but you should) or if you forget about flushing the output (more info below).

 
Input
 

After each query you should read one string from the input. It will be "yes" if the printed integer is a divisor of the hidden number, and "no" otherwise.

 
Output
 

Up to 20 times you can ask a query — print an integer from interval [2, 100] in one line. You have to both print the end-of-line character and flush the output. After flushing you should read a response from the input.

In any moment you can print the answer "prime" or "composite" (without the quotes). After that, flush the output and terminate your program.

To flush you can use (just after printing an integer and end-of-line):

  • fflush(stdout) in C++;
  • System.out.flush() in Java;
  • stdout.flush() in Python;
  • flush(output) in Pascal;
  • See the documentation for other languages.

Hacking. To hack someone, as the input you should print the hidden number — one integer from the interval [2, 100]. Of course, his/her solution won't be able to read the hidden number from the input.

 
Examples
 
input
yes
no
yes
output
2
80
5
composite
input
no
yes
no
no
no
output
58
59
78
78
2
prime

题意:

底数是[2,100]中的数,现在你最多询问20次,询问为这个数是不是底数的因数,最后判断这个数是不是素数;

思路:

[2,100]的素数有20+个,不能都询问一遍,可以询问[2,50]里面的素数,如果全都是no就是大于50的素数,如果yes的个数大于1,那么一定不是素数,如果是一个yes,那么这个数可能是小于50的素数,也可能像4,8,9这种数,那么再判断一下i*i就好了;具体的看代码吧;

AC代码:

//#include <bits/stdc++.h>
#include <vector>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <cstring>
#include <algorithm>
#include <cstdio>

using namespace std;
#define Riep(n) for(int i=1;i<=n;i++)
#define Riop(n) for(int i=0;i<n;i++)
#define Rjep(n) for(int j=1;j<=n;j++)
#define Rjop(n) for(int j=0;j<n;j++)
#define mst(ss,b) memset(ss,b,sizeof(ss));
typedef long long LL;
template<class T> void read(T&num) {
    char CH; bool F=false;
    for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
    for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
    F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {
    if(!p) { puts("0"); return; }
    while(p) stk[++ tp] = p%10, p/=10;
    while(tp) putchar(stk[tp--] + '0');
    putchar('
');
}

const LL mod=1e9+7;
const double PI=acos(-1.0);
const LL inf=1e14;
const int N=1e5+15;

int n,a,t[200],flag[200];
int prime()
{
    for(int i=2;i<=100;i++)
    {
        if(!flag[i])
        {
            for(int j=2*i;j<=100;j+=i)
                flag[j]=1;
        }
    }
    //cout<<"#"<<endl;
    return 0;
}
int main()
{
    prime();
   string s;
   int num=0;
   for(int i=2;i<=50;i++)
   {
       if(!flag[i])
       {
           printf("%d
",i);
           fflush(stdout);
           cin>>s;
           if(s=="yes")
           {
               num++;
               if(i*i<=100)
               {
               printf("%d
",i*i);
               fflush(stdout);
               cin>>s;
               if(s=="yes")num++;
               }
           }
       }
   }
   if(num<=1)cout<<"prime"<<"
";
   else cout<<"composite"<<"
";
fflush(stdout);


        return 0;
}


原文地址:https://www.cnblogs.com/zhangchengc919/p/5572153.html