結果

問題 No.636 硬貨の枚数2
ユーザー はむこはむこ
提出日時 2017-05-22 11:23:26
言語 C++11
(gcc 11.4.0)
結果
RE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,078 bytes
コンパイル時間 1,516 ms
コンパイル使用メモリ 170,528 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-19 14:17:10
合計ジャッジ時間 6,972 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 1 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 1 ms
4,348 KB
testcase_06 AC 1 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 1 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 1 ms
4,348 KB
testcase_11 AC 1 ms
4,348 KB
testcase_12 AC 1 ms
4,348 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 1 ms
4,348 KB
testcase_15 AC 0 ms
4,348 KB
testcase_16 AC 2 ms
4,348 KB
testcase_17 AC 1 ms
4,348 KB
testcase_18 AC 2 ms
4,348 KB
testcase_19 AC 2 ms
4,348 KB
testcase_20 AC 2 ms
4,348 KB
testcase_21 AC 2 ms
4,348 KB
testcase_22 AC 2 ms
4,348 KB
testcase_23 AC 2 ms
4,348 KB
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 RE -
testcase_28 RE -
testcase_29 RE -
testcase_30 RE -
testcase_31 RE -
testcase_32 RE -
testcase_33 RE -
testcase_34 RE -
testcase_35 RE -
testcase_36 RE -
testcase_37 RE -
testcase_38 RE -
testcase_39 RE -
testcase_40 RE -
testcase_41 RE -
testcase_42 RE -
testcase_43 RE -
testcase_44 RE -
testcase_45 RE -
testcase_46 RE -
testcase_47 RE -
testcase_48 RE -
testcase_49 RE -
testcase_50 RE -
testcase_51 RE -
testcase_52 RE -
testcase_53 RE -
testcase_54 AC 2 ms
4,348 KB
testcase_55 AC 1 ms
4,348 KB
testcase_56 AC 2 ms
4,348 KB
testcase_57 AC 2 ms
4,348 KB
testcase_58 AC 1 ms
4,348 KB
testcase_59 AC 2 ms
4,348 KB
testcase_60 AC 2 ms
4,348 KB
testcase_61 AC 2 ms
4,348 KB
testcase_62 AC 2 ms
4,348 KB
testcase_63 AC 1 ms
4,348 KB
testcase_64 RE -
testcase_65 RE -
testcase_66 RE -
testcase_67 RE -
testcase_68 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

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

#define rep(i,n) for(long long i = 0; i < (long long)(n); i++)
#define pb push_back
template<class T1, class T2> bool chmin(T1 &a, T2 b) { return b < a && (a = b, true); }

using ll = long long; using vll = vector<ll>;

unordered_map<ll, ll> memo;
ll pay[10] = {0, 1, 2, 3, 2, 1, 2, 3, 3, 2};
vll p10;
ll dfs(ll n) {
    n = abs(n);
    if (memo.count(n)) return memo[n];
    if (!n) return 0;

    vll nd;
    ll only_msd = 1; // n = a*10^kなるa, kが存在するか
    ll n_tmp = n;
    for (; n_tmp ; n_tmp /= 10) nd.pb(n_tmp % 10);
    rep(i, nd.size()-1) only_msd &= !nd[i];
    ll msd = nd.back(); // 最大桁
    ll d = p10[nd.size()-1]; // 桁数

    ll ret = 4e18;
    chmin(ret, pay[msd] + dfs(n - msd * d));
    if (!only_msd) 
        chmin(ret, pay[msd==9?1:msd+1] + dfs((msd + 1) * d - n));

    return memo[n] = ret;
}

int main(void) {
    ll n; cin >> n; assert(1<=n&&n<=1e18);
    for (ll b = 1, i = 0; i < 18; b*=10, i++) 
        p10.pb(b);
    cout << dfs(n) << endl;
    return 0;
}
0