HDU 5943 二分图

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  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

题意:给出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;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值