結果

問題 No.385 カップ麺生活
ユーザー hanorverhanorver
提出日時 2016-07-01 23:29:15
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 5 ms / 2,000 ms
コード長 1,304 bytes
コンパイル時間 722 ms
コンパイル使用メモリ 76,220 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-15 07:27:58
合計ジャッジ時間 1,765 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <iostream> 
#include <vector>
#include <algorithm>

bool is_prime(int n, std::vector<int> primes) {
	for (int i = 0; i < primes.size(); i++) {
		if (n % primes[i] == 0) return false;
	}
	return true;
}

void prime_list(int n, std::vector<int> &primes) {
	if (n == 1) return;
	primes.push_back(2);
	if (n == 2) return;
	primes.push_back(3);

	for (int i = 5; i <= n; i += 2) {
		if (is_prime(i, primes)) {
			primes.push_back(i);
		}
	}
}


int main() {
	int m, n;

	std::cin >> m >> n;

	std::vector<int> item(n);
	for (int i = 0; i < n; i++) {
		std::cin >> item[i];
	}

	// dp[i] = i円で購入できるカップ麺の最大個数
	int dp[10000 + 1] = { 0 };

	for (int i = 0; i < n; i++) {
		dp[item[i]] = 1;
	}

	for (int i = 0; i < n; i++) {
		for (int j = 0; j <= m; j++) {
			if (dp[j] > 0 && j + item[i] <= m) {
				dp[j + item[i]] = std::max(dp[j] + 1, dp[j + item[i]]);
			}
		}
	}

	std::vector<int> primes;
	prime_list(m, primes);

	//for (int i = 0; i <= m; i++) {
	//	std::cout << i << " "<<dp[i] << std::endl;
	//}

	long long ans = 0;

	for (int i = 0; i < primes.size(); i++) {
		ans += dp[m - primes[i]];
		//std::cout << dp[m - primes[i]] << std::endl;
	}

	if (!is_prime(m, primes)) {
		ans += *std::max_element(dp, dp+m+1);
	}

	std::cout << ans << std::endl;

	return 0;
}
0