Game with Pearls
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1914 Accepted Submission(s): 671
Problem Description
Tom and Jerry are playing a game with tubes and pearls. The rule of the game is:
1) Tom and Jerry come up together with a number K.
2) Tom provides N tubes. Within each tube, there are several pearls. The number of pearls in each tube is at least 1 and at most N.
3) Jerry puts some more pearls into each tube. The number of pearls put into each tube has to be either 0 or a positive multiple of K. After that Jerry organizes these tubes in the order that the first tube has exact one pearl, the 2nd tube has exact 2 pearls, …, the Nth tube has exact N pearls.
4) If Jerry succeeds, he wins the game, otherwise Tom wins.
Write a program to determine who wins the game according to a given N, K and initial number of pearls in each tube. If Tom wins the game, output “Tom”, otherwise, output “Jerry”.
1) Tom and Jerry come up together with a number K.
2) Tom provides N tubes. Within each tube, there are several pearls. The number of pearls in each tube is at least 1 and at most N.
3) Jerry puts some more pearls into each tube. The number of pearls put into each tube has to be either 0 or a positive multiple of K. After that Jerry organizes these tubes in the order that the first tube has exact one pearl, the 2nd tube has exact 2 pearls, …, the Nth tube has exact N pearls.
4) If Jerry succeeds, he wins the game, otherwise Tom wins.
Write a program to determine who wins the game according to a given N, K and initial number of pearls in each tube. If Tom wins the game, output “Tom”, otherwise, output “Jerry”.
Input
The first line contains an integer M (M<=500), then M games follow. For each game, the first line contains 2 integers, N and K (1 <= N <= 100, 1 <= K <= N), and the second line contains N integers presenting the number of pearls in
each tube.
Output
For each game, output a line containing either “Tom” or “Jerry”.
Sample Input
2 5 1 1 2 3 4 5 6 2 1 2 3 4 5 5
Sample Output
Jerry Tom
Source
题意:Jerry 和 Tom 玩一个游戏 , 给你 n 个盒子 , a[ i ] 表示開始时 ,
第 i 个盒子中的小球的个数 。
然后 Jerry 能够在每一个盒子里增加 0 或 k的倍数的小球 ,
操作完后,Jerry 能够又一次排列 盒子的顺序,终于使 第 i 个盒子中有 i 个小球。 若Jerry能
使终于的盒子变成那样,就输出 “Jerry” ,否则 输出 “Tom” 。
大神的解释:
仅仅只是我写的和他的建图的方式不太一样,我是用了n+1到2*n来建图,这里仅仅是想更easy懂所以附上大神解释原理是一样的。
这是大神解释的报告链接:点击打开链接
刚開始仅仅是一个劲的模拟,可是水平太次没有模拟出来。看了别人的思路才知道能够用最大匹配还是做题太少啊。
#include<stdio.h> #include<string.h> #define M 1100 int path[M][M],vis[M],used[M]; int n,k; int dfs(int x){ for(int i=n+1;i<=n*2;i++){ if(!vis[i] && path[x][i]){ vis[i]=1; if(used[i]==-1 || dfs(used[i])){ used[i]=x; return 1; } } } return 0; } int main(){ int t,i,j,a; scanf("%d",&t); while(t--){ scanf("%d%d",&n,&k); memset(path,0,sizeof(path)); for(i=1;i<=n;i++){ scanf("%d",&a); for(j=a;j<=n;j+=k){ path[i][j+n]=1;//把这个点多能加到的点都与这个点相连一条边 path[j+n][i]=1; } } int ans=0; memset(used,-1,sizeof(used)); for(i=1;i<=n;i++){ memset(vis,0,sizeof(vis)); ans+=dfs(i); } if(ans==n) printf("Jerry "); else printf("Tom "); } return 0; }