Time Limit: 1000MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64u

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

 

Source

2014上海全国邀请赛――题目重现(感谢上海大学提供题目)


分析:贪心即可,升序排序然后扫一遍,如果当前数等于下标就检查下一个,比下标大则Tom赢,比下标小就加上k然后重复上述过程。所有数都扫过了就Jerry赢。也有二分图匹配的做法。

AC代码:

#include <cstdio>
#include <algorithm>
using namespace std;
int n, k, a[110];
void input()
{
	scanf("%d%d", &n, &k);
	for (int i = 1; i <= n; i++)
		scanf("%d", &a[i]);
}
bool cmp(const int x, const int y)
{
	return x < y;
}
void solve()
{
	bool flag = 1, ok = 0;
	while (!ok)
	{
		sort(a + 1, a + 1 + n, cmp);
		for (int i = 1; i <= n; i++)
		{
			if (a[i] > i)
			{
				flag = 0;
				ok = 1;
				break;
			}
			if (a[i] != i)
			{
				a[i] += k;
				break;
			}
			if (i == n)
				ok = 1;
		}
	}
	if (flag)
		printf("Jerry\n");
	else
		printf("Tom\n");
}
int main()
{
	int T;
	scanf("%d", &T);
	while (T--)
	{
		input();
		solve();
	}
	return 0;
}

 

分类: ACM/ICPC

发表评论

电子邮件地址不会被公开。 必填项已用*标注