結果
| 問題 |
No.7 プライムナンバーゲーム
|
| コンテスト | |
| ユーザー |
commy
|
| 提出日時 | 2018-08-10 17:16:33 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 100 ms / 5,000 ms |
| コード長 | 1,702 bytes |
| コンパイル時間 | 887 ms |
| コンパイル使用メモリ | 82,808 KB |
| 実行使用メモリ | 5,248 KB |
| 最終ジャッジ日時 | 2024-10-01 16:13:23 |
| 合計ジャッジ時間 | 2,223 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 17 |
ソースコード
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <functional>
#define REP(i, a, b) for (int i = int(a); i < int(b); i++)
#define dump(val) cerr << __LINE__ << ":\t" << #val << " = " << (val) << endl
using namespace std;
typedef long long int lli;
template<typename T>
vector<T> make_v(size_t a, T b) {
return vector<T>(a, b);
}
template<typename... Ts>
auto make_v(size_t a, Ts... ts) {
return vector<decltype(make_v(ts...))>(a, make_v(ts...));
}
int main() {
int N;
cin >> N;
vector<bool> isPrime(N + 1, true);
isPrime[0] = isPrime[1] = false;
vector<int> Prime;
REP(i, 0, N + 1) {
if (isPrime[i]) {
Prime.push_back(i);
for (int j = i * 2; j < N + 1; j += i) {
isPrime[j] = false;
}
}
}
auto dp = make_v(N + 1, 2, -1);
function<int(int, int)> rec = [&](int n, int turn) -> int {
if (dp[n][turn] != -1) {
return dp[n][turn];
}
if (n == 0 || n == 1) {
return (turn == 0);
}
bool res;
if (turn == 0) {
res = false;
REP(i, 0, Prime.size()) {
if (Prime[i] > n) {
break;
}
res |= rec(n - Prime[i], !turn);
}
} else {
res = false;
REP(i, 0, Prime.size()) {
if (Prime[i] > n) {
break;
}
res |= (!rec(n - Prime[i], !turn));
}
res = !res;
}
return dp[n][turn] = res;
};
cout << (rec(N, 0) ? "Win" : "Lose") << endl;
return 0;
}
commy