結果

問題 No.102 トランプを奪え
ユーザー codershifthcodershifth
提出日時 2015-10-02 00:56:48
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 2,115 bytes
コンパイル時間 1,441 ms
コンパイル使用メモリ 161,332 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-27 01:22:33
合計ジャッジ時間 2,265 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

typedef long long ll;
typedef unsigned long long ull;

#define FOR(i,a,b) for(int (i)=(a);i<(b);i++)
#define REP(i,n) FOR(i,0,n)
#define RANGE(vec) (vec).begin(),(vec).end()

using namespace std;


class TakeTrump {
public:
    map<vector<int>,int> memo;
    int grundy(const vector<int>& n) {
            if (memo.count(n))
                return memo[n];
            set<int> S;
            auto N = n;
            REP(i,4)
            FOR(j,1,N[i]+1)
            {
                N[i] -= j;
                S.insert(grundy(N));
                N[i] += j;
            }
            int res = 0;
            while (S.count(res))
                ++res;
            return memo[n] = res;
    }
    void solve(void) {
            // 結局最後にトランプをとった方の勝ちになる。
            // 山がひとつだけのときを考えると
            //
            // 1~3 なら勝ち
            // 4 なら負け
            // 5 なら勝ち -> 4 へ遷移すればよい
            // 6 なら勝ち -> 4
            // 7 なら勝ち -> 4
            // 8 なら負け -> 5,6,7
            // 9 なら勝ち -> 8
            // 10 なら勝ち -> 8
            //  :
            // => 4 の倍数なら負け
            //
            // n 個の山があるとき
            // A が k (1<=k<=4) 個取るとき
            // B は 4-k 個取れば n-k-(4-k) = n-4 となり、 n%4 の結果は変わらない。
            // よって初期状態(で勝敗が決するなら)の山の高さは 0,1,2,3 と考えてよい。
            //
            // Nim として grandy 数を求めて解ける
            //
            vector<int> n(4);
            REP(i,4)
            {
                cin>>n[i];
                n[i] %= 4;
            }
            if (grundy(n))
                cout<<"Taro"<<endl;
            else
                cout<<"Jiro"<<endl;
    }
};

#if 1
int main(int argc, char *argv[])
{
        ios::sync_with_stdio(false);
        auto obj = new TakeTrump();
        obj->solve();
        delete obj;
        return 0;
}
#endif
0