結果

問題 No.458 異なる素数の和
ユーザー olphe
提出日時 2016-12-09 09:17:20
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 927 bytes
コンパイル時間 911 ms
コンパイル使用メモリ 82,884 KB
実行使用メモリ 6,820 KB
最終ジャッジ日時 2024-11-28 16:14:14
合計ジャッジ時間 2,183 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 25 WA * 3
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "iostream"
#include "time.h"
#include "stdlib.h"
#include "iomanip"
#include "math.h"
#include "algorithm"
#include "functional"
#include "list"

using namespace std;

int n;
list<int>p;
bool flag;
int ans;
int sum;

int main() {
	cin >> n;
	p.push_back(2);
	for (int i = 3; i <= n; i++) {
		flag = true;
		for (auto j = p.begin(); (*j)*(*j) <= i; ++j) {
			if (i % (*j)==0) {
				flag = false;
				break;
			}
		}
		if (flag)p.push_back(i);
	}
	for (auto i = p.begin(); sum<n; ++i) {
		//cout << (*i) << "\n";
		sum += (*i);
		ans++;
	}
	//cout << sum << "\n";
	for (auto i = p.begin(); (*i) <= sum - n; ++i) {
		if ((*i) == sum - n) {
			cout << ans - 1 << "\n";
			return 0;
		}
	}
	if (n == 1) {
		cout << "1\n";
		return 0;
	}
	if (sum - n > 0) {
		if ((sum - n) % 2 == 0)ans -= 2;
		else ans --;
	}
	if (n == 6||n==4)ans = -1;
	if (ans <= 0) {
		cout << "-1\n";
		return 0;
	}
	cout << ans << "\n";
	return 0;
}
0