HDU 5943 二分图

来源:互联网 发布:有什么二次元软件 编辑:程序博客网 时间:2024/06/09 22:50

Kingdom of Obsession

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)


Problem Description
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 ns.

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
25 144 11
 

Sample Output
Case #1: NoCase #2: Yes

题意:给出n和s,能不能找出一种方法使得s+1,s+2...s+n,能匹配上1,2...n,也就是能1-n能整除s+1....s+n


题解:在10的9次方中,不可能有连续的50个数都是素数,对于一个素数,只能匹配1和它自身,所以要先去重。

当n>s时  只要将s-n自身匹配即可  也就是交换s和n的值

然后判断剩余的数 ,如果区间有两个以上的素数, 因为只有一个1,所以也就不行,否则用二分图匹配即可。

ps:当然区间也会很小,开55即可。

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;typedef long long ll;ll s,n,a[55][55],match[55],vis[55];ll isprime(ll t){    for(ll i=2;i*i<=t;i++){        if(t%i==0)return 0;    }    return 1;}bool dfs(ll u){        for(ll v=1;v<=n;v++)        if(a[u][v]&&!vis[v]){               vis[v]=true;            if(match[v]==-1||dfs(match[v])){                 match[v]=u;                return true;            }    }    return false;}int main(){    ll cas=1,t;    scanf("%lld",&t);    while(t--){        scanf("%lld%lld",&n,&s);        if(n>s)swap(n,s);        ll i,num=0,j;        for(i=s+1;i<=s+n;i++){            num+=isprime(i);            if(num>1)break;        }        if(num==2)printf("Case #%lld: No\n",cas++);        else{            memset(a,0,sizeof(a));            for(i=s+1;i<=s+n;i++){                for(j=1;j<=n;j++){                    if(i%j==0)a[i-s][j]=1;                }            }            ll sum=0;             memset(match,-1,sizeof(match));            for(i=1;i<=n;i++){                memset(vis,0,sizeof(vis));                if(dfs(i))sum++;             }            if(sum==n)printf("Case #%lld: Yes\n",cas++);            else printf("Case #%lld: No\n",cas++);        }    }    return 0;}



0 0
原创粉丝点击