結果

問題 No.458 異なる素数の和
ユーザー bal4ubal4u
提出日時 2019-04-23 22:47:46
言語 C
(gcc 12.3.0)
結果
RE  
実行時間 -
コード長 833 bytes
コンパイル時間 313 ms
コンパイル使用メモリ 32,512 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-24 00:46:58
合計ジャッジ時間 5,579 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 AC 82 ms
5,376 KB
testcase_06 RE -
testcase_07 RE -
testcase_08 AC 81 ms
5,376 KB
testcase_09 RE -
testcase_10 RE -
testcase_11 AC 75 ms
5,376 KB
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 RE -
testcase_28 WA -
testcase_29 RE -
testcase_30 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

// yukicoder: No.458 異なる素数の和
// 2019.4.23 bal4u

#include <stdio.h>
#include <string.h>
#include <math.h>

#define MAX  20000
#define SQRT 141	     // sqrt(MAX)
char prime[MAX+2] = {1,1,0,0,1,0,1,0,1,1,0};       // 0:素数
int tbl[2300], sz;       // sz=2262

int a[MAX+2];
int b[MAX+2];

void sieve(int n)
{
	int i, j, b;
	b = (int)sqrt((double)n);
	for (i = 3; i <= b; i += 2) {
		for (j = i*i; j <= n; j += i) prime[j] = 1;
	}
	tbl[0] = 2, sz = 1;
	for (i = 3; i <= MAX; i+=2) if (!prime[i]) tbl[sz++] = i;
}

int main()
{
	int i, j, N;

	scanf("%d", &N);
	sieve(N);
	a[N] = 1;
	memset(b, -1, sizeof(b));
	for (i = N; i >= 0; i--) if (a[i]) {
		for (j = b[i]+1; j < sz; j++) {
			if (i-tbl[j] >= 0) a[i-tbl[j]] = a[i]+1, b[i-tbl[j]] = j;
		}
	}
	if (a[0] == 0) puts("-1");
	else printf("%d\n", a[0]-1);
	return 0;
}
0