結果

問題 No.636 硬貨の枚数2
ユーザー しらっ亭
提出日時 2017-05-10 17:46:08
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
TLE  
実行時間 -
コード長 727 bytes
コンパイル時間 699 ms
コンパイル使用メモリ 69,928 KB
実行使用メモリ 10,624 KB
最終ジャッジ日時 2024-09-19 10:23:21
合計ジャッジ時間 4,038 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3 TLE * 1
other -- * 65
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <cmath>
#include <functional>
#include <assert.h>

using namespace std;

int count(long long x) {
  int ret = 0;
  while (x) {
    ret += x % 5;
    if (x % 10 >= 5) ret++;
    x /= 10;
  }
  return ret;
}

int solve_all(long long n) {
  int d = log10(n) + 1;

  int ans = 1e9;

  const vector<int> C = {0,1,2,5,6,7};

  function<void(int, long long)> rec = [&](int i, long long change) {
    if (i == d) {
      int cand = count(n + change) + count(change);
      ans = min(ans, cand);
    }
    else {
      for (int c : C) rec(i+1, change * 10 + c);
    }
  };
  rec(0, 0);
  return ans;
}

int main() {
  long long n;
  cin >> n;
  cout << solve_all(n) << endl;
  return 0;
}
0