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+n],这n个数放在[1,n]这n个位置上,并使i位置上的数x整除i。)

(思路:

1.如果s<n,那么[s+1,n],这个区间的数肯定要放在他们本身的位置更优。假设一个数x(x!= s+1)放在了s+1的位置上,假设x>n,也就是说我们用掉了一个在[n+1,n+s]区间的数,那么x这个位置也需要一个在[n+1,n+s]区间的数,那么[n+1,n+s]区间的数,肯定不够用。所以[s+1,s+n]区间的数放在本身位置上。现在我们只要把[n+1,n+s]区间的数放在[1,s]位置上即可。也就是把s和n交换。

2.如果s>n,当[s+1,s+n]中有超过两个素数,那么肯定没有答案。因为这两个素数只能放在1位置上。打3e8内的表就可以发现,两个素数的差不会很大,就假设为300也就是当n大于300时,直接输出“No”。否则的话,二分图匹配即可。)

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <vector>
#include <stack>
#define ll long long
using namespace std;
const int N=3e2+10;
//int prime[N];
//bool vis[N];
int mp[N][N],vis[N],match[N];
void Init(int n)
{
	for(int i=1;i<=n;i++)
	{
		match[i]=-1;
		for(int j=1;j<=n;j++)
			mp[i][j]=0;		
	}
	return ;
}
bool dfs(int x,int n)
{
	
	for(int i=1;i<=n;i++)
	{
		if(!mp[x][i]||vis[i]) continue;
		vis[i]=1;
		if(match[i]==-1||dfs(match[i],n))	
		{
			match[i]=x;
			return 1;
		}
	}
	return 0;	
}
bool hungary(int n)
{
	int res=0;
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=n;j++)
			vis[j]=0;
		if(dfs(i,n))
			res++;
	}
	if(res==n) return 1;
	else return 0;	
}
int main(void)
{
//	int maxx=0;
//	int cnt=0;
//	for(int i=2;i<N;i++)
//	{
//		if(!vis[i])
//		{
//			prime[++cnt]=i;
//		//	cout<<prime[cnt]<<endl;
//			if(cnt!=1)
//				maxx=max(maxx,prime[cnt]-prime[cnt-1]);			
//		}
//		for(int j=1;j<=cnt&&i*prime[j]<N;j++)
//		{
//			vis[i*prime[j]]=1;	
//			if(i%prime[j]==0) break;
//		}
//				
//	}
//	cout<<maxx<<endl;		
	int t,tt=0;
	scanf("%d",&t);
	while(t--)
	{
		int s,n;
		scanf("%d%d",&n,&s);
		if(s<n) swap(s,n);
		printf("Case #%d: ",++tt);
		if(n>300)
		{
			printf("No\n");	
			continue;	
		}
		Init(n);
		for(int i=1;i<=n;i++)
			for(int j=1;j<=n;j++)
				if((s+i)%j==0) mp[i][j]=1;			
		if(hungary(n))
			printf("Yes\n");
		else
			printf("No\n");	
	}
	
	
	return 0;	
} 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值