【HDU - 5943】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 11Sample Output
Case #1: No Case #2: Yes題目大意:
給定S,N,把S+1,S+2,...S+N這N個數填到1,2,...,N里,要求X只能填到X的因子的位置。(即X%Y=0,那么X才能放在Y位置)。問是否能夠放滿。
解題報告:
首先打表猜出素數的間隔不會超過300,于是乎如果n大于300,那么就是NO,否則的話就是用那些數字去匹配這些位置,考慮匈牙利算法求解匹配問題。
寫完發現怎么都WA,后來發現s==0的時候需要特判,交,WA。然后發現s==1也需要特判,s==2好像也可以特判。。。GG。
賽后知道,因為s和n相差很大的話,中間的都可以抵消掉,所以有多少素數都無所謂,所以可以把s和n互換一下,再去判斷。
AC代碼:
#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define ll long long #define pb push_back #define pm make_pair using namespace std; const int MAX = 300 + 5; double H,h,D; bool bk[MAX]; bool line[MAX][MAX]; int nxt[MAX]; bool used[MAX]; int n,s; bool find(int v) {for(int i = 1; i<=n; i++) {if(!used[i] && line[v][i]) {used[i]=1; if(!nxt[i] || find(nxt[i])) {nxt[i]=v;return 1;}}}return 0 ; } int match(int m) {int res = 0;for(int i = 1; i<=m; i++) {memset(used,0,sizeof used);if(find(i)) res++;}return res; } int main() {int t;cin>>t;int iCase = 0;while(t--) {scanf("%d%d",&n,&s);if(s == 0 || n == 1 || s == 1 || s == 2) {printf("Case #%d: Yes\n",++iCase);continue;}if(s < n) swap(s,n);if(n >= 300) {printf("Case #%d: No\n",++iCase);continue;}ll low = s+1,up = s+n;int flag = 1;memset(line,0,sizeof line);memset(nxt,0,sizeof nxt);for(ll i = low; i<=up; i++) {for(int j = 1; j<=n; j++) {if(i%j==0) line[i-low+1][j] = 1;}}int ans = match(up-low+1);if(ans == n) flag = 1;else flag = 0;if(flag) printf("Case #%d: Yes\n",++iCase);else printf("Case #%d: No\n",++iCase);}return 0 ; }?
總結
以上是生活随笔為你收集整理的【HDU - 5943】Kingdom of Obsession(数论,素数间隔结论,构造,思维,匈牙利算法,匹配问题)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 现在有数字人民币了吗?数字人民币跟比特币
- 下一篇: 【HDU - 5475】An easy