Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cmath>
- #include <cstdio>
- #include <vector>
- #include <iostream>
- #include <algorithm>
- #include <bits/stdc++.h>
- using namespace std;
- unordered_map<string, int> memo;
- // returns {0, 1} as per if A or B wins.
- int solve(int n, int currPlayer) {
- string key = to_string(n) + "|" + to_string(currPlayer);
- if(memo.count(key)) return memo[key];
- if(n==0) {
- // currPlayer loses
- memo[key] = !currPlayer;
- return !currPlayer;
- }
- int option1 = !currPlayer, option2 = !currPlayer, option3 = !currPlayer;
- if (n>=1) {
- // currPlayer can take 1 item
- option1 = solve(n-1, !currPlayer);
- }
- if (n>=2) {
- option2 = solve(n-2, !currPlayer);
- }
- if (n>=4) {
- option3 = solve(n-4, !currPlayer);
- }
- if(option1 == currPlayer || option2 == currPlayer || option3 == currPlayer) {
- memo[key] = currPlayer;
- }
- else {
- memo[key] = !currPlayer;
- return !currPlayer;
- }
- return memo[key];
- }
- int main() {
- int T; cin >> T;
- while(T--) {
- int n; cin >> n;
- int answer = solve(n, 0);
- if(answer == 0) cout << "A" << endl;
- else cout << "B" << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement