結果

問題 No.1006 Share an Integer
ユーザー qLethonqLethon
提出日時 2020-03-06 23:29:50
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 741 ms / 2,000 ms
コード長 1,248 bytes
コンパイル時間 2,294 ms
コンパイル使用メモリ 204,460 KB
実行使用メモリ 19,092 KB
最終ジャッジ日時 2024-04-22 10:39:09
合計ジャッジ時間 9,543 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 3 ms
6,940 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 390 ms
12,128 KB
testcase_12 AC 694 ms
18,092 KB
testcase_13 AC 737 ms
19,092 KB
testcase_14 AC 741 ms
19,008 KB
testcase_15 AC 626 ms
16,832 KB
testcase_16 AC 292 ms
9,900 KB
testcase_17 AC 404 ms
12,284 KB
testcase_18 AC 628 ms
17,012 KB
testcase_19 AC 583 ms
16,260 KB
testcase_20 AC 651 ms
17,312 KB
testcase_21 AC 740 ms
18,900 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

template<class INT>
std::vector<INT> get_min_prime_fact_table(INT n){
    std::vector<INT> lp(n + 1), P;
    for (INT i = 2; i <= n; i++){
        if (lp[i] == 0){
            lp[i] = i;
            P.push_back(i);
        }
        for (INT j = 0; j <= P.size() and P[j] <= lp[i] and i * P[j] <= n; j++)
            lp[i * P[j]] = P[j];
    }

    return lp;
}

template<class INT>
std::map<INT, int> prime_factorize(INT n, std::vector<INT>& lp){
    std::map<INT, int> res;
    while (n != 1){
        res[lp[n]]++;
        n /= lp[n];
    }

    return res;
}

int main(){
    int x;
    cin >> x;
    auto lp = get_min_prime_fact_table(x);
    vector<int> A(x + 1);
    int mi = 1e9;
    for (int a = 1; a < x; a++){
        auto b = x - a;
        auto pa = prime_factorize(a, lp);
        auto pb = prime_factorize(b, lp);
        int ca = 1, cb = 1;
        for (const auto &[k, v] : pa)
            ca *= (v + 1);
        for (const auto &[k, v] : pb)
            cb *= (v + 1);
        auto s = abs(a - ca - (b - cb));
        A[a] = s;
        mi = min(mi, s); 
    }

    for (int i = 1; i < x; i++){
        if (A[i] == mi){
            cout << i << " " << x - i << endl;
        }
    }
}
0