(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;}
- (2016年中国大学生程序设计竞赛(杭州) )HDU 5943 Kingdom of Obsession 素数间距 + 匈牙利算法
- HDU 5943 Kingdom of Obsession 【二分图匹配 匈牙利算法】 (2016年中国大学生程序设计竞赛(杭州))
- HDU 5943 Kingdom of Obsession(2016年中国大学生程序设计竞赛(杭州)),好题
- 【2016-CCPC-K】匈牙利算法,最大基数匹配(Kingdom of Obsession,hdu 5943)
- 2016年中国大学生程序设计竞赛(杭州)-重现赛
- 2016年中国大学生程序设计竞赛(杭州)-重现赛
- 2016年中国大学生程序设计竞赛(杭州)
- 2016年中国大学生程序设计竞赛(杭州) 部分题解
- HDU5938+贪心+(2016年中国大学生程序设计竞赛(杭州))
- HDU-5943-Kingdom of Obsession(杭州CCPC K题)-数学推导+匈牙利算法优化
- HDU 5934 Bomb 【图论缩点】(2016年中国大学生程序设计竞赛(杭州))
- HDU 5935 Car 【模拟】 (2016年中国大学生程序设计竞赛(杭州))
- HDU 5938 Four Operations 【贪心】(2016年中国大学生程序设计竞赛(杭州))
- HDU 5936 Difference 【中途相遇法】(2016年中国大学生程序设计竞赛(杭州))
- HDU 5935 Car (贪心)——2016年中国大学生程序设计竞赛(杭州)
- HDU 5938 Four Operations(2016年中国大学生程序设计竞赛(杭州))
- HDU 5937 Equation 【DFS+剪枝】 (2016年中国大学生程序设计竞赛(杭州))
- (2016年中国大学生程序设计竞赛(杭州)) HDU 5938 Four Operations 思维题 + 枚举
- 解决navicat for mysql报错:1130is…
- Linux启动Mysql数据库
- java时区-DateFormat和TimeZone关…
- Java的日期格式化
- Javaweb项目中使用Log4j记录日志
- (2016年中国大学生程序设计竞赛(杭州) )HDU 5943 Kingdom of Obsession 素数间距 + 匈牙利算法
- 设置Log4j 日志文件存放位置
- MATLAB的符号计算
- MATLAB 积分求解
- jQuery 解析xml文件
- JVM性能调优
- 手算以及使用线性神经网络和感知器…
- [BZOJ4596][Shoi2016]黑暗前的幻想乡-Matrix Tree 矩阵树定理
- 线性神经网络用于求解异或逻辑