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

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很大啊。

不过我们知道素数有个特性就是因数只有1和它本身。所以素数只能待在位置1或者标号为本身的位置上

1.如果m>n


如果n大于等于两个素数之间的距离,那么肯定是不可能匹配的。

如果小于直接二分图匹配一下。

2.如果m<=n


黄色部分长度为m如果m的长度大于等于两个素数的间隔那肯定是不符合题意。

如果小于那么就把m部分二分图匹配,匹配的话肯定符合题意,不匹配的话不符合题意,至于为什么不和重合部分匹配,看评论。

这两个一综合就会发现第二种如果mn互换和第一种是一样的。

至于二分图匹配:

就是n个点之间的匹配,如果两个点之间满足等式就连一条边,最后看看能不能全匹配上就可以了。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <queue>
#include <vector>
#include <cstring>
#include <string>
using namespace std;
const int MAXN=300+7;

int n,m;
int vis[MAXN],match[MAXN];
int num[MAXN];
int head[MAXN];
int cnt;
struct node
{
    int v;
    int next;
} edge[MAXN*MAXN];

void add(int u,int v);
int dfs(int u);
int build()
{
    cnt=0;
    memset(match,-1,sizeof(match));
    int i,j,v;
    for(i=1; i<=n; ++i)head[i]=-1;
    for(i=1; i<=n; ++i)
    {
        match[i]=-1;
        for(j=1; j<=n; ++j)
        {
            v=j+m;
            if(v%i==0)add(i,j);
        }
    }
    int sum=0;
    for(i=1; i<=n; ++i)
    {
        memset(vis,0,sizeof(vis));
        if(dfs(i))sum++;
    }
    return sum;
}
void add(int u,int v)
{
    edge[cnt].v=v;
    edge[cnt].next=head[u];
    head[u]=cnt++;
}
int dfs(int u)
{
    int i;
    for(i=head[u]; i!=-1; i=edge[i].next)
    {
        int v=edge[i].v;
        if(!vis[v])
        {
            vis[v]=1;
            if(match[v]==-1||dfs(match[v]))
            {
                match[v]=u;
                return 1;
            }
        }
    }
    return 0;
}
int main()
{
    int t;
    scanf("%d",&t);
    for(int t1=1; t1<=t; ++t1)
    {
        scanf("%d%d",&n,&m);
        printf("Case #%d: ",t1);
        if(m<n)swap(m,n);
        if(n>300)puts("No");
        else
        {
            int ans=build();
            if(ans==n)puts("Yes");
            else puts("No");
        }

    }
    return 0;
}



评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值