Random Game CodeChef Solution

Problem -Random Game CodeChef Solution

This website is dedicated for CodeChef solution where we will publish right solution of all your favourite CodeChef problems along with detailed explanatory of different competitive programming concepts and languages.

Random Game CodeChef Solution in C++14

#include <bits/stdc++.h>
using namespace std;

bool win(long long n) {
	return (n > 0 and __builtin_ctz(n) % 2 == 0);
}

int main() {
	ios::sync_with_stdio(false); cin.tie(nullptr);
	int t;
	cin >> t;
	while (t--) {
		long long n;
		cin >> n;
		if (!win(n)) {
			cout << "Lose" << endl;
			string inp;
			cin >> inp;
			assert(inp != "WA");
			continue;
		}
		cout << "Win" << endl;
		while (true) {
			if (n % 2 == 0) {
				cout << "/2" << endl;
				n >>= 1;
			}
			else if (!win(n - 1)) {
				cout << "-1" << endl;
				n--;
			}
			else {
				cout << "+1" << endl;
				n++;
			}
			string inp;
			cin >> inp;
			assert(inp != "WA");
			if (inp == "GG") break;
			if (inp == "/2") n >>= 1;
			else if (inp == "+1") n++;
			else n--;
		}
	}
}

Random Game CodeChef Solution in PYTH 3

# cook your dish here
def howToWin(num):
    if num % 2 == 0: return "/2"
    if num == 1: return "-1"
    if num % 4 == 1: return "+1"
    return "-1"

def applyToNum(num, op):
    if op == "/2":
        return num // 2
    if op == "-1":
        return num - 1
    if op == "+1":
        return num + 1

for tea in range(int(input())):
    n = int(input())
    lol = n
    win = True
    while lol % 2 == 0:
        lol = lol // 2
        win = not win
    if not win:
        print("Lose")
        aaa = input()
        if aaa != "GG": break
        else: continue
    print("Win")
    while True:
        print(howToWin(n))
        n = applyToNum(n,howToWin(n))
        ar = input()
        if ar == "GG": break
        n = applyToNum(n,ar)

Random Game CodeChef Solution in C

#include<stdio.h>
#include <stdlib.h>
int mymain()
{
	long long int n,m,i,j,w=1;
	char s[10];
	scanf("%lld",&m);
	n=m;
	while(m%2==0)
	{
		w=-w;
		m/=2;
	}
	if (w==-1)
	{
		printf("Lose\n");fflush(stdout);
		scanf("%s",s);
	//	if (s[0]=='W'&&s[1]=='A')
		//	exit();
	}
	else
	{
		printf("Win\n");fflush(stdout);
		while(1)
		{
			if (n%2==0)
			{
				printf("/2\n");
				n/=2;
			}
			else if (n==1)
			{
			    printf("-1\n");
			    n--;
			}
			else
			{
				if (n%4==3)
				{
					printf("-1\n");
					n-=1;
				}
				else
				{
					printf("+1\n");
					n+=1;
				}
			}
			//fflush(stdin);
			fflush(stdout);
			scanf("%s",s);
			if (s[0]=='/'&&s[1]=='2')
				n/=2;
			else if (s[0]=='G'&&s[1]=='G')
				break;
			else if (s[0]=='-'&&s[1]=='1')
				n-=1;
			else if (s[0]=='+'&&s[1]=='1')
				n+=1;
		//	else if (s[0]=='W'&&s[1]=='A')
			//	exit();
		}
	}
}
int main()
{
	int T;
	scanf("%d",&T);
	while(T--)
		mymain();
}

Random Game CodeChef Solution in JAVA

import java.util.*;
import java.lang.*;
import java.io.*;

public class Main
{
	PrintWriter out = new PrintWriter(System.out);
	BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer tok = new StringTokenizer("");
    String next() throws IOException {
        if (!tok.hasMoreTokens()) { tok = new StringTokenizer(in.readLine()); }
        return tok.nextToken();
    }
    int ni() throws IOException { return Integer.parseInt(next()); }
    long nl() throws IOException { return Long.parseLong(next()); }
    
    long mod=1000000007;
    HashSet<Long>W,L;
    
    void solve() throws IOException {
        for (int tc=ni();tc>0;tc--) {
            long n=nl();
            
            if (solve(n)) {
                System.out.println("Win");
                while (n>0) {
                    if (n==1) { out.println("-1"); n--; }
                    else if (n%2==0) { out.println("/2"); n/=2; }
                    else if (!solve(n-1)) { out.println("-1"); n--; }
                    else { out.println("+1"); n++; }
                    //out.println(n);
                    out.flush();
                    String s=next();
                    if (s.charAt(0)=='W') System.exit(-1);
                    if (s.charAt(0)=='+') n++;
                    if (s.charAt(0)=='-') n--;
                    if (s.charAt(0)=='/') n/=2;
                    if (s.charAt(0)=='G') break;
                }
            }
            else {
                System.out.println("Lose");
                String s=next();
                if (s.charAt(0)=='W') System.exit(0);
            }
        }
        out.flush();
    }
    
    boolean solve (long u) {
        if (u==0) return false;
        if (u%2==1) return true;
        int ct=0;
        while (u%2==0) { ct++; u/=2; }
        if (ct%2==1) return false;
        else return true;
    }
    
