CHEFYODA : Editorial

Link to the Contest:
https://www.codechef.com/CSPK2020

CHEFYODA : Editorial
Author: CodeChef Public Repository
Tester: easy3279
Editorialist : chaitanya_44
Difficulty : Easy
Problem:

Chef has arrived in Dagobah to meet with Yoda to study cooking. Yoda is a very busy cook and he doesn’t want to spend time with losers. So he challenges the Chef to a series of games, and agrees to teach the Chef if Chef can win at least P of the games. The total number of games is K. The games will be played on a chessboard of size N*M. That is, it has N rows, each of which has M squares. At the beginning of the game, a coin is on square (1, 1), which corresponds to the top-left corner, and every other square is empty. At every step, Yoda and Chef have to move the coin on the chessboard. The player who cannot make a move loses. Chef makes the first move. They can’t move the coin to a square where it had been placed sometime before in the game, and they can’t move outside chessboard.

In this game, there are two different sets of rules according to which the game can be played:
-from (x, y) player can move coin to (x+1, y), (x-1, y), (x, y+1), (x, y-1) in his turn, if they are valid squares.
-from (x, y) player can move coin to (x+1, y+1), (x-1, y-1), (x-1, y+1), (x+1, y-1) in his turn, if they are valid squares.

Before every game, the Power of the kitchen chooses one among the two sets of rules with equal probability of 0.5, and the game will be played according to those rules. Chef and Yoda are very wise, therefore they play optimally. You have to calculate the probability that Yoda will teach Chef.
Input
Input begins with an integer T, the number of test cases
Each test case begins with 4 integers N, M, P, K.

Output
For each test case, output a line containing the answer for task. The output must have an absolute error at most 0.000001 (10-6).

Constraints And Subtasks
1 ≤ T ≤ 50
1 ≤ K

Subtask 1 : 10 points
2 ≤ N, M ≤ 5
0 ≤ P ≤ K ≤ 5

Subtusk 2 : 10 points
2 ≤ N, M ≤ 10
0 ≤ P ≤ K ≤ 10^3

Subtusk 3 : 20 points
2 ≤ N, M ≤ 100
0 ≤ P ≤ K ≤ 10^3

Subtusk 4 : 60 points
2 ≤ N, M ≤ 100
0 ≤ P ≤ K ≤ 10^6

Solution:

CPP 14:
#include “bits/stdc++.h”
using namespace std;

const int N=1e6+20,M=1e2+20;

int t,n,m,p,k;
double f[N];

int main()
{
for(int i=1;i<N;i++) f[i]=f[i-1]+log2(i);

scanf("%d",&t);
while(t--)
{
	scanf("%d%d%d%d",&n,&m,&p,&k);

	bool w1=not((n&1) and (m&1));
	bool w2=not((n&1) or (m&1));

	double ans=0;
	if((w1 and w2) or (p==0)) ans=1;
	else if(not w1 and not w2) ans=0;
	else
	{
		for(int i=p;i<=k;i++) ans+=pow(2,f[k]-f[i]-f[k-i]-k);
	}

	printf("%.6f\n",ans);
}

}

PYTHON 3.6:
import math

dp = []
dp.append(0)
for i in range(1,1000005):
dp.append(math.log(i) + dp[i-1])

t = int(input())
for i in range(t):
n,m,p,k = input().split()
n = int(n)
m = int(m)
p = int(p)
k = int(k)

if p==0 or (n%2==0 and m%2==0):
	ans = 1.0
	print(ans)
elif n%2==1 and m%2==1:
	ans=0.0
	print(ans*100)
else:
	P = 0
	kln2 = k*math.log(2)
	for i in range(p, k+1):
		lnPi = dp[k] - dp[i] - dp[k-i] - kln2
		Pi = pow(math.e, lnPi)
		P += Pi
	print(P)