結果

問題 No.1006 Share an Integer
ユーザー tkmst201tkmst201
提出日時 2021-01-19 11:27:07
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 281 ms / 2,000 ms
コード長 1,781 bytes
コンパイル時間 2,050 ms
コンパイル使用メモリ 207,136 KB
実行使用メモリ 19,072 KB
最終ジャッジ日時 2024-05-09 14:28:52
合計ジャッジ時間 5,263 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 151 ms
12,240 KB
testcase_12 AC 269 ms
18,048 KB
testcase_13 AC 281 ms
19,072 KB
testcase_14 AC 279 ms
19,072 KB
testcase_15 AC 235 ms
16,820 KB
testcase_16 AC 112 ms
9,940 KB
testcase_17 AC 150 ms
12,288 KB
testcase_18 AC 234 ms
16,896 KB
testcase_19 AC 218 ms
16,256 KB
testcase_20 AC 240 ms
17,396 KB
testcase_21 AC 277 ms
18,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) begin(v),end(v)
template<typename A, typename B> inline bool chmax(A & a, const B & b) { if (a < b) { a = b; return true; } return false; }
template<typename A, typename B> inline bool chmin(A & a, const B & b) { if (a > b) { a = b; return true; } return false; }
using ll = long long;
using pii = pair<int, int>;
constexpr ll INF = 1ll<<30;
constexpr ll longINF = 1ll<<60;
constexpr ll MOD = 1000000007;
constexpr bool debug = false;
//---------------------------------//

std::vector<int> sieve_smallest_prime_factor(int n) {
	assert(n >= 0);
	std::vector<int> res(n + 1);
	std::iota(begin(res), end(res), 0);
	
	for (int i = 2; i * i <= n; ++i) {
		if (res[i] < i) continue;
		for (int j = i * i; j <= n; j += i) {
			if (res[j] == j) res[j] = i;
		}
	}
	
	return res;
}

std::vector<std::pair<int, int>> fast_prime_factorization(int n, const std::vector<int> & smallest_prime_factor) {
	assert(n >= 0);
	assert(n <= smallest_prime_factor.size());
	std::vector<std::pair<int, int>> res;
	for (; n > 1; n /= smallest_prime_factor[n]) {
		if (res.empty() || res.back().first != smallest_prime_factor[n]) res.emplace_back(smallest_prime_factor[n], 1);
		else ++res.back().second;
	}
	return res;
}

int main() {
	int X;
	cin >> X;
	
	auto spf = sieve_smallest_prime_factor(X);
	
	vector<int> f(X + 1);
	FOR(i, 1, X + 1) {
		int cur = 1;
		for (auto [_, c] : fast_prime_factorization(i, spf)) cur *= c + 1;
		f[i] = i - cur;
	}
	
	int mn = INF;
	vector<int> ans;
	FOR(i, 1, X) {
		int cur = abs(f[i] - f[X - i]);
		if (mn < cur) continue;
		if (chmin(mn, cur)) ans.clear();
		ans.emplace_back(i);
	}
	for (int i : ans) printf("%d %d\n", i, X - i);
}
0