5-95 Fall 08 # Games and Nmbers A. Game 0.5 seconds, 64 megabytes There s a legend n the IT Cty college. A student that faled to answer all questons on the game theory exam s gven one more chance by hs professor. The student has to play a game wth the professor. The game s played on a square feld consstng of n n cells. Intally all cells are empty. On each turn a player chooses and pant an empty cell that has no common sdes wth prevously panted cells. Adjacent corner of panted cells s allowed. On the next turn another player does the same, then the frst one and so on. The player wth no cells to pant on hs turn loses. The professor have chosen the feld sze n and allowed the student to choose to be the frst or the second player n the game. What should the student choose to wn the game? Both players play optmally. 8 The only lne of the contans one nteger n ( n 0 ) the sze of the feld. number, f the player makng the frst turn wns when both players play optmally, otherwse prnt number.
B. Industral Nm seconds, 64 megabytes There are n stone quarres n Petrograd. Each quarry owns m dumpers ( n). It s known that the frst dumper of the -th quarry has x stones n t, the second dumper has x + stones n t, the thrd has x +, and the m-th dumper (the last for the -th quarry) has x + m - stones n t. Two olgarchs play a well-known game Nm. Players take turns removng stones from dumpers. On each turn, a player can select any dumper and remove any non-zero amount of stones from t. The player who cannot take a stone loses. Your task s to fnd out whch olgarch wll wn, provded that both of them play optmally. The olgarchs asked you not to reveal ther names. So, let's call the one who takes the frst stone «tolk» and the other one «bolk». The frst lne of the contans one nteger number n ( n 05 ) the amount of quarres. Then there follow n lnes, each of them contans two space-separated ntegers x and m ( x, m 06 ) the amount of stones n the frst dumper of the -th quarry and the number of dumpers at the -th quarry. «tolk» f the olgarch who takes a stone frst wns, and «bolk» otherwse. 3 tolk 4 bolk
C. Game of Stones 3 seconds, 56 megabytes Sam has been teachng Jon the Game of Stones to sharpen hs mnd and help hm devse a strategy to fght the whte walkers. The rules of ths game are qute smple: The game starts wth n ples of stones ndexed from to n. The -th ple contans s stones. The players make ther moves alternatvely. A move s consdered as removal of some number of stones from a ple. Removal of 0 stones does not count as a move. The player who s unable to make a move loses. Now Jon beleves that he s ready for battle, but Sam does not thnk so. To prove hs argument, Sam suggested that they play a modfed verson of the game. In ths modfed verson, no move can be made more than once on a ple. For example, f 4 stones are removed from a ple, 4 stones cannot be removed from that ple agan. Sam sets up the game and makes the frst move. Jon beleves that Sam s just tryng to prevent hm from gong to battle. Jon wants to know f he can wn f both play optmally. Frst lne conssts of a sngle nteger n ( n 06 ) the number of ples. Each of next n lnes contans an nteger s ( s 60) the number of stones n -th ple. Prnt a sngle lne contanng "YES" (wthout quotes) f Jon wns, otherwse prnt "NO" (wthout quotes) 5 NO YES In the frst case, Sam removes all the stones and Jon loses. In second case, the followng moves are possble by Sam: In each of these cases, last move can be made by Jon to wn the game as follows:
D. Sagheer and Apple Tree seconds, 56 megabytes Sagheer s playng a game wth hs best frend Solman. He brought a tree wth n nodes numbered from to n and rooted at node. The -th node has a apples. Ths tree has a specal property: the lengths of all paths from the root to any leaf have the same party (.e. all paths have even length or all paths have odd length). Sagheer and Solman wll take turns to play. Solman wll make the frst move. The player who can't make a move loses. In each move, the current player wll pck a sngle node, take a non-empty subset of apples from t and do one of the followng two thngs:. eat the apples, f the node s a leaf.. move the apples to one of the chldren, f the node s non-leaf. Before Solman comes to start playng, Sagheer wll make exactly one change to the tree. He wll pck two dfferent nodes u and v and swap the apples of u wth the apples of v. Can you help Sagheer count the number of ways to make the swap (.e. to choose u and v) after whch he wll wn the game f both players play optmally? (u, v) and (v, u) are consdered to be the same par. 5 The frst lne wll contan one nteger n ( n 0 ) the number of nodes n the apple tree. The second lne wll contan n ntegers a, a,..., a ( a 0 ) the number of apples on each node of the tree. The thrd lne wll contan n - ntegers p, p 3,..., p n ( p n) the parent of each node of the tree. Node has parent p (for n). Node s the root of the tree. n It s guaranteed that the descrbes a vald tree, and the lengths of all paths from the root to any leaf wll have the same party. On a sngle lne, prnt the number of dfferent pars of nodes (u, v), u v such that f they start playng after swappng the apples of both nodes, Sagheer wll wn the game. (u, v) and (v, u) are consdered to be the same par. 7
3 3 3 3 0 8 7 5 4 3 4 4 5 6 4 In the frst sample, Sagheer can only wn f he swapped node wth node 3. In ths case, both leaves wll have apples. If Solman makes a move n a leaf node, Sagheer can make the same move n the other leaf. If Solman moved some apples from a root to a leaf, Sagheer wll eat those moved apples. Eventually, Solman wll not fnd a move. In the second sample, There s no swap that wll make Sagheer wn the game. Note that Sagheer must make the swap even f he can wn wth the ntal tree.
E. Permutaton Game second, 56 megabytes After a long day, Alce and Bob decded to play a lttle game. The game board conssts of cells n a straght lne, numbered from to n, where each cell contans a number a between and n. Furthermore, no two cells contan the same number. A token s placed n one of the cells. They take alternatng turns of movng the token around the board, wth Alce movng frst. The current player can move from cell to cell only f the followng two condtons are satsfed: the number n the new cell j must be strctly larger than the number n the old cell (.e. a j > a ), and the dstance that the token travels durng ths turn must be a multple of the number n the old cell (.e. j mod a = 0). Whoever s unable to make a move, loses. For each possble startng poston, determne who wns f they both play optmally. It can be shown that the game s always fnte,.e. there always s a wnnng strategy for one of the players. The frst lne contans a sngle nteger n ( n 0 5 ) the number of numbers. The second lne contans n ntegers a, a,, a n ( a n). Furthermore, there are no par of ndces such that a a j. Prnt s a strng of n characters, where the -th character represents the outcome of the game f the token s ntally placed n the cell. If Alce wns, then s has to be equal to "A"; otherwse, s has to be equal to "B". = 8 3 6 5 4 7 8 BAAAABAB j n j 5 3 5 0 9 7 3 5 8 4 6 4 ABAAAABBBAABAAB In the frst sample, f Bob puts the token on the number (not poston): : Alce can move to any number. She can wn by pckng 7, from whch Bob has no move. 3 5 5 8 3 wns, as Bob has only a move to 4, from whch Alce can move to 8. 3: Alce can only move to 4, after whch Bob wns by movng to 8. 4, 5, or 6: Alce wns by movng to 8. 7, 8: Alce has no move, and hence she loses mmedately. : Alce can move to and. Upon movng to, Bob can wn by movng to. If she chooses nstead, she
F. Playng wth Strng seconds, 56 megabytes Two people play the followng strng game. Intally the players have got some strng s. The players move n turns, the player who cannot make a move loses. Before the game began, the strng s wrtten on a pece of paper, one letter per cell. An example of the ntal stuaton at s = "abacaba" A player's move s the sequence of actons:. The player chooses one of the avalable peces of paper wth some strng wrtten on t. Let's denote t s t. Note that ntally, only one pece of paper s avalable.. The player chooses n the strng t = t t... t character n poston ( t ) such that for some postve t nteger l (0 < - l; + l t ) the followng equatons hold: t = t, t = t,..., t = t. - + - + - l + l 3. Player cuts the cell wth the chosen character. As a result of the operaton, he gets three new peces of paper, the frst one wll contan strng tt... t - t, the thrd one contans strng t t... t. + + t, the second one wll contan a strng consstng of a sngle character An example of makng acton ( = 4) wth strng s = «abacaba» Your task s to determne the wnner provded that both players play optmally well. If the frst player wns, fnd the poston of character that s optmal to cut n hs frst move. If there are multple postons, prnt the mnmal possble one. The frst lne contans strng s ( s 5000). It s guaranteed that strng s only contans lowercase Englsh letters. If the second player wns, prnt n the sngle lne "Second" (wthout the quotes). Otherwse, prnt n the frst lne "Frst" (wthout the quotes), and n the second lne prnt the mnmal possble wnnng move nteger ( s ). abacaba Frst abcde Second In the frst sample the frst player has multple wnnng moves. But the mnmum one s to cut the character n poston. In the second sample the frst player has no avalable moves.