HDU - 5943(Kingdom of Obsession)二分图+数学

HDU - 5943(Kingdom of Obsession)二分图+数学

There is a kindom of obsession, so people in this kingdom do things very strictly.

They name themselves in integer, and there are n people with their id continuous (s+1,s+2,⋯,s+n) standing in a line in arbitrary order, be more obsessively, people with id x wants to stand at yth position which satisfy

xmody=0

Is there any way to satisfy everyone’s requirement?
Input
First line contains an integer T, which indicates the number of test cases.

Every test case contains one line with two integers n, s.

Limits
1≤T≤100.
1≤n≤109.
0≤s≤109.
Output
For every test case, you should output ‘Case #x: y’, where x indicates the case number and counts from 1 and y is the result string.

If there is any way to satisfy everyone’s requirement, y equals ‘Yes’, otherwise y equals ‘No’.
Sample Input
2
5 14
4 11
Sample Output
Case #1: No
Case #2: Yes


题目大意:
给你两个数n,s,每个人的编号分别是s+1,s+2……s+n;
座位号分别是1,2,3……n
编号为x的人只能坐在座位号为y的地方,当x mod y==0时。也就是x是
y的倍数的时候

解题思路:
我们分析下题意,只要有两个质数同时存在的话,那肯定满足不了条件啦。因为第一个质数可以放在1这个座位上,但是另一个就没处放了。
A: 首先我们要知道任意两个素数之间的间隔不超过300 这个数学知识。
所以当n > 600的时候,直接就输出NO就行了
B: 对剩下的600个数,我们可以用二分图的办法解决,(匈牙利算法)。对每个人的编号,和所有能整除的座位号连边,(虽然是n²的复杂度,但毕竟只有600个数呀)然后求最大匹配就好了,如果结果==n说明每个人都安排上了,输出YES 其他的输出NO。

代码如下:

#include<bits/stdc++.h>
using namespace std;
const int maxn=1009;
int mp[maxn][maxn];//存图
int used[maxn];//标记是否用过了
int ans[maxn];//用来标记这个i和哪个值安排
int n,s;
void init()
{
	memset(mp,0,sizeof(mp));
	memset(used,0,sizeof(used));
}
bool find(int x)
{
	for(int i=1;i<=n;i++)//遍历每一个座位
	{
		if(mp[x][i]==1&&used[i]==0)
		{
			used[i]=1;
			if(ans[i]==0||find(ans[i]))
			{
				ans[i]=x;
				return true;
			}
		}
	}
	return false;
}
int solve()
{
	int sum=0;
	memset(ans,0,sizeof(ans));
	for(int i=1;i<=n;i++)//遍历每一个人的编号
	{
		memset(used,0,sizeof(used));
		if(find(i))
			sum++;
	}
	return sum;
}
int main()
{
	int T;
	cin>>T;
	int cas=1;
	while(T--)
	{
		cin>>s>>n;
		if(s<n)
			swap(s,n);
		if(n>1000)
		{
			printf("Case #%d: No\n",cas++);
			continue;
		}
		init();
		printf("Case #%d: ",cas++); 
		//建图
		for(int i=1;i<=n;i++)
		{
			int t=i+s;
			for(int j=1;j<=n;j++)
			{
				if(t%j==0)
					mp[i][j]=1;
			}
		}
		int ans=0;
		ans=solve();
		if(ans==n)
			cout<<"Yes"<<endl;
		else
			cout<<"No"<<endl;
	}
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值