【CodeForces】679 A. Bear and Prime 100

【题目】A. Bear and Prime 100

【题意】有一数字x,每次可询问一个数字y是否x的因子,最后输出数字x是否素数,要求询问次数<=20。

【题解】容易发现[2,100]范围内的非素数一定能分解为[2,47]范围内的素数的乘积,所以只需要询问[2,47]范围内的15个素数。

平方数需要特殊判断,有4,9,16,25,49。(36可以分解为(2*3)^2,不需要特判),恰好20次询问。

#include<cstdio>
const int p[21]={0,2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,4,9,16,25,49};
char s[10];
int x=0;
int main(){
    for(int i=1;i<=20;i++){
        printf("%d\n",p[i]);
        fflush(stdout);
        scanf("%s",s);
        if(s[0]=='y')x++;
    }
    if(x<=1)printf("prime");else printf("composite");
    return 0;
}
View Code

 

posted @ 2017-12-20 12:36  ONION_CYC  阅读(188)  评论(0编辑  收藏  举报