    int gcd(int a,int b) { return(b==0?a:gcd(b,a%b)); }
    long gcd(long a,long b) { return(b==0?a:gcd(b,a%b)); }
    long mp(long a,long p) { long r=1; while(p>0) { if ((p&1)==1) r=(r*a)%mod; p>>=1; a=(a*a)%mod; } return r; }
    
    public static void main(String[] args) throws IOException {
        new Main().solve();
    }
}

Random Game CodeChef Solution in C#

using System;

public class Test
{
	public static void Main()
	{
		var t = int.Parse(Console.ReadLine());
        for (int tc = 0; tc < t; tc++)
        {
            var n = ulong.Parse(Console.ReadLine());
            
            //get power of 2 in prime factorization
            var power2n = power2(n);

            //lose
            if (power2n % 2 == 1)
            {
                Console.WriteLine("Lose");
                Console.Out.Flush();
                Console.ReadLine();
            }
            //odd number or even power of 2 (win)
            if (power2n % 2 == 0)
            {
                Console.WriteLine("Win");
                Console.Out.Flush();
                while (n > 0)
                {
                    if (n % 2 == 0)
                    {
                        Console.WriteLine("/2");
                        Console.Out.Flush();
                        n /= 2;
                    }
                    else
                    {
                        if (n - 1 == 0)
                        {
                            Console.WriteLine("-1");
                            Console.Out.Flush();
                            n--;
                        }
                        else if (power2(n + 1) % 2 == 1)
                        {
                            Console.WriteLine("+1");
                            Console.Out.Flush();
                            n++;
                        }
                        else if (power2(n - 1) % 2 == 1)
                        {
                            Console.WriteLine("-1");
                            Console.Out.Flush();
                            n--;
                        }
                    }

                    var other = Console.ReadLine();
                    if (other == "/2")
                    {
                        n /= 2;
                    }
                }
            }
        }
	}
	        static int power2(ulong num)
            {
                var temp = num;
                var ans = 0;
                while (true)
                {
                    if (temp % 2 == 0)
                    {
                        ans++;
                        temp /= 2;
                    }
                    else
                    {
                        break;
                    }
                }

                return ans;
            }
}

Random Game CodeChef Solution in GO

package main

import (
	"bufio"
	"fmt"
	"os"
)

func readInt(bytes []byte, from int, val *int) int {
	i := from
	sign := 1
	if bytes[i] == '-' {
		sign = -1
		i++
	}
	tmp := 0
	for i < len(bytes) && bytes[i] != ' ' {
		tmp = tmp*10 + int(bytes[i]-'0')
		i++
	}
	*val = tmp * sign
	return i
}

func readNum(scanner *bufio.Scanner) (a int) {
	scanner.Scan()
	readInt(scanner.Bytes(), 0, &a)
	return
}

func readTwoNums(scanner *bufio.Scanner) (a int, b int) {
	res := readNNums(scanner, 2)
	a, b = res[0], res[1]
	return
}

func readNNums(scanner *bufio.Scanner, n int) []int {
	res := make([]int, n)
	x := 0
	scanner.Scan()
	for i := 0; i < n; i++ {
		for x < len(scanner.Bytes()) && scanner.Bytes()[x] == ' ' {
			x++
		}
		x = readInt(scanner.Bytes(), x, &res[i])
	}
	return res
}

func fillNNums(scanner *bufio.Scanner, n int, res []int) {
	x := 0
	scanner.Scan()
	for i := 0; i < n; i++ {
		for x < len(scanner.Bytes()) && scanner.Bytes()[x] == ' ' {
			x++
		}
		x = readInt(scanner.Bytes(), x, &res[i])
	}
}

func readUint64(bytes []byte, from int, val *uint64) int {
	i := from

	var tmp uint64
	for i < len(bytes) && bytes[i] != ' ' {
		tmp = tmp*10 + uint64(bytes[i]-'0')
		i++
	}
	*val = tmp

	return i
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	tc := readNum(scanner)

	fn := func() string {
		scanner.Scan()
		return scanner.Text()
	}

	for tc > 0 {
		tc--
		scanner.Scan()
		var N uint64
		readUint64(scanner.Bytes(), 0, &N)
		fail := solve(N, fn)
		if fail {
			break
		}
	}
}

func check(N uint64) bool {
	if N&1 == 1 {
		return true
	}
	if N%4 == 2 {
		// losing position
		return false
	}

	return check(N / 4)
}

func solve(N uint64, fn func() string) bool {

	win := check(N)

	if !win {
		fmt.Println("Lose")
		res := fn()

		return res == "WA"
	}

	fmt.Println("Win")

	var res string
	for N != 0 {
		if N%2 == 0 {
			fmt.Println("/2")
			N /= 2
		} else if N > 1 && check(N-1) {
			fmt.Println("+1")
			N++
		} else {
			fmt.Println("-1")
			N--
		}
		res = fn()
		if res == "WA" {
			return true
		}
		if res == "GG" {
			return false
		}
		if res == "/2" {
			N /= 2
		} else if res == "+1" {
			N++
		} else {
			N--
		}
		if N == 0 {
			// other won, how can it be
			return true
		}
	}
	return false
}
Random Game CodeChef Solution Review:

In our experience, we suggest you solve this Random Game CodeChef Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Random Game CodeChef Solution.

Find on CodeChef

Conclusion:

I hope this Random Game CodeChef Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Programming Language in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

Cognitive Class Answer

CodeChef Solution

Microsoft Learn

Leave a Reply

Your email address will not be published. Required fields are marked *