結果

問題 No.811 約数の個数の最大化
ユーザー けーむけーむ
提出日時 2020-03-06 14:33:59
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 138 ms / 2,000 ms
コード長 2,170 bytes
コンパイル時間 2,009 ms
コンパイル使用メモリ 177,460 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-04 05:40:55
合計ジャッジ時間 2,847 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 3 ms
4,380 KB
testcase_02 AC 12 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 4 ms
4,376 KB
testcase_08 AC 4 ms
4,380 KB
testcase_09 AC 24 ms
4,380 KB
testcase_10 AC 9 ms
4,380 KB
testcase_11 AC 4 ms
4,380 KB
testcase_12 AC 3 ms
4,380 KB
testcase_13 AC 138 ms
4,380 KB
testcase_14 AC 4 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for(ll i = 0, i##_len = (n); i < i##_len; i++)
#define reps(i, s, n) for(ll i = (s), i##_len = (n); i < i##_len; i++)
#define rrep(i, n) for(ll i = (n) - 1; i >= 0; i--)
#define rreps(i, e, n) for(ll i = (n) - 1; i >= (e); i--)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((ll)(x).size())
#define len(x) ((ll)(x).length())

vector<pair<ll, ll>> factorize(ll n) {
    vector<pair<ll, ll>> res;
    for (ll i = 2; (i * i) <= n; i++) {
        if (n % i) continue;
        res.emplace_back(i, 0);
        while((n % i) == 0) {
            n /= i;
            res.back().second++;
        }
    }
    if (n != 1) res.emplace_back(n, 1);
    return res;
}

ll faccnt(ll n) {
    auto fac = factorize(n);
    set<ll> s;
    s.insert(1);
    rep(i, sz(fac)) {
        rep(j, fac[i].second) {
            set<ll> add;
            for(auto x : s) add.insert(x * fac[i].first);
            for(auto x : add) s.insert(x);
        }
    }
    return sz(s);
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    // ifstream in("input.txt");
    // cin.rdbuf(in.rdbuf());
    ll n, k;
    cin >> n >> k;
    auto fac = factorize(n);
    vector<set<ll>> vs(k + 1);
    vs[0].insert(1);
    rep(i, k) {
        rrep(j, i + 1) {
            for (auto x : vs[j]) {
                rep(l, sz(fac)) {
                    ll cnt = 0, cur = x;
                    while(cur % fac[l].first == 0) {
                        cnt++;
                        cur /= fac[l].first;
                    }
                    if (cnt < fac[l].second) {
                        vs[j + 1].insert(x * fac[l].first);
                    }
                }
            }
        }
    }
    ll mv = 0, ans = 0;
    reps(i, 1, n) {
        bool f = false;
        for(auto x : vs[k]) {
            if (i % x == 0) {
                f = true;
                break;
            }
        }
        if (!f) continue;
        auto fi = factorize(i);
        ll cnt = faccnt(i);
        if (cnt > mv) {
            mv = cnt;
            ans = i;
        }
    }
    cout << ans << endl;
    return 0;
}
0