Bear and Prime 100

Description

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.

Sample Input

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次询问,每次询问你可以输出一个数,然后后台会返回一个字符串,yes或no给你,如果你输出的数是隐藏数的约数的话,后台就会返回一个yes,否则就是一个no,根据这样的多次交互,你需要猜测隐藏数是素数还是合数。

思路:

我们假设隐藏数是一个合数,那么它就是多个素数相乘,因为在2到100间最小的素数是2,又因为隐藏数是小于等于100的,所以构成隐藏数的最大的素数为47,可以通过素数打表找出2到47之间的所有素数:
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47。
对于一个隐藏数,我们想要知道它是素数还是合数,只需知道上述素数中有几个是它的约数,若有大于1个的则是合数,当然这样考虑会忽略平方数,次方数的情况(比如说4,只有2一个素数约数,但是却是合数),所以在考虑2,3,5,7的情况的时候,我们还要考虑它们的平方数4,9,25,49(11*11>100)是不是隐藏数的约数,由于最多可以询问20次,而我们考虑的情况只有15(2到47的素数个数)+4(2,3,5,7的平方数)=19种,也就是最多只需要询问19次,所以全都询问一遍确定约数的个数即可。需要注意的是在输出一个数后你需要在后面写上fflush(stdout),代码如下:

代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
#include<stack>
using namespace std;
#define clr_all(a,b) memset(a,b,sizeof(a))
#define clr(a,b,n) memset(a,b,sizeof(a[0])*n)
template<typename T> T Max(T a, T b){ return a > b ? a : b; }
const int MAXN = 100 + 10;

int main(){
	int d = 0;
	int num[] = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47 };
	for (int i = 0; i < 15; i++){
		printf("%d\n", num[i]); 
		fflush(stdout);
		char str[5];
		scanf("%s", str);
		if (str[0] == 'y'){
			d++;
			if (num[i] <= 7){
				printf("%d\n", num[i] * num[i]);
				fflush(stdout);
				scanf("%s", str);
				if (str[0] == 'y')
					d++;
			}
		}
	}
	if (d >= 2){
		printf("composite\n");
		fflush(stdout);
	}
	else{
		printf("prime\n");
		fflush(stdout);
	}
	return 0;
}
Bear 编译数据库是一个用于帮助开发者生成和维护代码编辑器的编译数据库的工具。编译数据库是一组描述代码文件的元数据,包括每个文件的编译指令、编译选项、头文件和源代码之间的依赖关系等。 Bear 编译数据库的目的是为了帮助开发者通过为编辑器提供准确的编译数据库来改善代码的智能功能,如自动补全、代码导航、语法检查和重构等。通过使用编译数据库,编辑器能够更好地理解代码,并提供更准确和有用的代码建议。 使用 Bear 编译数据库的步骤大致如下: 1. 首先,需要安装 Bear 编译数据库工具。可以通过在终端中运行相应的命令,使用包管理器(如apt、yum、brew等)安装该工具。 2. 安装完成后,运行 bear make 命令,该命令会在当前工作目录中执行 make 命令,并生成编译数据库。可以根据具体需求,使用不同的编译器选项来生成数据库,如 -C (--clean) 参数可以清除现有数据库,重新生成新的。 3. 生成的编译数据库文件默认为 compile_commands.json,它是一个 JSON 格式的文件,描述了项目中所有源代码文件的编译相关信息。 4. 将生成的编译数据库文件移动到编辑器所需的位置。不同的编辑器可能需要将数据库文件放置在不同的目录中,具体可以参考编辑器的文档。 通过以上步骤,开发者就可以使用 Bear 编译数据库来改善代码编辑体验。需要注意的是,由于生成的编译数据库中包含了源代码文件的路径信息,因此在将数据库文件与他人共享或在不同的机器上使用时,需要注意路径的一致性,以避免编辑器无法正确解析编译数据库的问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值