結果

問題 No.1006 Share an Integer
ユーザー 👑 jupirojupiro
提出日時 2020-03-06 21:43:09
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,668 ms / 2,000 ms
コード長 3,169 bytes
コンパイル時間 1,712 ms
コンパイル使用メモリ 120,796 KB
実行使用メモリ 52,088 KB
最終ジャッジ日時 2023-08-04 07:53:04
合計ジャッジ時間 19,685 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 219 ms
51,964 KB
testcase_01 AC 178 ms
51,896 KB
testcase_02 AC 179 ms
52,088 KB
testcase_03 AC 182 ms
51,896 KB
testcase_04 AC 187 ms
52,088 KB
testcase_05 AC 190 ms
51,972 KB
testcase_06 AC 185 ms
52,052 KB
testcase_07 AC 189 ms
51,932 KB
testcase_08 AC 180 ms
51,976 KB
testcase_09 AC 180 ms
51,888 KB
testcase_10 AC 178 ms
52,088 KB
testcase_11 AC 926 ms
51,928 KB
testcase_12 AC 1,585 ms
51,888 KB
testcase_13 AC 1,649 ms
51,996 KB
testcase_14 AC 1,668 ms
51,892 KB
testcase_15 AC 1,381 ms
51,992 KB
testcase_16 AC 698 ms
52,084 KB
testcase_17 AC 942 ms
51,804 KB
testcase_18 AC 1,427 ms
51,976 KB
testcase_19 AC 1,345 ms
51,892 KB
testcase_20 AC 1,439 ms
51,756 KB
testcase_21 AC 1,635 ms
51,900 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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関連注意!!!!!!!!!!!!
	*/
}
0