(2016年中国大学生程序设计竞赛(杭州) )HDU 5943 Kingdom of Obsession 素数间距 + 匈牙利算法

来源:互联网 发布:linux设定文件夹权限 编辑:程序博客网 时间:2024/06/02 06:42

Kingdom of Obsession
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1009 Accepted Submission(s): 289

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
1≤T≤100.
1≤n≤109.
0≤s≤109.

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
2016年中国大学生程序设计竞赛(杭州)

题意:
给定S,N,把S+1,S+2,…S+N这N个数填到1,2,…,N里,要求X只能填到X的因子的位置。(即X%Y=0,那么X才能放在Y位置)
问是否能够放满。

思路:
将题目的意思抽象出来:有n个位置和n个人,一个人可以坐多个位置中的一个,问你能否让每一个位置分配一个人? 就是匹配问题了。
但是关于匹配问题我们要建图,可是数据太大 ,那我们就来想想

首先,如果S < N,那么S+1,S+2…N这些数直接放在S+1,S+2…N的位置上(如果其他数x放在这些位置上面,这些数不放在对应位置,那么x一定能放在这些数放的位置,所以直接交换即可)

所以可以直接将S和N调换,缩小N。
接着看N个连续的数,如果出现2个素数。那么必然无解(都只能放1)
所以可以估算一下素数的最大间隔(我取504),N超过必然无解。
N小于504的情况下,直接暴力建边(能整除就连边),然后跑二分图匹配即可。

被sqrt()函数坑哭了 还有就是No写成了NO ……

AC代码:

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>using namespace std;const int maxn = 504;struct edge{    int next,to;} edges[maxn*maxn];int e;int head[2*maxn];int mk[2*maxn];bool vis[2*maxn];void add_edges(int u,int v){    edges[e].to = v;    edges[e].next = head[u];    head[u] = e++;}bool dfs(int u){    for(int i=head[u];i!=-1;i=edges[i].next)    {        int v = edges[i].to;        if(!vis[v])        {            vis[v] = 1;            if(!mk[v] || dfs(mk[v]))            {                mk[v] = u;                return 1;            }        }    }    return 0;}int main(){    int t,cas = 1;    int n,s,ans;    scanf("%d",&t);    while(t--)    {        e = 0; ans = 0;        memset(head,-1,sizeof(head));        memset(mk,0,sizeof(mk));        scanf("%d%d",&n,&s);        if(s < n) swap(n,s);        if(n > maxn)        {            printf("Case #%d: No\n",cas++);            continue;        }        for(int i=1;i<=n;i++)        {            for(int j=1;j<=n;j++)            {                if((s + i) % j == 0) add_edges(j,n+i);            }        }        for(int i=1;i<=n;i++)        {            if(!mk[i])            {                memset(vis,0,sizeof(vis));                if(dfs(i)) ans++;            }        }        if(ans == n) printf("Case #%d: Yes\n",cas++);        else printf("Case #%d: No\n",cas++);    }    return 0;}
1 0