Problem Descriptiontom and Jerry is 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 is several pearls. The number of pearls in each tube are at least 1 and at most N.
3) Jerry puts some more pearls to each tube. The number of pearls put into each tube have to be either 0 or a positive multiple of K. After this Jerry organizes these tubes in the order that the first tube have exact one pearl, the 2nd tube has exact 2 pear LS, ..., 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".
Inputthe first line contains a integer m (m<=500), then M. Games follow. For each game, the first line contains 2 integers, N and K (1 <= n <=, 1 <= K <= N), and the second line C Ontains N integers presenting the number of pearls in each tube.
Outputfor each game, output a line containing either "Tom" or "Jerry".
Sample Input2 5 1 1 2 3 4 5 6 2 1 2 3 4 5 5
Sample Outputjerry Tom
SOURCE2014 Shanghai National Invitational Competition--re-title (thanks to Shanghai University for its topics)
Test instructions: give you n number, (1<=n<=100), and a K (1<=k<=n), and then allow to a certain number plus K of positive integer times, of course, can not add, ask you whether you can turn this n number to,..., N, you can output Jerry, Otherwise output Tom.
Analysis: First directly open a 100 array to record the number of occurrences of each ... If a number appears, then do not move this number, for the number that does not appear, enumeration to add the number (k,2k,3k,... Note the boundary, not more than this number). If there is a number that does not appear and cannot be added by a number of other k, the direct judgment cannot be achieved. Just a simple.
Source: http://mathlover.info/archives/hdu5090
Code:
1#include <iostream>2#include <cstring>3#include <cstdio>4 using namespacestd;5 6 intMain ()7 {8 intm,n,k;9Cin>>m;Ten inta[ the],cnt[102]; One BOOLp[ the]; A while(m--) - { -Cin>>n>>K; thememset (CNT,0,sizeof(CNT)); -Memset (P,0,sizeof(P)); - - for(intI=0; i<n;i++) + { -Cin>>A[i]; + if(p[a[i]]==0)///Judging the signs that appear Ap[a[i]]=1; at Else -cnt[a[i]]++;///extra numbers. - } - - BOOLflag=1;///Mark who can win - for(intI=1; i<=n;i++) in { - if(p[i]==0)///If this number does not appear to { + BOOLok=0; - for(intj=1; i-j*k>=1; j + +)///subtract multiples of k in turn the { * if(Cnt[i-j*k])///if superfluous $ {Panax Notoginsengcnt[i-j*k]--; -ok=1; the Break;///which can be changed by the extra numbers, the inverse process + } A } the if(!ok)///ok==0-------If not found, do this if + { -flag=0; $ Break; $ } - } - } the - if(flag)WuyiPuts"Jerry"); the Else -Puts"Tom"); Wu } - return 0; About}
Game with Pearls