結果

問題 No.1006 Share an Integer
ユーザー qLethonqLethon
提出日時 2020-03-06 23:27:14
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 652 ms / 2,000 ms
コード長 1,353 bytes
コンパイル時間 2,433 ms
コンパイル使用メモリ 199,644 KB
実行使用メモリ 19,108 KB
最終ジャッジ日時 2024-04-22 10:38:06
合計ジャッジ時間 8,792 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,940 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 3 ms
6,940 KB
testcase_09 AC 2 ms
6,944 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 315 ms
12,184 KB
testcase_12 AC 605 ms
18,192 KB
testcase_13 AC 614 ms
19,108 KB
testcase_14 AC 652 ms
19,008 KB
testcase_15 AC 510 ms
16,736 KB
testcase_16 AC 235 ms
9,960 KB
testcase_17 AC 324 ms
12,464 KB
testcase_18 AC 522 ms
16,904 KB
testcase_19 AC 502 ms
16,464 KB
testcase_20 AC 572 ms
17,232 KB
testcase_21 AC 622 ms
18,884 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::vector<int> prime_factorize(INT n, std::vector<INT> &lp){
    vector<int> res(1);
    int last = 2;
    while (n != 1){
        if (last != lp[n]){
            res.push_back(0);
            last = lp[n];
        }
        n /= lp[n];
        res[res.size() - 1]++;
    }

    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 &v : pa)
            ca *= (v + 1);
        for (const auto &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