首页 > 其他分享 >A. Bear and Prime 100 (交互)

A. Bear and Prime 100 (交互)

时间:2022-11-21 14:44:19浏览次数:71  
标签:Prime int 合数 50 Bear 因子 100 质数

A. Bear and Prime 100

题意:

有一个范围为 [2, 100] 的数 x,让你提问不超过20次这,每次提问你可以给bot一个整数,如果整数是x的因子,则返回 yes,否则返回no,问这个数是不是质数。

考察知识点:

算数基本定理:一个合数能分解为至少2个质因数。

思路:

如果提问的这个数是合数,那么它至少有两个质因子,由于 $ x \leq 100 $,所以质因子的大小不会超过50,我们可以枚举2~50以内的质因子(15个),看有多少是x的质因子,如果超过1个就是合数,如果为0个那么x一定是大于50的质数,如果为1个,那么看看50以内质因子的平方是不是x的因子(判断100以内,一共4个),如果是那么就是合数,否则是质数。

const int N = 110;
int st[N];

bool is_prime(int x){
	for(int i = 2; i * i <= x; i ++ ){
		if(x % i == 0) return false;
	}
	return true;
}

bool ask(int x){
	cout << x << endl;
	string s; cin >> s;
	return s == "yes";
}

void answer1(){
	cout << "composite" << endl;
}

void answer2(){
	cout << "prime" << endl;
}
signed main()
{

	for(int i = 2; i <= 50; i ++ ){
		if(is_prime(i)) st[i] = 1;
	}
	int cnt = 0;
	for(int i = 1; i <= 50; i ++ ){
		if(st[i]){
			if(ask(i)) cnt ++;
		}
	}
	if(cnt == 1){
		if(ask(4) || ask(9) || ask(25) || ask(49)) answer1();
		else answer2();
	}else if(cnt == 0){
		answer2();
	}else {
		answer1();
	}
	return 0;
}


标签:Prime,int,合数,50,Bear,因子,100,质数
From: https://www.cnblogs.com/acmerbs/p/16911351.html

相关文章