結果

問題 No.458 異なる素数の和
ユーザー bal4ubal4u
提出日時 2019-04-24 07:59:17
言語 C
(gcc 12.3.0)
結果
RE  
実行時間 -
コード長 837 bytes
コンパイル時間 2,472 ms
コンパイル使用メモリ 32,000 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-24 19:31:52
合計ジャッジ時間 8,584 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 AC 1 ms
5,376 KB
testcase_11 RE -
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 RE -
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=2263

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[1] = 2, sz = 2;
	for (i = 3; i <= n; i+=2) if (!prime[i]) tbl[sz++] = i;
}

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

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