hdu5943 Kingdom of Obsession 二分图+打表找规律
Posted zhgyki
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hdu5943 Kingdom of Obsession 二分图+打表找规律相关的知识,希望对你有一定的参考价值。
题目传送门
Kingdom of Obsession
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3051 Accepted Submission(s): 919
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
Is there any way to satisfy everyone‘s requirement?
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.
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‘.
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
Recommend
题意:给你n和s,问你s后面n个数能够完全跟1-n匹配,(s+i)与i匹配,
如果(s+i)%i==0(1<=n)
题解:首先n个数中要是出现两个质数那肯定匹配不成功,然后我们通过打表发现
1-1e8每两个相隔质数的最大距离不超过300,但是这里有一个情况,就是
当n>s时,是可以容许有两个质数的,比如:n=4,s=3,这里3和5是可以的,其实解决
的方法就是交换n和s的值,因为求n=4,s=3和n=3,s=4是一样的,就是把一样的数自己
给匹配掉而已,最后当n<300的时候就是一个二分如完美匹配的问题了
代码:
#include<iostream> #include<string.h> #include<algorithm> #include<stdio.h> #include<math.h> #include<queue> #include<stack> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef long long ll; typedef pair<int,int> PII; #define mod 1000000007 #define pb push_back #define mp make_pair #define all(x) (x).begin(),(x).end() #define fi first #define se second //head #define MAX 1005 int t; ll n,m; ll a[MAX],b[MAX]; ll nxt[MAX],used[MAX]; bool Find(int x) { for(int i=1;i<=m;i++) { if(!used[i]&&a[x]%b[i]==0) { used[i]=1; if(nxt[i]==0||Find(nxt[i])) { nxt[i]=x; return true; } } } return false; } ll match() { ll sum=0; for(int i=1;i<=n;i++) { memset(used,0,sizeof(used)); if(Find(i)) sum++; } return sum; } int main() { cin>>t; int k=0; while(t--) { memset(nxt,0,sizeof(nxt)); int d,dd; cin>>d>>dd; if(d>dd) swap(d,dd); cout<<"Case #"<<++k<<": "; if(d>1000) cout<<"No"<<endl; else { n=m=d; for(int i=1;i<=d;i++) a[i]=++dd; for(int i=1;i<=d;i++) b[i]=i; if(match()==n) cout<<"Yes"<<endl; else cout<<"No"<<endl; } } return 0; }
#include<iostream> #include<string.h> #include<algorithm> #include<stdio.h> #include<math.h> #include<queue> #include<stack> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef long long ll; typedef pair<int,int> PII; #define mod 1000000007 #define pb push_back #define mp make_pair #define all(x) (x).begin(),(x).end() #define fi first #define se second //head #define MAX 1005 int t; int n; vector<int >G[MAX]; int nxt[MAX],used[MAX]; bool Find(int x) { for(int i=0;i<G[x].size();i++) { int v=G[x][i]; if(!used[v]) { used[v]=1; if(nxt[v]==0||Find(nxt[v])) { nxt[v]=x; return true; } } } return false; } int match() { int sum=0; for(int i=1;i<=n;i++) { memset(used,0,sizeof(used)); if(Find(i)) sum++; } return sum; } int main() { scanf("%d",&t); int k=0; while(t--) { for(int i=1;i<=n;i++) G[i].clear(); memset(nxt,0,sizeof(nxt)); int d,dd; scanf("%d %d",&d,&dd); if(d>dd) swap(d,dd); printf("Case #%d: ",++k); if(d>300) printf("No "); else { n=d; for(int i=1;i<=d;i++) { int tmp=dd+i; for(int j=1;j<=n;j++) { if(tmp%j==0) G[j].push_back(i); } } if(match()==n) printf("Yes "); else printf("No "); } } return 0; }
以上是关于hdu5943 Kingdom of Obsession 二分图+打表找规律的主要内容,如果未能解决你的问题,请参考以下文章
HDU 5943 Kingdom of Obsession 二分图的匹配
HDU5943 Kingdom of Obsession(思路题+二分匹配)
HDU 5943 Kingdom of Obsession 二分图匹配 匈牙利算法 (中国大学生程序设计竞赛(杭州))