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
1T100 .
1n109 .
0s109
.
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



/*  
 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5943  
  
题目大意:  
  给定S,N,把S+1,S+2,...S+N这N个数填到1,2,...,N里,要求X只能填到X的因子的位置。  
    (即X%Y=0,那么X才能放在Y位置)  
    问是否能够放满。  
  
题目思路:  【二分图匹配 匈牙利算法】  
  
首先,如果S<N,那么S+1,S+2...N这些数直接放在S+1,S+2...N的位置上  
(如果其他数x放在这些位置上面,这些数不放在对应位置,那么x一定能放在这些数放的位置,所以直接交换即可)  
所以可以直接将S和N调换,缩小N。  
接着看N个连续的数,如果出现2个素数。那么必然无解(都只能放1)  
所以可以估算一下素数的最大间隔(我取504),N超过必然无解。  
N小于504的情况下,直接暴力建边(能整除就连边),然后跑二分图匹配即可。  

*/

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long ll;
const int MAXN=1000;
ll L1,L2,R1,R2;  //u,v数目
int g[MAXN][MAXN];//编号是0~n-1的 
int linker[MAXN];
bool used[MAXN];
bool dfs(int u)
{
    ll v;
    for(v=1;v<=R1-L1+1;v++)
        if(g[u][v]&&!used[v])
        {
            used[v]=true;
            if(linker[v]==-1||dfs(linker[v]))
            {
                linker[v]=u;
                return true;
            }    
        }  
    return false;  
}    
int hungary()
{
    int res=0;
    int u;
    memset(linker,-1,sizeof(linker));
    for(u=1;u<=R2;u++)
    {
        memset(used,0,sizeof(used));
        if(dfs(u))  res++;
    } 
    return res;   
}     
bool is_prime(ll num)
{
	ll i,j,n=sqrt(num);
	if(num==2) return true;
	for(i=2;i<=n;i++) {
		if(num%i==0) return false;
	}
	return true;
}

int main()
{
	ll i,j,n,s;
	int T,cnt;
	cin>>T;
	for(int _=1;_<=T;_++) {
		cin>>n>>s;
		L1=max(s+1,n+1),R1=s+n;
		R2=min(n,s),L2=1;
		if(s==0) {
			cout<<"Case #"<<_<<": Yes"<<endl;
			continue;
		}
		for(i=L1,cnt=0;i<=R1;i++) {
			if(is_prime(i)) cnt++;
			if(cnt==2) break;
		}
		if(i<=R1) {
			cout<<"Case #"<<_<<": No"<<endl;
			continue;
		}
		memset(g,0,sizeof(g));
		for(i=L2;i<=R2;i++) {
			for(j=L1;j<=R1;j++) {
				if(j%i==0) g[i][j-L1+1]=1;
			}
		}
		if(hungary()==R2) {
			cout<<"Case #"<<_<<": Yes"<<endl;
			continue;
		}
		else {
			cout<<"Case #"<<_<<": No"<<endl;
			continue;
		}
	}
	return 0;
}




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值