hdu 5943 Kingdom of Obsession(二分匹配)

Kingdom of Obsession

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1958    Accepted Submission(s): 581


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
 

Source

解:首先估计一下两个素数之间的距离大约是500~600,如果序列移动距离超过这个区间即一定存在两个素数

序列重合区间用被重合的数作为最终标号,因为将小的约数留给其他数一定更优

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int N = 600;
typedef long long LL;
int w[600][600], vis[600],match[N];
int n, s;
int dfs(int u)
{
    for(int i=1;i<=n;i++)
    {
        if(!w[u][i]||vis[i]) continue;
        vis[i]=1;
        if(match[i]==-1||dfs(match[i]))
        {
            match[i]=u;
            return 1;
        }
    }
    return 0;
}

int main()
{
    int t, ncase=1;
    scanf("%d", &t);
    while(t--)
    {
        scanf("%d %d", &n, &s);
        if(s<n)swap(s,n);
        if(n>504) printf("Case #%d: No\n",ncase++);
        else
        {
            memset(w,0,sizeof(w));
            for(int i=1;i<=n;i++)
            {
                int x=i+s;
                for(int j=1;j<=n;j++)
                {
                    if(x%j==0) w[i][j]=1;
                }
            }
            int cnt=0;
            memset(match,-1,sizeof(match));
            for(int i=1;i<=n;i++)
            {
                memset(vis,0,sizeof(vis));
                if(dfs(i)) cnt++;
            }
            if(cnt!=n) printf("Case #%d: No\n",ncase++);
            else printf("Case #%d: Yes\n",ncase++);
        }
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值