結果

問題 No.7 プライムナンバーゲーム
ユーザー polylogKpolylogK
提出日時 2017-09-09 00:21:31
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 41 ms / 5,000 ms
コード長 885 bytes
コンパイル時間 710 ms
コンパイル使用メモリ 71,644 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-09 04:23:01
合計ジャッジ時間 1,710 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,820 KB
testcase_01 AC 1 ms
6,944 KB
testcase_02 AC 41 ms
6,944 KB
testcase_03 AC 4 ms
6,944 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,948 KB
testcase_06 AC 13 ms
6,944 KB
testcase_07 AC 8 ms
6,944 KB
testcase_08 AC 4 ms
6,948 KB
testcase_09 AC 18 ms
6,944 KB
testcase_10 AC 2 ms
6,948 KB
testcase_11 AC 8 ms
6,948 KB
testcase_12 AC 29 ms
6,944 KB
testcase_13 AC 31 ms
6,944 KB
testcase_14 AC 41 ms
6,948 KB
testcase_15 AC 39 ms
6,944 KB
testcase_16 AC 36 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//https://yukicoder.me/problems/25
//https://yukicoder.me/submissions/194289
#include <iostream>
#include <math.h>
#include <cstdio>
#include <algorithm>
#include <string>
#include <string.h>
using namespace std;

const int MAX_N=10000;
int n;
bool IsWin[MAX_N+1]; //lose(0), win(1)
bool p[MAX_N+1]; //not prime(0), prime(1)

int main(){
	cin >> n;
	
	memset(p, true, sizeof(p));
	for(int i=0;i<2;i++) p[i]=false;
	for(int i=0;i<4;i++) IsWin[i]=false;
	p[2]=true;
	
	
	for(int i=3;i<=n;i++){
		for(int j=2;j*j<=i;j++){
			if(p[j]){
				if(i%j){
					continue;
				}else{
					p[i]=false;
				}
			}
		} 
	}
	
	for(int i=4;i<=n;i++){
		for(int j=2;j<=i-2;j++){
			if(p[j] && !IsWin[i-j]){ 
				//j減らした時にIsWin[i-j]がfalseなら相手が勝てないので自分は勝てる
				IsWin[i]=true;
				break;
			}
		}
	}
	
	cout << (IsWin[n]?"Win":"Lose") << endl;
	
	return 0;
}
0