Friday, 2 December 2016

Mojo, Jojo and Game

Mojo, Jojo and Game
After solving some programming problems, Mojo and Jojo want to take some time off and play some game. They ask Rhezo what to play. Rhezo tells them about a game with the following rules:
There are N stacks of coins, each containing X coins. Both players take turns, with Mojo moving first. The only allowed moves are the following:
  1. Remove any stack of coin.
  2. Remove any 2 stacks.
  3. Remove X/2 coins from each stack if X is even, or remove (X+1)/2 coins from each pile, if X is odd.
The game finishes when there are no more coins left in any stack. The last person to make the move wins. Both, Mojo and Jojo play optimally and want to win. Can you tell who will win this game?
Input:
First line of the input contains an integer T, denoting the number of test cases you have to handle.
Each test case consists of two integers, N and X, separated by a single space.
Output:
For each test case, if Mojo wins, print "Mojo", else print "Jojo" (quotes only for clarity).
Constraints:
  • 1T103
  • 1N103
  • 1X109
SAMPLE INPUT
1
4 5
SAMPLE OUTPUT
Jojo
Explanation
One possible flow of the game is as follows:
 There are 4 stacks with 5 coins each, Mojo removes 2 stacks.
 There are 2 stacks left now with 5 coins each, Jojo removes 2 stacks, and wins.
We can show that for any possible flow of the game, Jojo will always win.

--------------------------------------------------editorial-----------------------------------------------------------------------------

 this problem is all about  state optimization ...

Let us have a function isWinning(N,X) that tells us if the current state is winning or not if there are N stacks left and each stack has X coins.
One important observation is that there are only a very small number of distinct values that all stacks can take for a particular starting XThis small number is upper bounded by log2(X)Let us keep all these distinct values in an array A.
let value  of X is 16 than instead of 16 use it as 4 ans each time instead of dividing x by 2 decreasing value of x  by 1 
Let us redefine the state (N,X) as (N,P), where P denotes the the index of different values that we can obtain from Xin array A.
Then we can easily write a DP solution. A state is winning, if we can go to at least one state that is losing. A state is losing, if we can only go to winning states from it.
The base conditions are as follows: if N is 0 or P is not a valid index, then it is a losing state.
Refer to the author's solution for a proper implementation of the isWinning function.

======================================code============================================
#include<bits/stdc++.h>
using namespace std;
typedef long long int lli;
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define mod 1000000007


bool dp[100][1010];
bool used[100][1010];
bool solve(int height,int n)
{
// cout<<height<<" "<<n<<endl;
 if(height==0) return false;
 else if(n==0) return false;
 else if(used[height][n]) return dp[height][n];
 else
 {
  used[height][n]=true;
  bool ans=false;
   ans= ans| (!solve(height-1,n));
    ans= ans| (!solve(height,n-1));
     if(n>=2)
     ans= ans| (!solve(height,n-2));
     dp[height][n]=ans;
     return ans;
 }
}

int main()
{
 int t;
  cin>>t;
  while(t--)
  {
      memset(dp,false,sizeof dp);
   memset(used,false,sizeof used);
    int n,x;
     cin>>n>>x;
     int height=0;
     while(x!=0)
       {
       if(x&1) x=x-(x+1)/2;
else x/=2;
height++;
 }
// cout<<"height is "<<height<<endl;
 
bool ans=solve(height,n);
if(ans) cout<<"Mojo"<<endl;
else cout<<"Jojo"<<endl;
  }
}

Wednesday, 2 March 2016

**Kitty and Katty

Kitty and Katty

Kitty and Katty have N plastic blocks. They label the blocks with sequential numbers from 1to N and begin playing a game in turns, with Kitty always taking the first turn. The game's rules are as follows:
  • For each turn, the player removes 2 blocks, A and B, from the set. They calculate AB, write the result on a new block, and insert the new block into the set.
  • The game ends when only 1 block is left. The winner is determined by the value written on the final block, X:
    • If X%3=1, then Kitty wins.
    • If X%3=2, then Katty wins.
    • If X%3=0, then the player who moved last wins.
Recall that % is the Modulo Operation.
Given the value of N, can you find and print the name of the winner? Assume that both play optimally.
Note: The selection order for A and B matters, as sometimes ABBA. The diagram below shows an initial set of blocks where N=5. If A=2 and B=3, then the newly inserted block is labeled 1; alternatively, if A=3 and B=2, the newly inserted block is labeled 1.
Input Format
The first line contains a single positive integer, T (the number of test cases or games). 
The T subsequent lines each contain an integer, N (the number of blocks for that test case).
Constraints
  • 1T100
  • 1N105
Output Format
For each test case, print the name of the winner (i.e.: either Kitty or Katty) on a new line.
Sample Input
2
2
3
Sample Output
Kitty
Katty
Explanation
Test Case 0: 
N=2 so there are two blocks labeled 1 and 2. Kitty chooses A=2 and B=1, then inserts a new block with the label 1 (the result of 21). The game ends, as there is now only 1 block in the set. The label on the last block, X, is 1, so we calculate result=1 % 3=1. Becauseresult=1, Kitty wins and we print Kitty on a new line.
Test Case 1: 
N=3, so there are three blocks labeled 12, and 3. No matter how Kitty makes the first move, Katty will win. If Kitty chooses A=3 and B=2 on the first move and inserts a block labeled 1 (the result of 32), the set of blocks becomes {1,1}. Katty then must choose A=1 and B=1 and insert a new block labeled 0 (the result of 11). The game ends, as there is now only 1 block in the set. The label on the last block, X, is 0, so we calculate result=0 % 3=0. Because result=0 and Katty made the last move, Katty wins and we print Katty on a new line.

---------------------------------------editorial------------------------------------

Special Case


Let us first deal with the special case. When `N=1`, there is only one element in the array, and nobody can make a move. Kitty wins since the remainder is `1`.

Solution


For any arbitary `N>1`, imagine the game is being played. Eventually, the game will come to a situation when there are exactly two elements in the array, `A` and `B`. Since the game is decided by the final value modulo `3`, we can say that `a=A%3` and `b=B%3` and play with these two numbers instead. It will not affect the game state.

Now, if `a=b`, then this player can simply replace `a,b` with `ab=0`. The final value will be `0`, and this player will win.

If they are not equal, then this player will still win. This player wins because she can choose the value of her move where either `1` or `2` is left as the final value.

Suppose `a=1` and `b=2`. If this player wants to have the final value as `1`, then she will just choose `2` and `1` and replace them with `21=1`. If she wants `2`, then she will chose `1` and `2` and replace them with `12=1`.

So, the player who makes the last move always wins. If `N` is even, then Kitty wins, otherwise Katty wins.
-------------------------------------------------code--------------------------------------------
#include<bits/stdc++.h>
using namespace std;
int main(){
    int T;
    cin >> T;
    for(int a0 = 0; a0 < T; a0++){
        int n;
        cin >> n;
        if(n==1) cout<<"Kitty"<<endl;
        else if(n%2==0) cout<<"Kitty"<<endl;
        else cout<<"Katty"<<endl;
    }
    return 0;
}