結果

問題 No.458 異なる素数の和
ユーザー kakakakanekokakakakaneko
提出日時 2018-04-15 11:23:08
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 44 ms / 2,000 ms
コード長 1,247 bytes
コンパイル時間 752 ms
コンパイル使用メモリ 95,004 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-09 05:39:52
合計ジャッジ時間 2,416 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 14 ms
4,376 KB
testcase_02 AC 18 ms
4,380 KB
testcase_03 AC 4 ms
4,376 KB
testcase_04 AC 5 ms
4,380 KB
testcase_05 AC 38 ms
4,380 KB
testcase_06 AC 17 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 38 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 44 ms
4,376 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 3 ms
4,380 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 1 ms
4,384 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 AC 2 ms
4,376 KB
testcase_23 AC 1 ms
4,380 KB
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 2 ms
4,380 KB
testcase_26 AC 2 ms
4,380 KB
testcase_27 AC 16 ms
4,380 KB
testcase_28 AC 44 ms
4,380 KB
testcase_29 AC 2 ms
4,376 KB
testcase_30 AC 10 ms
4,380 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: 関数 ‘int main()’ 内:
main.cpp:44:45: 警告: iteration 20010 invokes undefined behavior [-Waggressive-loop-optimizations]
   44 |         for (int i = 0;i <= 20010;i++)dp[i] = -1;
      |                                       ~~~~~~^~~~
main.cpp:44:26: 備考: within this loop
   44 |         for (int i = 0;i <= 20010;i++)dp[i] = -1;
      |                        ~~^~~~~~~~
main.cpp:44:45: 警告: ‘void* __builtin_memset(void*, int, long unsigned int)’ writing 80044 bytes into a region of size 80040 overflows the destination [-Wstringop-overflow=]
   44 |         for (int i = 0;i <= 20010;i++)dp[i] = -1;
      |                                       ~~~~~~^~~~
main.cpp:41:16: 備考: destination object ‘dp’ of size 80040
   41 |         int m, dp[20010] = { 0 };
      |                ^~

ソースコード

diff #

#include <iostream>
#include <string>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <map>
#include <cmath>
#include <queue>
#include <utility>
#include <functional>
#include <deque>
#include <cctype>
#include <stack>
#include <bitset>
#include <set>

using ll = long long;
using namespace std;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

const ll MOD = 1000000007;
const ll INF = 1 << 30;
const ll INF2 = 9000000000000000000LL;
const double INF3 = 900000000000000;
const int dx[4] = { 1,0,-1,0 }, dy[4] = { 0,1,0,-1 };
const int tx[8] = { -1,0,1,-1,1,-1,0,1 }, ty[8] = { -1,-1,-1,0,0,1,1,1 };
#define ALL(x) (x).begin(),(x).end()

bool bpr[20010];

void sieve(int n) {
	for (int i = 0;i <= n;i++)bpr[i] = true;
	bpr[0] = bpr[1] = false;
	for (int i = 2;i <= n;i++) {
		if (bpr[i]) for (int j = i * 2;j <= n;j += i)bpr[j] = false;
	}
}

int main() {
	int m, dp[20010] = { 0 };
	vector<int>p;
	cin >> m;
	for (int i = 0;i <= 20010;i++)dp[i] = -1;
	dp[0] = 0;
	sieve(20010);
	for (int i = 0;i <= m;i++) {
		if (bpr[i])p.push_back(i);
	}
	for (int i = 0;i < p.size();i++) {
		for (int j = m;j >= 0;j--) {
			if (p[i] + j <= m&& dp[j] != -1)dp[p[i] + j] = max(dp[p[i] + j], dp[j] + 1);
		}
	}
	cout << dp[m] << endl;
}
0