hdu 5943 Kingdom of Obsession 二分图匹配+素数定理

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

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
#define eps 1e-14
const int N=2e5+10,M=1e6+10,inf=1e9+10,mod=1e9+7;
const ll INF=1e18+10;
int prime(int n)
{
    if(n<=1)return 0;
    if(n==2)return 1;
    if(n%2==0)return 0;
    int k, upperBound=n/2;
    for(k=3; k<=upperBound; k+=2)
    {
        upperBound=n/k;
        if(n%k==0)return 0;
    }
    return 1;
}
const int MAXN=1505;
map<int,int>linker;
map<int,int>used;
vector<int>mp[MAXN];
int uN;
bool dfs(int u)
{
    for(int i=0;i<mp[u].size();i++)
    {
        if(!used[mp[u][i]])
        {
            used[mp[u][i]]=1;
            if(linker[mp[u][i]]==0||dfs(linker[mp[u][i]]))
            {
                linker[mp[u][i]]=u;
                return true;
            }
        }
    }
    return false;
}
int hungary()
{
    int u;
    int res=0;
    linker.clear();
    for(u=1;u<=uN;u++)
    {
        used.clear();
        if(dfs(u)) res++;
    }
    return res;
}
int main()
{
    int T,cas=1;
    scanf("%d",&T);
    while(T--)
    {
        for(int i=0;i<MAXN;i++)
           mp[i].clear();
        int n,s;
        scanf("%d%d",&n,&s);
        if(n>s)swap(n,s);
        int p=0;
        for(int i=s+1; i<=s+n; i++)
        {
            if(prime(i))
            {
                p++;
                if(p>=2)break;
            }
        }
        printf("Case #%d: ",cas++);
        if(p>=2)
        {
            printf("No\n");
            continue;
        }
        for(int i=s+1; i<=s+n; i++)
        {
            for(int j=1; j<=n; j++)
            {
                if(i%j==0)
                {
                    mp[j].push_back(i);
                }
            }
        }
        uN=n;
        int hh=hungary();
        if(hh==n)
            printf("Yes\n");
        else
            printf("No\n");
    }
    return 0;
}
时间: 2024-08-01 20:30:49

hdu 5943 Kingdom of Obsession 二分图匹配+素数定理的相关文章

HDU 5943 Kingdom of Obsession 二分图的匹配

题目链接→戳 s,n 1e9的范围直接暴力匹配二分图肯定会T... 对于区间[1,n]和[s+1,s+n],如果存在重叠部分, 比如1,2,3......,n-2,   n-1 ,  n ↓      ↓      ↓ s+1,s+2,s+3......s+n s+1,s+2,s+2直接放在s+1,s+2,s+3的位置,只需要去匹配[1,s]和[n,s+n] 这样,我们需要去匹配的区间内就没有s+i可以放在s+i位置的了,如果区间内存在>=2个素数,这些素数只能放在第一个位置,这种情况肯定是No

HDU 5943 Kingdom of Obsession 【二分图匹配 匈牙利算法】 (2016年中国大学生程序设计竞赛(杭州))

Kingdom of Obsession Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 49    Accepted Submission(s): 14 Problem Description There is a kindom of obsession, so people in this kingdom do things very

HDU 5943 Kingdom of Obsession

题意:n个人编号为[s+1, s+n],有n个座位编号为[1,n],编号为 i 的人只能坐到编号为它的约数的座位,问每个人是否都有位置坐. 题解:由于质数只能坐到1或者它本身的位置上,所以如果[n+1,n+s]区间内如果有多于一个质数时肯定无解, 有解时s 一定很小因为1e9以内,最远的两个素数相差282 (打表得出), 可以证明 [s+1,n]这一段数肯定坐到自己编号的位置上要更好所以剩下的用匈牙利匹配一下即可 简单证明一下" [s+1,n]这一段数肯定坐到自己编号的位置上要更好"

hdu 3829 Cat VS Dog 二分图匹配 最大点独立集

Cat VS Dog Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 125536/65536 K (Java/Others) Problem Description The zoo have N cats and M dogs, today there are P children visiting the zoo, each child has a like-animal and a dislike-animal, if the

HDU 5093 Battle ships(二分图匹配)

该题是一道经典的二分图匹配题目 .  同一列(行)上不能放两个船除非有冰山隔着.对于这种二维平面图,我们很容易想到将行和列分成两个集合,进行二分图匹配,当一个行坐标匹配到一个列坐标时,该格子可以放置船.那么为了使任意两个船都不在同一行或者同一列,除非有冰山,我们可以将每一行中一块连续的只能放置一个船的区域都设成一个编号,同样的按照列也这样处理,这样就相当于将行和列缩点了,接下来用最大流模板套一套就可以了 . 处理二分图还有一种更好的算法,叫匈牙利算法,紫书上没有,先用最大流算法解决吧 . 紫书十

hdu 5727 Necklace 阴阳珠 二分图匹配+暴力全排列

Necklace Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2462    Accepted Submission(s): 775 Problem Description SJX has 2*N magic gems. N of them have Yin energy inside while others have Yang e

HDU 2236 无题II(二分图匹配+二分)

HDU 2236 无题II 题目链接 思路:行列只能一个,想到二分图,然后二分区间长度,枚举下限,就能求出哪些边是能用的,然后建图跑二分图,如果最大匹配等于n就是符合的 代码: #include <cstdio> #include <cstring> #include <vector> #include <algorithm> using namespace std; const int N = 105; int t, n, x[N][N], have[N]

HDU 1150 Machine Schedule(二分图匹配)

解题思路: 本题要求的为最小点覆盖,最小点覆盖 == 最大匹配,要注意初始为模式0,没有消耗,所以模式0不需要连边. #include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> #include <algorithm> #include <vector> #include <cmath> #include <queue>

hdu 5943(素数间隔+二分图匹配)

Kingdom of Obsession Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 200    Accepted Submission(s): 64 Problem Description There is a kindom of obsession, so people in this kingdom do things ver