結果

問題 No.375 立方体のN等分 (1)
ユーザー kimiyukikimiyuki
提出日時 2016-07-04 12:05:47
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 221 ms / 5,000 ms
コード長 2,106 bytes
コンパイル時間 1,121 ms
コンパイル使用メモリ 101,024 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-02 21:53:04
合計ジャッジ時間 3,113 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 6 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 23 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 3 ms
4,376 KB
testcase_11 AC 3 ms
4,380 KB
testcase_12 AC 3 ms
4,380 KB
testcase_13 AC 3 ms
4,376 KB
testcase_14 AC 32 ms
4,380 KB
testcase_15 AC 3 ms
4,376 KB
testcase_16 AC 4 ms
4,380 KB
testcase_17 AC 135 ms
4,376 KB
testcase_18 AC 32 ms
4,376 KB
testcase_19 AC 4 ms
4,376 KB
testcase_20 AC 3 ms
4,380 KB
testcase_21 AC 4 ms
4,376 KB
testcase_22 AC 221 ms
4,376 KB
testcase_23 AC 3 ms
4,380 KB
testcase_24 AC 3 ms
4,380 KB
testcase_25 AC 5 ms
4,376 KB
testcase_26 AC 4 ms
4,380 KB
testcase_27 AC 3 ms
4,380 KB
testcase_28 AC 3 ms
4,380 KB
testcase_29 AC 4 ms
4,376 KB
testcase_30 AC 4 ms
4,376 KB
testcase_31 AC 3 ms
4,376 KB
testcase_32 AC 4 ms
4,380 KB
testcase_33 AC 4 ms
4,376 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