2016杭州ccpc

 

Kingdom of Obsession

标签: 二分图最大匹配
  51人阅读  评论(2)  收藏  举报
  分类:

Kingdom of Obsession

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

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
1T100 .
1n109 .
0s109 .
 

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

题意:我想来到这的都不用讲什么题意的吧。

思路:估计也都能想到利用二分图最大匹配来确定是不是能够全匹配,重点是n很大啊。

不过我们知道素数有个特性就是因数只有1和它本身。所以素数只能待在位置1或者标号为本身的位置上

1.如果m>n


如果n大于等于两个素数之间的距离,那么肯定是不可能匹配的。

如果小于直接二分图匹配一下。

2.如果m<=n


黄色部分长度为m如果m的长度大于等于两个素数的间隔那肯定是不符合题意。

如果小于那么就把m部分二分图匹配,匹配的话肯定符合题意,不匹配的话不符合题意,至于为什么不和重合部分匹配,看评论。

这两个一综合就会发现第二种如果mn互换和第一种是一样的。

至于二分图匹配:

就是n个点之间的匹配,如果两个点之间满足等式就连一条边,最后看看能不能全匹配上就可以了。

[cpp]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. #include <iostream>  
  2. #include <cstdio>  
  3. #include <algorithm>  
  4. #include <cmath>  
  5. #include <queue>  
  6. #include <vector>  
  7. #include <cstring>  
  8. #include <string>  
  9. using namespace std;  
  10. const int MAXN=300+7;  
  11.   
  12. int n,m;  
  13. int vis[MAXN],match[MAXN];  
  14. int num[MAXN];  
  15. int head[MAXN];  
  16. int cnt;  
  17. struct node  
  18. {  
  19.     int v;  
  20.     int next;  
  21. } edge[MAXN*MAXN];  
  22.   
  23. void add(int u,int v);  
  24. int dfs(int u);  
  25. int build()  
  26. {  
  27.     cnt=0;  
  28.     memset(match,-1,sizeof(match));  
  29.     int i,j,v;  
  30.     for(i=1; i<=n; ++i)head[i]=-1;  
  31.     for(i=1; i<=n; ++i)  
  32.     {  
  33.         match[i]=-1;  
  34.         for(j=1; j<=n; ++j)  
  35.         {  
  36.             v=j+m;  
  37.             if(v%i==0)add(i,j);  
  38.         }  
  39.     }  
  40.     int sum=0;  
  41.     for(i=1; i<=n; ++i)  
  42.     {  
  43.         memset(vis,0,sizeof(vis));  
  44.         if(dfs(i))sum++;  
  45.     }  
  46.     return sum;  
  47. }  
  48. void add(int u,int v)  
  49. {  
  50.     edge[cnt].v=v;  
  51.     edge[cnt].next=head[u];  
  52.     head[u]=cnt++;  
  53. }  
  54. int dfs(int u)  
  55. {  
  56.     int i;  
  57.     for(i=head[u]; i!=-1; i=edge[i].next)  
  58.     {  
  59.         int v=edge[i].v;  
  60.         if(!vis[v])  
  61.         {  
  62.             vis[v]=1;  
  63.             if(match[v]==-1||dfs(match[v]))  
  64.             {  
  65.                 match[v]=u;  
  66.                 return 1;  
  67.             }  
  68.         }  
  69.     }  
  70.     return 0;  
  71. }  
  72. int main()  
  73. {  
  74.     int t;  
  75.     scanf("%d",&t);  
  76.     for(int t1=1; t1<=t; ++t1)  
  77.     {  
  78.         scanf("%d%d",&n,&m);  
  79.         printf("Case #%d: ",t1);  
  80.         if(m<n)swap(m,n);  
  81.         if(n>300)puts("No");  
  82.         else  
  83.         {  
  84.             int ans=build();  
  85.             if(ans==n)puts("Yes");  
  86.             else puts("No");  
  87.         }  
  88.   
  89.     }  
  90.     return 0;  
  91. }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值