結果

問題 No.7 プライムナンバーゲーム
ユーザー cwwcww
提出日時 2016-12-05 02:57:55
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 294 ms / 5,000 ms
コード長 1,435 bytes
コンパイル時間 1,710 ms
コンパイル使用メモリ 170,208 KB
実行使用メモリ 394,496 KB
最終ジャッジ日時 2024-04-09 04:05:33
合計ジャッジ時間 7,646 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 287 ms
394,368 KB
testcase_01 AC 292 ms
394,496 KB
testcase_02 AC 288 ms
394,240 KB
testcase_03 AC 288 ms
394,496 KB
testcase_04 AC 286 ms
394,240 KB
testcase_05 AC 287 ms
394,496 KB
testcase_06 AC 290 ms
394,496 KB
testcase_07 AC 287 ms
394,368 KB
testcase_08 AC 288 ms
394,368 KB
testcase_09 AC 289 ms
394,368 KB
testcase_10 AC 289 ms
394,496 KB
testcase_11 AC 285 ms
394,368 KB
testcase_12 AC 288 ms
394,368 KB
testcase_13 AC 290 ms
394,368 KB
testcase_14 AC 287 ms
394,368 KB
testcase_15 AC 294 ms
394,240 KB
testcase_16 AC 292 ms
394,496 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
struct cww{cww(){ios::sync_with_stdio(false);cin.tie(0);}}star;
int N;
vector<int> P;
vector<vector<int>> visit( 10001, vector<int>(10001, -1 ) );//<---初期化!!!
void prime( int N )
{
	for( int i = 2; i <= N; i++ )
	{
		bool flag = true;
		for( int j = 2; j * j <= i; j++ )
		{
			if( i % j == 0 )
			{
				flag = false;
				break;
			}
		}
		if( flag )
		{
			P.emplace_back( i );
		}
	}
	reverse( P.begin(), P.end() );
}
bool dfs( const int T, const int N )
{
	//0 か 1 を受け取ったのが先手番だったらtrueを返す( = Win )
	if( N == 0 || N == 1 )
	{
		return T == 0 ? 1 : 0;
	}
	//visit が 0 以上だったら訪問済
	if( visit[ T ][ N ] >= 0 )
	{
		//記録を返す
		return visit[ T ][ N ];
	}
	if ( T == 0 )
	{
		for( const auto &x : P )
		{
			if( N - x >= 0 )
			{
				//T, N に対しての戻り値を記憶
				visit[ T ][ N ] = dfs( 1, N - x );
				//先手勝ち(1/true)が1つでもあれば先手勝ち(true)
				if( visit[ T ][ N ] ) return 1;
			}
		}
		return 0;
	}
	else
	{
		for( const auto &x : P )
		{
			if( N - x >= 0 )
			{
				//T, N に対しての戻り値を記憶
				visit[ T ][ N ] = dfs( 0, N - x );
				//1つでも負け(0/false)があったら負け(false)
				if( !visit[ T ][ N ] ) return 0;
			}
		}
		return 1;
	}
}
int main()
{
	cin >> N;

	prime( N );

	cout <<  ( dfs( 0, N ) ? "Win" : "Lose" ) << endl;

	return 0;
}
0