結果

問題 No.376 立方体のN等分 (2)
ユーザー kimiyukikimiyuki
提出日時 2016-07-04 12:03:05
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2,770 ms / 5,000 ms
コード長 2,106 bytes
コンパイル時間 1,171 ms
コンパイル使用メモリ 101,060 KB
実行使用メモリ 10,460 KB
最終ジャッジ日時 2023-08-06 20:26:24
合計ジャッジ時間 17,405 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 53 ms
9,016 KB
testcase_03 AC 56 ms
8,616 KB
testcase_04 AC 61 ms
8,500 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 4 ms
4,376 KB
testcase_09 AC 14 ms
4,424 KB
testcase_10 AC 228 ms
4,380 KB
testcase_11 AC 29 ms
5,552 KB
testcase_12 AC 37 ms
5,892 KB
testcase_13 AC 38 ms
5,960 KB
testcase_14 AC 42 ms
5,872 KB
testcase_15 AC 48 ms
6,096 KB
testcase_16 AC 2,472 ms
8,784 KB
testcase_17 AC 54 ms
8,864 KB
testcase_18 AC 56 ms
8,976 KB
testcase_19 AC 57 ms
9,644 KB
testcase_20 AC 58 ms
10,108 KB
testcase_21 AC 2,596 ms
8,440 KB
testcase_22 AC 61 ms
8,828 KB
testcase_23 AC 2,651 ms
9,204 KB
testcase_24 AC 2,447 ms
8,516 KB
testcase_25 AC 63 ms
9,528 KB
testcase_26 AC 2,770 ms
8,732 KB
testcase_27 AC 69 ms
10,272 KB
testcase_28 AC 63 ms
8,544 KB
testcase_29 AC 65 ms
9,140 KB
testcase_30 AC 63 ms
8,916 KB
testcase_31 AC 65 ms
9,120 KB
testcase_32 AC 69 ms
9,280 KB
testcase_33 AC 71 ms
9,148 KB
testcase_34 AC 68 ms
9,164 KB
testcase_35 AC 69 ms
9,656 KB
testcase_36 AC 72 ms
9,444 KB
testcase_37 AC 71 ms
9,460 KB
testcase_38 AC 67 ms
10,460 KB
testcase_39 AC 64 ms
9,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <map>
#include <functional>
#define repeat(i,n) for (int i = 0; (i) < (n); ++(i))
typedef long long ll;
template <class T> bool setmin(T & l, T const & r) { if (not (r < l)) return false; l = r; return true; }
using namespace std;

vector<int> sieve_of_eratosthenes(int n) { // enumerate primes in [2,n] with O(n log log n)
    vector<bool> is_prime(n+1, true);
    is_prime[0] = is_prime[1] = false;
    for (int i = 2; i*i <= n; ++i)
        if (is_prime[i])
            for (int k = i+i; k <= n; k += i)
                is_prime[k] = false;
    vector<int> primes;
    for (int i = 2; i <= n; ++i)
        if (is_prime[i])
            primes.push_back(i);
    return primes;
}
map<ll,int> factors(ll n, vector<int> const & primes) {
    map<ll,int> result;
    for (int p : primes) {
        if (n < p *(ll) p) break;
        while (n % p == 0) {
            result[p] += 1;
            n /= p;
        }
    }
    if (n != 1) result[n] += 1;
    return result;
}

const ll inf = 1e18+9;
template <typename F>
ll go(ll n, map<ll,int> & ps, map<ll,int>::iterator it, ll acc, F cont) {
    if (it == ps.end()) {
        return cont(n, ps, acc);
    } else {
        ll p; int cnt; tie(p, cnt) = *it;
        ++ it;
        ll ans = inf;
        repeat (i,cnt+1) {
            setmin(ans, go(n, ps, it, acc, cont));
            ps[p] -= 1;
            acc *= p;
            n /= p;
        }
        ps[p] += cnt+1;
        return ans;
    }
}
template <typename F>
ll go(ll n, map<ll,int> ps, F cont) {
    return go(n, ps, ps.begin(), 1, cont);
}

int main() {
    ll n; cin >> n;
    vector<int> primes = sieve_of_eratosthenes(ceil(sqrt(n)));
    map<ll,ll> memo;
    auto ps = factors(n, primes);
    ll ans = go(n, ps, [&](ll n, map<ll,int> ps, ll a) {
        if (not memo.count(n)) {
            memo[n] = go(n, ps, [&](ll c, map<ll,int> ps, ll b) {
                return (b - 1) + (c - 1);
            });
        }
        return (a - 1) + memo[n];
    });
    cout << ans << ' ' << n-1 << endl;
    return 0;
}
0