Question link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 5090
Problem descriptiontom 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 ".
Inputthe 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.
Outputfor 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
Source2014 Shanghai national invitational competition-reproduction of the questions (thanks to Shanghai University for providing the questions)
Recommendhujie | we have carefully selected several similar problems for you: 5098 5097 5096 5094
Question:
There are n containers, each containing some pearls.
You can add multiple pearls of K to any container.
Check whether each container can have 1 ~ N pearls.
The Code is as follows:
# Include <cstdio> # include <cstring> # include <algorithm> # include <iostream> using namespace STD; # define maxn 177int N; int G [maxn] [maxn], linker [maxn]; bool used [maxn]; int DFS (int l) // find the augmented path {int R; For (r = 1; r <= N; r ++) // The vertex number starts from 0. To start from 1, modify {If (G [l] [r]! = 0 &&! Used [R]) {// find the augmented path, reverse used [R] = true; If (linker [R] =-1 | DFS (linker [R]) {linker [R] = L; return 1 ;}} return 0; // do not forget this, often forget this sentence} int Hungary () {int res = 0; memset (linker,-1, sizeof (linker); For (int l = 1; L <= N; l ++) {memset (used, 0, sizeof (used); If (DFS (L) RES ++;} return res;} int main () {int t; int K, res, TT; scanf ("% d", & T); While (t --) {scanf ("% d", & N, & K); memset (G, 0, sizeof (g); For (INT I = 1; I <= N; I ++) {scanf ("% d", & TT ); while (TT <= N) {G [TT] [I] = 1; TT + = K;} res = Hungary (); If (RES = N) {printf ("Jerry \ n") ;}else {printf ("Tom \ n") ;}} return 0 ;}
HDU 5090 game with pearls (binary matching)