結果

問題 No.726 Tree Game
ユーザー ゆにぽけゆにぽけ
提出日時 2024-03-06 05:26:20
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 3 ms / 2,000 ms
コード長 1,898 bytes
コンパイル時間 1,558 ms
コンパイル使用メモリ 138,288 KB
実行使用メモリ 6,676 KB
最終ジャッジ日時 2024-03-06 05:26:23
合計ジャッジ時間 2,809 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
testcase_03 AC 2 ms
6,676 KB
testcase_04 AC 1 ms
6,676 KB
testcase_05 AC 2 ms
6,676 KB
testcase_06 AC 2 ms
6,676 KB
testcase_07 AC 2 ms
6,676 KB
testcase_08 AC 2 ms
6,676 KB
testcase_09 AC 2 ms
6,676 KB
testcase_10 AC 2 ms
6,676 KB
testcase_11 AC 2 ms
6,676 KB
testcase_12 AC 2 ms
6,676 KB
testcase_13 AC 2 ms
6,676 KB
testcase_14 AC 1 ms
6,676 KB
testcase_15 AC 2 ms
6,676 KB
testcase_16 AC 1 ms
6,676 KB
testcase_17 AC 2 ms
6,676 KB
testcase_18 AC 2 ms
6,676 KB
testcase_19 AC 2 ms
6,676 KB
testcase_20 AC 2 ms
6,676 KB
testcase_21 AC 3 ms
6,676 KB
testcase_22 AC 3 ms
6,676 KB
testcase_23 AC 2 ms
6,676 KB
testcase_24 AC 2 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <array>
#include <iterator>
#include <string>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <cassert>
#include <cmath>
#include <ctime>
#include <iomanip>
#include <numeric>
#include <stack>
#include <queue>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <bitset>
#include <random>
#include <utility>
#include <functional>
using namespace std;
bool MillerRabin(long long N)
{
	if(N <= 1) return false;
	if(N == 2) return true;
	if(N % 2 == 0) return false;
	vector<long long> A;
	if(N < 4759123141LL)
	{
		A = {2,7,61};
	}
	else
	{
		A = {2, 325, 9375, 28178, 450775, 9780504, 1795265022};
	}

	auto mult = [&N](long long x,long long y) -> long long
	{
		return (__int128_t) x * y % N;
	};

	auto mod_pow = [&mult](long long p,long long n) -> long long
	{
		long long res = 1;
		while(n)
		{
			if(n & 1) res = mult(res,p);
			n >>= 1;
			p = mult(p,p);
		}
		return res;
	};

	long long S = 0,D = N - 1;
	while(D % 2 == 0)
	{
		S++;
		D >>= 1;
	}

	for(const long long &a : A)
	{
		if(N <= a) return true;
		long long t;
		long long x = mod_pow(a,D);

		if(x == 1);
		else
		{
			for(t = 0;t < S;t++)
			{
				if(x == N - 1) break;
				x = mult(x,x); 
			}

			if(t == S) return false;
		}
	}

	return true;
}

void Main()
{
	int Y,X;
	cin >> Y >> X;
	map<pair<int,int>,int> dp;

	auto dfs = [&](auto dfs,int y,int x) -> int
	{
		if((y != Y || x != X) && (MillerRabin(y) || MillerRabin(x)))
		{
			return 1;
		}
		pair<int,int> P = make_pair(y,x);
		if(dp.find(P) != dp.end())
		{
			return dp[P];
		}
		if(!dfs(dfs,y + 1,x) || !dfs(dfs,y,x + 1))
		{
			return dp[P] = 1;
		}
		return dp[P] = 0;
	};
	cout << (dfs(dfs,Y,X) ? "First\n" : "Second\n");
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int tt = 1;
	/* cin >> tt; */
	while(tt--) Main();
}
0