题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5943

题意:给你两个数n, s 然后让你判断是否存在(s+1, s+2, s+3, ... , s+n )的任意排列方式使得每个数都满足当前数num,与num所在位置 pos  形成num%pos=0;

例如 n = 4 , s = 11

num = {13, 14, 15, 12}

pos =  {1,    2,   3,    4}

每个num与之对应的pos都是num%pos = 0;的关系

分为两种情况:当s+1>n 时,我们只需看第二个数组中是否含有>=2个素数即可, 因为1只有1个;

当s+1<=n时,两个区间有重叠,我们可以忽略这些重叠部分,看两端的即可,其实就是交换一下n,s的值; 

其实就是两个数组建图,然后求最大匹配是否为1,当然n比较大,但是连续600个数中间一定包含>=2个素数,所以当n<600时可以用二分匹配来做;

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<vector>
using namespace std;
typedef long long LL;
const int oo = 0xfffffff;
const int N = ; int used[N], vis[N], n, s;
vector<int> G[N]; bool Find(int u)
{
int len=G[u].size();
for(int i=; i<len; i++)
{
int v = G[u][i];
if(!vis[v])
{
vis[v] = ;
if(!used[v] || Find(used[v]))
{
used[v] = u;
return true;
}
}
}
return false;
} int main()
{
int T, t = ;
scanf("%d", &T);
while(T --)
{
scanf("%d %d", &n, &s); if(s+ <= n)///忽略中间重合的部分;
swap(s, n); if(n > )///因为连续600个数中一定有>=2个素数, 1只有一个,所以结果都是No;
{
printf("Case #%d: No\n", t++);
continue;
} for(int i=; i<=n; i++)
G[i].clear(); for(int i=; i<=n; i++)
{
for(int j=; j<=n; j++)
{
if((s+i)%j == )///说明s+i是j的倍数;
G[i].push_back(j);
}
}
memset(used, , sizeof(used));
int ans = ;
for(int i=; i<=n; i++)
{
memset(vis, , sizeof(vis));
ans += Find(i);
}
if(ans != n)///必须要找到所有与之对应的才可以;
printf("Case #%d: No\n", t++);
else
printf("Case #%d: Yes\n", t++);
}
return ;
}

Kingdom of Obsession---hdu5943(二分匹配)的更多相关文章

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

    Kingdom of Obsession Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Oth ...

  2. hdu5943 Kingdom of Obsession 二分图+打表找规律

    题目传送门 Kingdom of Obsession Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Ja ...

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

    Kingdom of Obsession Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Oth ...

  4. HDU 3861 The King’s Problem(tarjan缩点+最小路径覆盖:sig-最大二分匹配数,经典题)

    The King’s Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  5. zoj 2362 Beloved Sons【二分匹配】

    题目:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2361 来源:http://acm.hust.edu.cn/vjudg ...

  6. POJ 1274 The Perfect Stall、HDU 2063 过山车(最大流做二分匹配)

    The Perfect Stall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 24081   Accepted: 106 ...

  7. [kuangbin带你飞]专题十 匹配问题 二分匹配部分

    刚回到家 开了二分匹配专题 手握xyl模板 奋力写写写 终于写完了一群模板题 A hdu1045 对这个图进行 行列的重写 给每个位置赋予新的行列 使不能相互打到的位置 拥有不同的行与列 然后左行右列 ...

  8. BZOJ 1189 二分匹配 || 最大流

    1189: [HNOI2007]紧急疏散evacuate Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1155  Solved: 420[Submi ...

  9. poj 2060 Taxi Cab Scheme (二分匹配)

    Taxi Cab Scheme Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 5710   Accepted: 2393 D ...

随机推荐

  1. ural 1249. Ancient Necropolis

    1249. Ancient Necropolis Time limit: 5.0 secondMemory limit: 4 MB Aerophotography data provide a bit ...

  2. centos 编译 安装 protobuf

    link:http://dbua.iteye.com/blog/1633079 yum -y install gcc+ gcc-c++ yum -y install make 下载protobuf-2 ...

  3. AngularJS学习笔记之directive—scope选项与绑定策略

    From:http://www.linuxidc.com/Linux/2015-05/116924.htm scope:{}使指令与外界隔离开来,使其模板(template)处于non-inherit ...

  4. div基础

    1. 写在后面的样式优于前面,会把前面的覆盖掉! 2.三角形的造法:width:0; height:0;然后设置border-left   border-right  border-top  bord ...

  5. topcoder SRM 619 DIV2 GoodCompanyDivTwo

    注意题目给的最后一句话,如果部门任何employee都做不同类型的工作,则这个部门是一个diverse,题目是计算department的diverse数 读起来感觉有点别扭,英语没学好的原因 int ...

  6. HDU-1466 计算直线的交点数 经典dp

    1.HDU-1466   计算直线的交点数 2.链接:http://acm.hdu.edu.cn/showproblem.php?pid=1466 3.总结:不会推这个,看了题解.. 状态转移: m条 ...

  7. Linux_CentOS6.5安装vncserver实现图形化访问

    一. 安装gnome图形化桌面 #yum groupinstall -y "X Window System" #yum groupinstall -y "Desktop& ...

  8. Log打印日志遇到的问题

    Log日志打印出现空指针问题 AndroidRuntime(372): Caused by: java.lang.NullPointerException: println needs a messa ...

  9. [LintCode] Valid Number 验证数字

    Validate if a given string is numeric. Have you met this question in a real interview? Yes Example & ...

  10. Java 中的 Characters

    1.一般情况下,如果使用单一的 character 值,应该使用原始的 char 类型.比如: public class TestCharacter { public static void main ...