結果

問題 No.458 異なる素数の和
ユーザー megasharesmegashares
提出日時 2018-09-13 14:17:19
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 111 ms / 2,000 ms
コード長 941 bytes
コンパイル時間 432 ms
コンパイル使用メモリ 35,504 KB
実行使用メモリ 261,836 KB
最終ジャッジ日時 2023-09-13 19:50:23
合計ジャッジ時間 2,377 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
9,420 KB
testcase_01 AC 48 ms
149,388 KB
testcase_02 AC 56 ms
169,956 KB
testcase_03 AC 20 ms
75,192 KB
testcase_04 AC 22 ms
83,436 KB
testcase_05 AC 98 ms
246,104 KB
testcase_06 AC 53 ms
165,848 KB
testcase_07 AC 5 ms
17,652 KB
testcase_08 AC 96 ms
248,156 KB
testcase_09 AC 11 ms
44,388 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 111 ms
261,836 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 14 ms
56,748 KB
testcase_17 AC 2 ms
5,292 KB
testcase_18 AC 2 ms
5,240 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 2 ms
5,176 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 1 ms
4,376 KB
testcase_23 AC 2 ms
5,248 KB
testcase_24 AC 2 ms
5,248 KB
testcase_25 AC 1 ms
4,376 KB
testcase_26 AC 2 ms
5,244 KB
testcase_27 AC 52 ms
161,608 KB
testcase_28 AC 108 ms
259,712 KB
testcase_29 AC 8 ms
31,964 KB
testcase_30 AC 39 ms
128,724 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

int snt(int a, int arr[], int max) {
	int i;

	for (i = 1; i <= max; i++) {
		if (a == arr[i]) {
			return 1;
		}
	}

	return 0;
}

int findMax(int a, int b) {
	return a > b ? a : b;
}

int f[20000][30000] = {};
const int inf = -INT_MAX;

int main() {
	int n;
	int i, j;
	int max;
	int check[21000] = {};
	int arr[20000] = {};


	scanf("%d", &n);

	for (i = 2; i <= sqrt(n); i++) {
		for (j = 2; j <= n / i; j++) {
			check[i*j] = 1;
		}
	}

	max = 1;
	for (i = 2; i <= n; i++) {
		if (!check[i]) arr[max++] = i;
	}
	max--;

	for (i = 1; i <= n; i++) {
		f[0][i] = inf;
	}

	for (i = 1; i <= max; i++) {
		for (j = 0; j <= n; j++) {
			if (j - arr[i] >= 0) {
				f[i][j] = findMax(f[i - 1][j], f[i - 1][j - arr[i]] + 1);
			}
			else {
				f[i][j] = f[i - 1][j];
			}
		}
	}


	int result = (f[max][n] <0 ? -1 : f[max][n]);
	printf("%d", result);
	getchar(); getchar();

	return 0;
}
0