結果

問題 No.7 プライムナンバーゲーム
ユーザー assy1028
提出日時 2015-03-20 20:04:13
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 15 ms / 5,000 ms
コード長 1,193 bytes
コンパイル時間 869 ms
コンパイル使用メモリ 82,892 KB
実行使用メモリ 6,824 KB
最終ジャッジ日時 2024-10-01 15:30:28
合計ジャッジ時間 1,363 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 17
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:58:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   58 |     scanf("%d", &n);
      |     ~~~~~^~~~~~~~~~

ソースコード

diff #

#include <algorithm>
#include <iostream>
#include <cstdio>
#include <map>
#include <numeric>
#include <cmath>
#include <set>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <complex>
#include <string.h>
using namespace std;

#define endl '\n'
#define all(v) (v).begin(), (v).end()
#define uniq(v) (v).erase(unique((v).begin(), (v).end()), (v).end())

typedef long long ll;
typedef pair<int, int> P;
typedef unsigned int uint;

const int inf = 1000000009;

bool is_prime[10010];
vector<int> primes;
int memo[10010];

void sieve(int n) {
    memset(is_prime, true, sizeof(is_prime));
    is_prime[0] = is_prime[1] = false;
    for (int i = 2; i <= n; i++) {
	if (is_prime[i]) {
	    primes.push_back(i);
	    for (int j = i + i; j <= n; j += i)
		is_prime[j] = false;
	}
    }
}

bool solve(int n) {
    if (n < 2) return true;
    if (memo[n] == 0) {
	int idx = 0;
	bool win = false;
	while (idx < primes.size() && primes[idx] <= n) {
	    win = (win || !solve(n-primes[idx]));
	    idx++;
	}
	memo[n] = (win ? 1 : -1);
    }
    return memo[n] > 0;
}

int main() {
    int n;
    scanf("%d", &n);
    sieve(n);
    printf("%s\n", solve(n) ? "Win" : "Lose");
}
0