結果
問題 | No.1006 Share an Integer |
ユーザー | jupiro |
提出日時 | 2020-03-06 21:43:09 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 1,593 ms / 2,000 ms |
コード長 | 3,169 bytes |
コンパイル時間 | 1,220 ms |
コンパイル使用メモリ | 121,328 KB |
実行使用メモリ | 52,068 KB |
最終ジャッジ日時 | 2024-10-14 04:57:52 |
合計ジャッジ時間 | 18,064 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 19 |
ソースコード
#include <cstdio> #include <iostream> #include <string> #include <sstream> #include <stack> #include <algorithm> #include <cmath> #include <queue> #include <map> #include <set> #include <cstdlib> #include <bitset> #include <tuple> #include <assert.h> #include <deque> #include <bitset> #include <iomanip> #include <limits> #include <chrono> #include <random> #include <array> #include <unordered_map> #include <functional> #include <complex> template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } constexpr long long MAX = 5100000; constexpr long long INF = 1LL << 60; constexpr int inf = 1 << 28; constexpr long long mod = 1000000007LL; //constexpr long long mod = 998244353LL; using namespace std; typedef unsigned long long ull; typedef long long ll; bool IsPrime[MAX]; ll MinFactor[MAX]; vector<ll> preprocess(ll n = MAX) { vector<ll> res; for (ll i = 0; i < n; ++i) IsPrime[i] = true, MinFactor[i] = -1; IsPrime[0] = false; IsPrime[1] = false; MinFactor[0] = 0; MinFactor[1] = 1; for (ll i = 2; i < n; ++i) { if (IsPrime[i]) { MinFactor[i] = i; res.push_back(i); for (ll j = i * 2; j < n; j += i) { IsPrime[j] = false; if (MinFactor[j] == -1) MinFactor[j] = i; } } } return res; } vector<pair<ll, ll> > fast_prime_factor(ll n) { vector<pair<ll, ll> > res; while (n != 1) { ll prime = MinFactor[n]; ll exp = 0; while (MinFactor[n] == prime) { ++exp; n /= prime; } res.push_back(make_pair(prime, exp)); } return res; } int main() { /* cin.tie(nullptr); ios::sync_with_stdio(false); */ preprocess(); ll X; scanf("%lld", &X); if (X == 2) { cout << "1 1" << endl; return 0; } vector<pair<ll, ll>> res; ll ans = INF; for (ll a = 1; a < X; a++) { ll b = X - a; vector<pair<ll, ll>> va = fast_prime_factor(a); vector<pair<ll, ll>> vb = fast_prime_factor(b); ll cnt1 = 1, cnt2 = 1; for (auto p : va) { cnt1 *= (p.second + 1); } for (auto p : vb) { cnt2 *= (p.second + 1); } ll res1 = a - cnt1; ll res2 = b - cnt2; chmin(ans, llabs(res1 - res2)); } for (ll a = 1; a < X; a++) { ll b = X - a; vector<pair<ll, ll>> va = fast_prime_factor(a); vector<pair<ll, ll>> vb = fast_prime_factor(b); ll cnt1 = 1, cnt2 = 1; for (auto p : va) { cnt1 *= (p.second + 1); } for (auto p : vb) { cnt2 *= (p.second + 1); } ll res1 = a - cnt1; ll res2 = b - cnt2; if (llabs(res1 - res2) == ans) { res.emplace_back(a, b); } } for (int i = 0; i < res.size(); i++) { printf("%lld %lld\n", res[i].first, res[i].second); } return 0; /* おまじないを使ったらscanfとprintf関連注意!!!!!!!!!!!! */ }