K - Kingdom of Obsession

探讨一个关于整数排列的数学谜题,在一个由连续整数组成的王国中,每个成员希望根据特定条件占据特定位置,算法挑战在于判断是否能实现所有人的愿望。

There is a kindom of obsession, so people in this kingdom do things very strictly. 

They name themselves in integer, and there are nn people with their id continuous (s+1,s+2,⋯,s+n)(s+1,s+2,⋯,s+n) standing in a line in arbitrary order, be more obsessively, people with id xx wants to stand at ythyth position which satisfy 

xmody=0xmody=0



Is there any way to satisfy everyone's requirement?

Input

First line contains an integer TT, which indicates the number of test cases. 

Every test case contains one line with two integers nn, ss. 

Limits 
1≤T≤1001≤T≤100. 
1≤n≤1091≤n≤109. 
0≤s≤1090≤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 yequals 'No'.

Sample Input

2
5 14
4 11

Sample Output

Case #1: No
Case #2: Yes

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
 
using namespace std;
 
#define LL long long
const int INF=0x3f3f3f3f;
 
int n,ss;
int x[559],y[559];
int s[559],nt[260009],e[260009];
int visit[559];
bool path(int k)
{
    for(int i=s[k]; ~i; i=nt[i])
    {
        int ee=e[i];
        if(!visit[ee])
        {
            visit[ee]=1;
            if(y[ee]==-1||path(y[ee]))
            {
                y[ee]=k;
                x[k]=ee;
                return 1;
            }
        }
    }
    return 0;
}
 
void MaxMatch()
{
    int ans=0;
    memset(x,-1,sizeof(x));
    memset(y,-1,sizeof(y));
    for(int i=1;i<=n;i++)
    {
        if(x[i]==-1)
        {
            memset(visit,0,sizeof(visit));
            if(path(i)) 
			ans++;
        }
    }
    if(ans==n) 
	printf("Yes\n");
    else 
	printf("No\n");
}
 
int main()
{
    int t,cas=0;
    scanf("%d",&t);
    while(t--)
    {
        printf("Case #%d: ",++cas);
        scanf("%d%d",&n,&ss);
        if(ss<=1)
        {
            printf("Yes\n");
            continue;
        }
        if(ss<n) 
		swap(ss,n);
        if(n>100)
        {
            printf("No\n");
            continue;
        }
        int cnt=1;
        memset(s,-1,sizeof(s));
        for(int i=ss+1;i<=ss+n;i++)
        { 
            for(int j=1;j<=n;j++)
            {
            	if(i%j==0) 
				{
					nt[cnt]=s[i-ss];
					s[i-ss]=cnt;
					e[cnt++]=j;
				}
            }
		} 
        MaxMatch();
    }
    return 0;
}

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值