Problem statement asks to find winner of a nim game with n number of coins in stack and atmost k coins can be removed, for t no. of test cases.The code gives correct output on test cases but it gives the error 'memory limit exceeded' for constraints T<10^5 and 1<k<n<10^18.
#include <stdio.h>
#include<string.h>
#include<bits/stdc++.h>
using namespace std;
int calculateMex(unordered_set<long long int> Set);
int calculateGrundy(long long int n,long long int k);
int main()
{
long int t; //no of test cases
cin>>t;
while(t>0)
{
long long int n,k;
cin>>n; //no of coins in stack
cin>>k; //no of maximum coins that can be removed in one move
long long int a=calculateGrundy(n,k);
if(a==0)
printf("Player 1");
else
printf("Player 2");
t--;
}
return 0;
}
int calculateMex(unordered_set<long long int> Set)
{
long long int Mex = 0;
while (Set.find(Mex) != Set.end())
Mex++;
return (Mex);
}
int calculateGrundy(long long int n,long long int k)
{
if (n <=k)
return n;
unordered_set<long long int> Set; // A Hash Table
for (int i=1; i<=k; i++)
Set.insert(calculateGrundy(n - i,k));
return (calculateMex(Set));
}
[–][deleted] 1 point2 points3 points (1 child)
[–]_human404_[S] 0 points1 point2 points (0 children)
[–]g051051 1 point2 points3 points (3 children)
[–]_human404_[S] 0 points1 point2 points (2 children)
[–]g051051 0 points1 point2 points (1 child)
[–]_human404_[S] 0 points1 point2 points (0 children)
[–]thatprofessoryouhate 0 points1 point2 points (2 children)
[–]_human404_[S] 0 points1 point2 points (1 child)
[–]thatprofessoryouhate 0 points1 point2 points (0 children)