Header Ad

HackerEarth Mishki Playing Games problem solution

In this HackerEarth Mishki, Playing Games problem solution Mishki loves playing games, so she asked her friend Hacker to join her in the Game of Stones. In this game, they have N sets of stones, numbered from 1 to N. Each set consists of Ai stones, where 1 <= i <= N.
In each turn, the player can select any of the sets containing at least 1 stone and have to reduce it to half of the present number of stones, i.e Ai/ 2 (Integer Division) from it, and in the case of a single stone, he/she has to empty the set by removing it.
As the game is really interesting, both will play this game on Q days. On each day, they will some select sets of stones numbered from l to r, where 1 <= l <= N and being a lover of the games, every day Mishki will be the first player to take the turn.
The one who won't be able to play his/her turn in the game (i.e no stones left in the selected set of stones), will lose the game. You need to tell the winner of the game on each day if both the player will play optimally and take their turn alternatively.


HackerEarth Mishki Playing Games problem solution


HackerEarth Mishki Playing Games problem solution.

#include <iostream>
#include <cstdio>
#include <cassert>
#define ll long long
using namespace std;

const int ma = 1e6+6;
int a[ma];
ll prefix_sum[ma];

int check_log(int x)
{
int c=0;
while(x)
{
x>>=1;
c++;
}
return c;
}

int main()
{

int n,q;
scanf("%d%d",&n,&q);

for(int i=0;i<n;i++)
scanf("%d",&a[i]);

prefix_sum[0] = check_log(a[0]);
for(int i=1;i<n;i++)
{
prefix_sum[i] = prefix_sum[i-1]+ check_log(a[i]);
}

int l,r;
for(int i=0;i<q;i++)
{
scanf("%d%d",&l,&r);
l-=1;
r-=1;
if(l==0)
{
if(prefix_sum[r]%2)
printf("Mishki\n");
else
printf("Hacker\n");
}
else
{
ll tp = prefix_sum[r]- prefix_sum[l-1];

if(tp%2)
printf("Mishki\n");
else
printf("Hacker\n");
}
}
return 0;

}

Post a Comment

0 Comments