[蓝桥杯]约数倍数选卡片dfs

[蓝桥杯]约数倍数选卡片dfs_第1张图片

[蓝桥杯]约数倍数选卡片dfs_第2张图片 

 http://oj.ecustacm.cn/problem.php?id=1450

#include
#define ls o<<1
#define rs o<<1|1
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
typedef long long LL; 
template inline void gmax(T &a,T b){if(b>a)a=b;}
template inline void gmin(T &a,T b){if(b a[N],b;
//vector si; 
int dfs(int x){
	for(int i=a[x].size()-1;i>=0;i--){
		int u=a[x][i];
		if(num[u]){
			num[u]--;
			int t=dfs(u);//now I take the u
			num[u]++;
			if(t==-1){
				return 1;
			}
		}
	}
	return -1;// the first time to call this dfs
}
int main(){
//	freopen("in.txt","r",stdin);
	string s;
	getline(cin,s);
	stringstream in1(s);
	int x;
	int mmax=-0x3f3f3f3f;
	int mmin=0x3f3f3f3f;
	while(in1>>x){
		num[x]++;
		gmax(mmax,x);
		gmin(mmin,x);
	}
	getline(cin,s);
	stringstream in2(s);
	while(in2>>x){
		b.push_back(x);
	}
	for(int i=mmin;i<=mmax;i++){
		if(num[i]){
			num[i]--;
			for(int j=mmin;j<=mmax;j++){
				if(num[j]){
					if(i%j==0||j%i==0){
							a[i].push_back(j);
						}
					}
				}
				num[i]++;//related numbers
		}
	}
	sort(b.begin(),b.end());
	for(int i=0;i

 1.优先用大数,会提前结束游戏

2.类似枚举的dfs

你可能感兴趣的:([蓝桥杯]约数倍数选卡片dfs)