結果
| 問題 |
No.7 プライムナンバーゲーム
|
| コンテスト | |
| ユーザー |
asugen0402
|
| 提出日時 | 2019-03-06 14:30:30 |
| 言語 | C (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 6 ms / 5,000 ms |
| コード長 | 3,467 bytes |
| コンパイル時間 | 267 ms |
| コンパイル使用メモリ | 32,256 KB |
| 実行使用メモリ | 5,248 KB |
| 最終ジャッジ日時 | 2024-10-01 16:17:10 |
| 合計ジャッジ時間 | 919 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 17 |
ソースコード
#include <float.h>
#include <limits.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
// 内部定数
#define D_ON 1 // 汎用フラグ - ON
#define D_OFF 0 // 汎用フラグ - OFF
#define D_VAL_MAX 10005 // 最大値
#define D_PRM_MAX D_VAL_MAX // 最大素数リスト数
// 内部変数
static FILE *szpFpI; // 入力
static int si1Prm[D_PRM_MAX]; // 素数リスト
static char sc1PWork[D_PRM_MAX + 5]; // 素数リスト作成用ワーク
static int siPCnt; // 素数リスト数
static int si1Win[D_VAL_MAX]; // 勝敗[値]
// 内部変数 - テスト用
#ifdef D_TEST
static int siRes;
static FILE *szpFpA;
#endif
// 素数リストの作成 - エラトステネスの篩
int
fMakePrm(
int piMax // <I> 最大値
)
{
// 初期化
siPCnt = 0;
memset(sc1PWork, D_OFF, sizeof(sc1PWork));
// 開始値
int liNow = 2;
// 上限値
int liLimit = (int)sqrt((double)piMax);
// 作成 - 開始
while (1) {
// 素数の検索
while (sc1PWork[liNow] != D_OFF) {
liNow++;
}
// 上限チェック
if (liNow > liLimit) {
break;
}
// 素数リスト - 追加
si1Prm[siPCnt] = liNow;
siPCnt++;
// ワークに素数以外をセット
int liVal = liNow;
while (liVal <= piMax) {
sc1PWork[liVal] = D_ON;
liVal += liNow;
}
}
// 残った素数を追加
while (liNow <= piMax) {
if (sc1PWork[liNow] == D_OFF) {
si1Prm[siPCnt] = liNow;
siPCnt++;
}
liNow++;
}
return 0;
}
// 勝敗 - セット
int
fSetWin(
int piVal // 値
)
{
int i, liRet;
// 勝
if (piVal < 2) {
return 1;
}
// セット済
if (si1Win[piVal] != 0) {
return si1Win[piVal];
}
// 全素数を試す
for (i = 0; i < siPCnt; i++) {
if (piVal < si1Prm[i]) {
break;
}
// 相手へ
liRet = fSetWin(piVal - si1Prm[i]);
if (liRet == -1) { // 負
si1Win[piVal] = 1; // 勝
return 1;
}
}
// 負
si1Win[piVal] = -1;
return -1;
}
// 実行メイン
int
fMain(
int piTNo // <I> テスト番号 1~
)
{
char lc1Buf[1024], lc1Out[1024];
// データ - 初期化
memset(si1Win, 0, sizeof(si1Win)); // 勝敗
// 入力 - セット
#ifdef D_TEST
sprintf(lc1Buf, ".\\Test\\T%d.txt", piTNo);
szpFpI = fopen(lc1Buf, "r");
sprintf(lc1Buf, ".\\Test\\A%d.txt", piTNo);
szpFpA = fopen(lc1Buf, "r");
siRes = 0;
#else
szpFpI = stdin;
#endif
// 開始値 - 取得
int liVal;
fgets(lc1Buf, sizeof(lc1Buf), szpFpI);
sscanf(lc1Buf, "%d", &liVal);
// 素数リストの作成
fMakePrm(liVal);
// 勝敗 - セット
fSetWin(liVal);
// 結果 - セット
if (si1Win[liVal] == 1) {
sprintf(lc1Out, "Win\n");
}
else {
sprintf(lc1Out, "Lose\n");
}
// 結果 - 表示
#ifdef D_TEST
fgets(lc1Buf, sizeof(lc1Buf), szpFpA);
if (strcmp(lc1Buf, lc1Out)) {
siRes = -1;
}
#else
printf("%s", lc1Out);
#endif
// 残データ有無
#ifdef D_TEST
lc1Buf[0] = '\0';
fgets(lc1Buf, sizeof(lc1Buf), szpFpA);
if (strcmp(lc1Buf, "")) {
siRes = -1;
}
#endif
// テストファイルクローズ
#ifdef D_TEST
fclose(szpFpI);
fclose(szpFpA);
#endif
// テスト結果
#ifdef D_TEST
if (siRes == 0) {
printf("OK %d\n", piTNo);
}
else {
printf("NG %d\n", piTNo);
}
#endif
return 0;
}
int
main()
{
#ifdef D_TEST
int i;
for (i = D_TEST_SNO; i <= D_TEST_ENO; i++) {
fMain(i);
}
#else
fMain(0);
#endif
return 0;
}
asugen0402