历届试题 约数倍数选卡片

对于必胜状态,必有一个后继状态是必败的,对于必败状态所有后继状态都是必胜的。假设当前选择是必胜态,那么后手的所有选择都必须是P态才行。


#include<bits/stdc++.h>

using namespace std;

typedef long long LL;
const int N = 100;

vector<int> a[N], b; 
int cnta[N];

int dfs(int x)
{
//	for(int i=0; i<a[x].size(); i++)//TLE
	for(int i=a[x].size()-1; i>=0; i--)//从大的数开始选,因为大的数约数和倍数少
	{
		int tmp = a[x][i];
		
		if(cnta[tmp]){
			cnta[tmp]--;
			//不管是否为必胜态,都必须还原 
			if(dfs(tmp)){
				cnta[tmp]++;//还原  
				return 0;
			} 
			cnta[tmp]++;//还原 
			
		}
	}
	
	return 1;
}

int main()
{
	memset(cnta, 0, sizeof(cnta));
	
	string s;
	
	getline(cin, s);
	
	stringstream ss(s);
	
	int x;
	
	while(ss >> x){
		cnta[x]++;
	//计算x的约数或倍数
	if(cnta[x] == 1){//若未加此条件...则会有好几倍的x的约数或倍数,会TLE 
	 	for(int j=1; j<=N; j++){
	 		if(x%j == 0)
	 			a[x].push_back(j);
	 		else if(j%x == 0)
	 			a[x].push_back(j);
		 }
		 
	//	 sort(a[x].begin(), a[x].end());
	}
}
	
	string sc;
	getline(cin, sc);
	
	stringstream st(sc);

	int tmp;
	while(st >> tmp){
		b.push_back(tmp);
	//	cout << tmp << endl;
	}
	
	sort(b.begin(), b.end());
	
	
	int flag = 0; 
	for(int i=0; i<b.size(); i++){
		int tmp = b[i];
	//	cout << tmp << endl;
		
		if(cnta[tmp]){
			cnta[tmp]--;
			
			if(dfs(tmp)){//若为必胜态 
				cout << tmp <<endl;
				flag = 1;
				break;
			}
			cnta[tmp]++;//还原 
		}
		
	}

	if(!flag){
		cout << -1 <<endl;
	} 
	
	
	return 0;
 } 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值