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): 1450    Accepted Submission(s): 433


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

题目大意:


给出1~N,N个位子,然后给出s+1~s+N,N个数,一个数x可以放在位子pos的条件是:x%pos==0,问我们这N个数能否放在N个位子上。


思路:


问题可以分成两种情况去讨论:


①s<=n,我们知道,此时这种情况,我们肯定希望从s+1到n这些数,都放在位子s+1到n上是最好的,然后我们去判定从n+1到n+s这些数,能否放在从1到s这些位子上即可。

但是我们知道,如果这里s很小的话,我们可以进行二分图匈牙利匹配即可,否则如果s很大的话我们没法搞定这个问题。

随后观察到问题的本质,我们如果从n+1到s+n有一个素数的话,这个素数无论如何都要放在位子1上才行,如果有两个素数的话,问题的结果肯定就是No了。根据素数间隔的一些理论我们知道,大约一个素数和另一个素数的间隔,在int范围内的话,大概有300+个数就会再出现另一个素数,所以我们可以判定,如果s大于了1000的话,就一定是No的结果了。

从而搞定了当前情况的解。


②s>n,其实这种情况,我们交换一下s和n,问题就翻转变成了①的问题,而且对结果并没有影响。


Ac代码:

#include<stdio.h>
#include<string.h>
#include<vector>
using namespace std;
vector<int>mp[1500];
int match[1500];
int vis[1500];
int find(int u)
{
    for(int i=0;i<mp[u].size();i++)
    {
        int v=mp[u][i];
        if(vis[v]==0)
        {
            vis[v]=1;
            if(match[v]==-1||find(match[v]))
            {
                match[v]=u;
                return 1;
            }
        }
    }
    return 0;
}
int main()
{
    int t;
    int kase=0;
    scanf("%d",&t);
    while(t--)
    {
        int n,s;
        scanf("%d%d",&n,&s);
        if(s>n)swap(n,s);
        printf("Case #%d: ",++kase);
        if(s>=1400)printf("No\n");
        else
        {
            for(int i=1;i<=s;i++)
            {
                mp[i].clear();
                for(int j=n+1;j<=s+n;j++)
                {
                    if(j%i==0)
                    {
                        mp[i].push_back(j-n);
                    }
                }
            }
            int output=0;
            memset(match,-1,sizeof(match));
            for(int i=1;i<=s;i++)
            {
                memset(vis,0,sizeof(vis));
                if(find(i))output++;
            }
            if(output==s)printf("Yes\n");
            else printf("No\n");
        }
    }
}










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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值