Game with Pearls

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

Source

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

题意:给你n个数,(1<=n<=100),还有一个k(1<=k<=n),然后允许给某一个数加上k的正整数倍,当然可以不加,问你是否可以把这n个数变成1,2,3,...,n,可以就输出Jerry,否则输出Tom。

分析:一开始直接开个100数组去记录每个数出现的次数。。。如果某一个数出现了,那就不用动这个数了,对于没出现的数,枚举要加的数(k,2k,3k,...注意边界,不能超过这个数)。如果存在某一个数没出现而且不能通过别的数加若干个k,直接判断不能实现 。。简简单单就过了

来源:http://mathlover.info/archives/hdu5090

代码:

 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 using namespace std;
 5
 6 int main()
 7 {
 8     int m,n,k;
 9     cin>>m;
10     int a[105],cnt[102];
11     bool p[105];
12     while(m--)
13     {
14         cin>>n>>k;
15         memset(cnt,0,sizeof(cnt));
16         memset(p,0,sizeof(p));
17
18         for(int i=0;i<n;i++)
19         {
20             cin>>a[i];
21             if(p[a[i]]==0)///判断出现的标志
22                 p[a[i]]=1;
23             else
24                 cnt[a[i]]++;///多余的数字
25         }
26
27         bool flag=1;///标记谁能赢
28         for(int i=1;i<=n;i++)
29         {
30             if(p[i]==0)///如果这个数没有出现
31             {
32                 bool ok=0;
33                 for(int j=1;i-j*k>=1;j++)///依次减去k的倍数
34                 {
35                     if(cnt[i-j*k])///若多余
36                     {
37                         cnt[i-j*k]--;
38                         ok=1;
39                         break;///即是可以由多余的数字变化而来,逆过程
40                     }
41                 }
42                 if(!ok)///ok==0-------若果没有找到,则进行此if
43                 {
44                     flag=0;
45                     break;
46                 }
47             }
48         }
49
50         if(flag)
51             puts("Jerry");
52         else
53             puts("Tom");
54     }
55     return 0;
56 }
时间: 2024-10-10 08:50:40

Game with Pearls的相关文章

Hdoj 1300 Pearls 【DP】

Pearls Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 1699    Accepted Submission(s): 778 Problem Description In Pearlania everybody is fond of pearls. One company, called The Royal Pearl, pro

HDU 5090 Game with Pearls(二分匹配)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5090 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, the

HDU - 5009 Paint Pearls(dp+双向链表优化)

Problem Description Lee has a string of n pearls. In the beginning, all the pearls have no color. He plans to color the pearls to make it more fascinating. He drew his ideal pattern of the string on a paper and asks for your help. In each operation,

HDU 5009 Paint Pearls(西安网络赛C题) dp+离散化+优化

转自:http://blog.csdn.net/accelerator_/article/details/39271751 吐血ac... 11668627 2014-09-16 22:15:24 Accepted 5009 1265MS 1980K 2290 B G++ czy   Paint Pearls Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Subm

Paint Pearls

Paint Pearls 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5009 dp+双向链表优化 看到题目,很自然地可以定义状态:dp[i]表示涂好a[0...i]的字符串,花费的最小代价. 状态转移方程:dp[i]=min(dp[i],dp[j]+num2),其中num=从a[j]到a[i]不同的数字个数. 时间复杂度为O(n2),对于n=50000的数据,明显会T. 于是,我们需要进行优化.注意到状态数无法化简,考虑优化转移复杂度. 当区间

hdu 5009 Paint Pearls (dp)

Problem Description Lee has a string of n pearls. In the beginning, all the pearls have no color. He plans to color the pearls to make it more fascinating. He drew his ideal pattern of the string on a paper and asks for your help. In each operation,

HDU5090 Game with Pearls

/* HDU5090 Game with Pearls http://acm.hdu.edu.cn/showproblem.php?pid=5090 匈牙利算法 * */ #include<stdio.h> #include<algorithm> using namespace std; const int Nmax=305; int t,n,k,x,ans; int match[Nmax],book[Nmax],map[Nmax][Nmax]; int init() { ans=

【POJ 1260】Pearls

[POJ 1260]Pearls dp问题 最近做背包做多了 一做动规就往背包想-- 这题其实也有点背包的意思(然而只是做背包做的看啥都像背包-- c件物品 有各自的数量a 和价值p 每进行一次交易的花费cost = (物品数+10)*价格 低价物品可以用高价一起购买 一次交易只能按照一种价值购买 初始dp[0] = 0 dp数组下标为物品件数 枚举物品种类 没枚举一种物品 遍历该物品之前物品量 假设之前有num件物品 当前枚举到的物品价值p 那么就要找到min(dp[k(0~num)] + (

SGU 553 Sultan&#39;s Pearls

题意: 一串珍珠  可以从头或者尾偷窃  但要保证悬挂的珍珠的数量不变  珍珠保持悬挂状态要求重量满足题中的式子  问  最大偷窃多少价值 思路: 关注悬挂的珍珠  由于偷窃从头或者尾进行  所以末状态悬挂的珍珠一定是原串中一段连续的珍珠 那么如果知道悬挂的珍珠是哪一段  就可以利用二分查找桌上放多少珍珠能使得串不滑下去  这样二分的结果前面的珍珠就都可以偷 根据上述分析  可以枚举悬挂的珍珠  然后二分  这样可以确定一个解  不断去更新优的解就好了  复杂度nlogn 对于做法还有2点要补充

HDU 5009 Paint Pearls (动态规划)

Paint Pearls Problem Description Lee has a string of n pearls. In the beginning, all the pearls have no color. He plans to color the pearls to make it more fascinating. He drew his ideal pattern of the string on a paper and asks for your help. In eac