結果

問題 No.7 プライムナンバーゲーム
コンテスト
ユーザー data9824
提出日時 2015-06-09 02:16:31
言語 C++11
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=gnu++11 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 4 ms / 5,000 ms
コード長 831 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 580 ms
コンパイル使用メモリ 80,620 KB
実行使用メモリ 6,400 KB
最終ジャッジ日時 2026-04-17 17:17:17
合計ジャッジ時間 1,245 ms
ジャッジサーバーID
(参考情報)
judge1_1 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 17
権限があれば一括ダウンロードができます
コンパイルメッセージ
In file included from /home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/string:53,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/bits/locale_classes.h:42,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/bits/ios_base.h:43,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/ios:46,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/bits/ostream.h:43,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/ostream:42,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/iostream:43,
                 from main.cpp:1:
In function 'void std::__fill_a1(_ForwardIterator, _ForwardIterator, const _Tp&) [with _ForwardIterator = bool*; _Tp = bool]',
    inlined from 'void std::__fill_a(_FIte, _FIte, const _Tp&) [with _FIte = bool*; _Tp = bool]' at /home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/bits/stl_algobase.h:979:21,
    inlined from 'void std::fill(_ForwardIterator, _ForwardIterator, const _Tp&) [with _ForwardIterator = bool*; _Tp = bool]' at /home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/bits/stl_algobase.h:1011:20,
    inlined from 'int main()' at main.cpp:26:6:
/home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/bits/stl_algobase.h:925:18: warning: 'void* __builtin_memset(void*, int, long unsigned int)' writing 10002 bytes into a region of size 10001 overflows the destination [-Wstringop-overflow=]
  925 |         *__first = __val;
      |         ~~~~~~~~~^~~~~~~
main.cpp: In function 'int main()':
main.cpp:7:6: note: destination object 'isPrime' of size 10001
    7 | bool isPrime[10001];
      |      ^~~~~~~
In function 'void std::__fill_a1(_ForwardIterator, _ForwardIterator, const _Tp&) [with _ForwardIterator = int*; _Tp = int]',
    inlined from 'void std::__fill_a(_FIte, _FIte, const _Tp&) [with _FIte = int*; _Tp = in

ソースコード

diff #
raw source code

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

bool isPrime[10001];
vector<int> primes;
int cache[10001];

bool turn(int n) {
	if (cache[n] != -1) {
		return cache[n] ? true : false;
	}
	bool win = false;
	for (size_t i = 0; 
		i < primes.size() && n - primes[i] > 1 && !win;
		++i) {
		win |= !turn(n - primes[i]);
	}
	cache[n] = win ? 1 : 0;
	return win;
}

int main() {
	fill(isPrime, isPrime + 10001 + 1, true);
	isPrime[0] = false;
	isPrime[1] = false;
	for (int i = 2; i <= 10000; ++i) {
		if (isPrime[i]) {
			for (int k = 2 * i; k <= 10000; k += i) {
				isPrime[k] = false;
			}
		}
	}
	for (int i = 0; i <= 10000; ++i) {
		if (isPrime[i]) {
			primes.push_back(i);
		}
	}
	fill(cache, cache + 10001 + 1, -1);
	int n;
	cin >> n;
	cout << (turn(n) ? "Win" : "Lose") << endl;
	return 0;
}
0