211人阅读 评论(0)

# Game with Pearls

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2205    Accepted Submission(s): 806

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”.

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 

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<queue>
#include<stack>
using namespace std;
typedef long long ll;
const int N=555;///两边的最大数量
bool tu[N][N];
int from[N];///记录右边的点如果配对好了它来自哪里
bool use[N];///记录右边的点是否已经完成了配对
int n,m;///m,n分别表示两边的各自数量,n是左边，m是右边
bool dfs(int x)
{
for(int i=1; i<=m; i++) ///m是右边，所以这里上界是m
if(!use[i]&&tu[x][i])
{
use[i]=1;
if(from[i]==-1||dfs(from[i]))
{
from[i]=x;
return 1;
}
}
return 0;
}
int hungary()
{
int tot=0;
memset(from,-1,sizeof(from));
for(int i=1; i<=n; i++) ///n是左边，所以这里上界是n
{
memset(use,0,sizeof(use));
if(dfs(i))
tot++;
}
}

int main()
{
int t;
scanf("%d",&t);
while(t--)
{
memset(tu,0,sizeof(tu));
int x,k;
scanf("%d%d",&x,&k);
for(int i=1; i<=x; i++)
{
int tt,num=0;
scanf("%d",&tt);
while(tt+num*k<=x)
tu[i][tt+num++*k]=1;
}
n=m=x;
int ans=hungary();
if(ans<x)
puts("Tom");
else
puts("Jerry");
}
return 0;
}


0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人说明

即便如此，

我们也在自己

选择的道路上

稳步前行！

个人资料
• 访问：112965次
• 积分：4362
• 等级：
• 排名：第7282名
• 原创：335篇
• 转载：8篇
• 译文：0篇
• 评论：2条
最爱JAY
博客专栏
 状态压缩dp 文章：13篇 阅读：3613
 图论 文章：16篇 阅读：13049