結果

問題 No.7 プライムナンバーゲーム
ユーザー data9824data9824
提出日時 2015-06-09 02:16:31
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 5 ms / 5,000 ms
コード長 831 bytes
コンパイル時間 592 ms
コンパイル使用メモリ 63,384 KB
実行使用メモリ 6,820 KB
最終ジャッジ日時 2024-10-01 15:33:21
合計ジャッジ時間 1,044 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 1 ms
6,816 KB
testcase_02 AC 5 ms
6,820 KB
testcase_03 AC 2 ms
6,816 KB
testcase_04 AC 2 ms
6,820 KB
testcase_05 AC 2 ms
6,820 KB
testcase_06 AC 3 ms
6,820 KB
testcase_07 AC 2 ms
6,820 KB
testcase_08 AC 2 ms
6,816 KB
testcase_09 AC 3 ms
6,816 KB
testcase_10 AC 2 ms
6,816 KB
testcase_11 AC 2 ms
6,820 KB
testcase_12 AC 4 ms
6,820 KB
testcase_13 AC 4 ms
6,816 KB
testcase_14 AC 5 ms
6,820 KB
testcase_15 AC 5 ms
6,816 KB
testcase_16 AC 4 ms
6,820 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
In file included from /usr/include/c++/11/bits/char_traits.h:39,
                 from /usr/include/c++/11/ios:40,
                 from /usr/include/c++/11/ostream:38,
                 from /usr/include/c++/11/iostream:39,
                 from main.cpp:1:
In function ‘typename __gnu_cxx::__enable_if<std::__is_scalar<_Tp>::__value, void>::__type 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 /usr/include/c++/11/bits/stl_algobase.h:969:21,
    inlined from ‘void std::fill(_ForwardIterator, _ForwardIterator, const _Tp&) [with _ForwardIterator = bool*; _Tp = bool]’ at /usr/include/c++/11/bits/stl_algobase.h:999:20,
    inlined from ‘int main()’ at main.cpp:26:6:
/usr/include/c++/11/bits/stl_algobase.h:924:18: warning: ‘void* __builtin_memset(void*, int, long unsigned int)’ writing 10002 bytes into a region of size 10001 overflows the destination [-Wstringop-overflow=]
  924 |         *__first = __tmp;
      |         ~~~~~~~~~^~~~~~~
main.cpp: In function ‘int main()’:
main.cpp:7:6: note: destination object ‘isPrime’ of size 10001
    7 | bool isPrime[10001];
      |      ^~~~~~~
In file included from /usr/include/c++/11/bits/char_traits.h:39,
                 from /usr/include/c++/11/ios:40,
                 from /usr/include/c++/11/ostream:38,
                 from /usr/include/c++/11/iostream:39,
                 from main.cpp:1:
In function ‘typename __gnu_cxx::__enable_if<std::__is_scalar<_Tp>::__value, void>::__type 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 = int]’ at /usr/include/c++/11/bits/stl_algobase.h:969:21,
    inlined from ‘void std::fill(_ForwardIterator, _ForwardIterator, const _Tp&) [with _ForwardIterator

ソースコード

diff #

#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