prithivMLmods/Coder-Stat huggingface dataset:
Problem p03329 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repn(i, j, k) for (int i = (j); i < (k); i++)
#define all(v) v.begin(), v.end()
#define INF 1e9
using namespace std;
using ll = long long;

int main() {
  int dp[600000];
  int N;
  cin >> N;
  rep(i, N + 1) dp[i] = INF;
  dp[0] = 0;
  rep(i, N + 1) {
    rep(j, 8) {
      int pow6 = pow(6, j);
      int res = dp[i + pow6];
      dp[i + pow6] = min(res, dp[i] + 1);
    }
    rep(j, 7) {
      int pow9 = pow(9, j);
      int res = dp[i + pow9];
      dp[i + pow9] = min(res, dp[i] + 1);
    }
  }
  cout << dp[N] << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repn(i, j, k) for (int i = (j); i < (k); i++)
#define all(v) v.begin(), v.end()
#define INF 1e9
using namespace std;
using ll = long long;

int main() {
  int dp[1000000];
  int N;
  cin >> N;
  rep(i, N + 1) dp[i] = INF;
  dp[0] = 0;
  rep(i, N + 1) {
    rep(j, 8) {
      int pow6 = pow(6, j);
      int res = dp[i + pow6];
      dp[i + pow6] = min(res, dp[i] + 1);
    }
    rep(j, 7) {
      int pow9 = pow(9, j);
      int res = dp[i + pow9];
      dp[i + pow9] = min(res, dp[i] + 1);
    }
  }
  cout << dp[N] << endl;
  return 0;
}
Problem p03329 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef tuple<ll, ll, ll> tll;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll N;
  cin >> N;
  vector<ll> baseNums;
  baseNums.push_back(1);
  ll temp = 6;
  while (temp <= N) {
    baseNums.push_back(temp);
    temp *= 6;
  }
  temp = 9;
  while (temp <= N) {
    baseNums.push_back(temp);
    temp *= 9;
  }
  sort(baseNums.begin(), baseNums.end(), greater<ll>());
  priority_queue<tll, vector<tll>, greater<tll>> que;
  que.push(make_tuple(0, N, 0));
  ll answer = 1e12;
  while (!que.empty()) {
    ll count, remain, baseNumIndex;
    tie(count, remain, baseNumIndex) = que.top();
    que.pop();
    ll baseNum = baseNums.at(baseNumIndex);
    if (count >= answer) {
      break;
    }
    if (remain % baseNum == 0) {
      answer = min(answer, count + remain / baseNum);
      continue;
    }
    ll currentCount = 0;
    while (baseNum * currentCount < remain) {
      que.push(make_tuple(count + currentCount, remain - baseNum * currentCount,
                          baseNumIndex + 1));
      currentCount++;
    }
  }
  cout << answer << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef tuple<ll, ll, ll> tll;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll N;
  cin >> N;
  vector<ll> baseNums;
  baseNums.push_back(1);
  ll temp = 6;
  while (temp <= N) {
    baseNums.push_back(temp);
    temp *= 6;
  }
  temp = 9;
  while (temp <= N) {
    baseNums.push_back(temp);
    temp *= 9;
  }
  sort(baseNums.begin(), baseNums.end(), greater<ll>());
  priority_queue<tll, vector<tll>, greater<tll>> que;
  que.push(make_tuple(0, N, 0));
  ll answer = 1e12;
  while (!que.empty()) {
    ll count, remain, baseNumIndex;
    tie(count, remain, baseNumIndex) = que.top();
    que.pop();
    ll baseNum = baseNums.at(baseNumIndex);
    if (count >= answer) {
      break;
    }
    if (remain % baseNum == 0) {
      answer = min(answer, count + remain / baseNum);
      continue;
    }
    ll currentCount = 0;
    while (baseNum * currentCount < remain) {
      if ((baseNum % 2 == 1 && currentCount == 9) ||
          (baseNum % 2 == 0 && currentCount == 6)) {
        break;
      }
      que.push(make_tuple(count + currentCount, remain - baseNum * currentCount,
                          baseNumIndex + 1));
      currentCount++;
    }
  }
  cout << answer << endl;
  return 0;
}
Problem p03329 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define mkp make_pair
#define pr(num) cout << num << "\n"
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
#define INF 1000000000000000000
#define MOD 1000000007LL
#define MAX 100010
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef map<ll, ll> Map;

int main(void) {
  ll N;
  cin >> N;
  ll dp[MAX];
  ll c[] = {1,         6, 36, 216, 216 * 6, 216 * 36,
            216 * 216, 9, 81, 729, 729 * 9, 729 * 81};
  ll i, j;
  for (i = 0; i <= N; i++) {
    dp[i] = INF;
  }

  dp[0] = 0;

  for (i = 1; i <= 12; i++) {
    for (j = 0; j <= N; j++) {
      dp[j] = min(dp[j], dp[j - c[i - 1]] + 1);
    }
  }

  pr(dp[N]);
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define mkp make_pair
#define pr(num) cout << num << "\n"
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
#define INF 1000000000000000000
#define MOD 1000000007LL
#define MAX 100010
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef map<ll, ll> Map;

int main(void) {
  ll N;
  cin >> N;
  ll dp[MAX];
  ll c[] = {1,         6, 36, 216, 216 * 6, 216 * 36,
            216 * 216, 9, 81, 729, 729 * 9, 729 * 81};
  ll i, j;
  for (i = 0; i <= N; i++) {
    dp[i] = INF;
  }

  dp[0] = 0;

  for (i = 1; i <= 12; i++) {
    for (j = 0; j <= N; j++) {
      if (j >= c[i - 1])
        dp[j] = min(dp[j], dp[j - c[i - 1]] + 1);
    }
  }

  pr(dp[N]);
}
Problem p03329 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define rep1(i, n) for (int i = 1; i <= (int)n; i++)
#define rep2(i, j, n) for (int j = i; j < n; j++)
#define rep3(i, j, n) for (int j = i; j <= n; j++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define sp(n) cout << fixed << setprecision(n)
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
typedef long long ll;
using namespace std;
int n;
vector<int> y;
int dfs(int i, int j, int k) {
  if (y[j] == 1) {
    int res = k + i;
    return res;
  }
  int buf = i / y[j];
  int res = 1e+7;
  rep(l, buf + 1) {
    int buf2 = l * y[j];
    int r = dfs(i - buf2, j + 1, k + l);
    chmin(res, r);
  }
  return res;
}
int main(void) {
  int n;
  cin >> n;
  priority_queue<int> que;
  int tmp = 9;
  while (tmp <= n) {
    que.push(tmp);
    tmp *= 9;
  }
  tmp = 6;
  while (tmp <= n) {
    que.push(tmp);
    tmp *= 6;
  }
  while (que.size()) {
    int x = que.top();
    que.pop();
    y.push_back(x);
  }
  y.push_back(1);
  int res = dfs(n, 0, 0);
  cout << res << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define rep1(i, n) for (int i = 1; i <= (int)n; i++)
#define rep2(i, j, n) for (int j = i; j < n; j++)
#define rep3(i, j, n) for (int j = i; j <= n; j++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define sp(n) cout << fixed << setprecision(n)
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
typedef long long ll;
using namespace std;
int n;
vector<int> y;
int dfs(int i, int j, int k) {
  if (y[j] == 1) {
    int res = k + i;
    return res;
  }
  int buf = i / y[j];
  int res = 1e+7;
  int tmp = (y[j] % 9 == 0) ? 9 : 6;
  rep(l, min(buf + 1, tmp)) {
    int buf2 = l * y[j];
    int r = dfs(i - buf2, j + 1, k + l);
    chmin(res, r);
  }
  return res;
}
int main(void) {
  int n;
  cin >> n;
  priority_queue<int> que;
  int tmp = 9;
  while (tmp <= n) {
    que.push(tmp);
    tmp *= 9;
  }
  tmp = 6;
  while (tmp <= n) {
    que.push(tmp);
    tmp *= 6;
  }
  while (que.size()) {
    int x = que.top();
    que.pop();
    y.push_back(x);
  }
  y.push_back(1);
  int res = dfs(n, 0, 0);
  cout << res << endl;
}
Problem p03329 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
using namespace std;

ll inf = 9223372036854775806;

int main() {
  // freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int i, j, k, n,
      a[12] = {1, 6, 9, 36, 81, 216, 729, 1296, 6561, 7776, 46656, 59049},
      w[100001] = {0};
  for (i = 0; i < 12; i++) {
    w[a[i]] = 1;
  }
  for (i = 2; i <= 100000; i++) {
    if (w[i] != 1) {
      int t = 1000000000;
      for (j = 0; a[j] < i; j++) {
        t = min(t, w[i - a[j]]);
      }
      w[i] = t + 1;
    }
  }
  cin >> n;
  cout << w[n];
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
using namespace std;

ll inf = 9223372036854775806;

int main() {
  // freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int i, j, k, n,
      a[12] = {1, 6, 9, 36, 81, 216, 729, 1296, 6561, 7776, 46656, 59049},
      w[100001] = {0};
  for (i = 0; i < 12; i++) {
    w[a[i]] = 1;
  }
  for (i = 2; i <= 100000; i++) {
    if (w[i] != 1) {
      int t = 1000000000;
      for (j = 0; j < 12; j++) {
        if (a[j] < i) {
          t = min(t, w[i - a[j]]);
        } else {
          break;
        }
      }
      w[i] = t + 1;
    }
  }
  cin >> n;
  cout << w[n];
  return 0;
}
Problem p03329 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <queue>
#include <stdio.h>
#include <string.h>
using namespace std;
int a[10], vis[100010];
int n;
int bfs() {
  int i, j, k;
  queue<int> q;
  memset(vis, 0, sizeof(vis));
  vis[0] = 1;
  q.push(0);
  while (!q.empty()) {
    k = q.front();
    q.pop();
    if (k == n)
      return vis[k] - 1;
    for (i = 0; i < 12; i++) {
      j = k + a[i];
      if (j <= n && vis[j] == 0) {
        vis[j] = vis[k] + 1;
        q.push(j);
      }
    }
  }
}
int main() {
  int i, j, k;
  a[0] = 1;
  for (i = 1; i < 7; i++)
    a[i] = a[i - 1] * 6;
  a[7] = 9;
  for (i = 8; i < 12; i++)
    a[i] = a[i - 1] * 9;
  scanf("%d", &n);
  printf("%d\n", bfs());
  return 0;
}


Fixed submission:
#include <algorithm>
#include <queue>
#include <stdio.h>
#include <string.h>
using namespace std;
int a[20], vis[100010];
int n;
int bfs() {
  int i, j, k;
  queue<int> q;
  memset(vis, 0, sizeof(vis));
  vis[0] = 1;
  q.push(0);
  while (!q.empty()) {
    k = q.front();
    q.pop();
    if (k == n)
      return vis[k] - 1;
    for (i = 0; i < 12; i++) {
      j = k + a[i];
      if (j <= n && vis[j] == 0) {
        vis[j] = vis[k] + 1;
        q.push(j);
      }
    }
  }
}
int main() {
  int i, j, k;
  a[0] = 1;
  for (i = 1; i < 7; i++)
    a[i] = a[i - 1] * 6;
  a[7] = 9;
  for (i = 8; i < 12; i++)
    a[i] = a[i - 1] * 9;
  scanf("%d", &n);
  printf("%d\n", bfs());
  return 0;
}
Problem p03329 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {

  int N, p;
  cin >> N;
  vector<int> dp(N + 1);
  for (int i = 0; i <= N; i++) {

    dp.at(i) = i;
    int j = 1;
    while (i >= pow(6, j)) {
      p = pow(6, j);
      dp.at(i) = min(dp.at(i), dp.at(i - p) + 1);
      j++;
    }
    j = 1;
    while (i >= pow(9, j)) {
      p = pow(9, j);
      dp.at(i) = min(dp.at(i), dp.at(i - p) + 1);
      j++;
    }
  }
  for (int i = 0; i < 50; i++) {
    cout << dp.at(i) << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int main() {

  int N, p;
  cin >> N;
  vector<int> dp(N + 1);
  for (int i = 0; i <= N; i++) {

    dp.at(i) = i;
    int j = 1;
    while (i >= pow(6, j)) {
      p = pow(6, j);
      dp.at(i) = min(dp.at(i), dp.at(i - p) + 1);
      j++;
    }
    j = 1;
    while (i >= pow(9, j)) {
      p = pow(9, j);
      dp.at(i) = min(dp.at(i), dp.at(i - p) + 1);
      j++;
    }
  }
  cout << dp.at(N) << endl;
  return 0;
}
Problem p03329 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
int ans[100005];
int b[13] = {0, 1, 6, 9, 36, 81, 216, 729, 1296, 6561, 7776, 46656, 59049};
int main() {
  int n, k;
  cin >> n;
  for (int i = 1; i <= n; i++)
    ans[i] = INF;
  k = 0;
  while (b[k] <= n)
    k++;
  k--;
  // cout<<k;
  for (int j = k; j >= 1; j--) {
    for (int i = b[j]; i <= n; i++)
      ans[i] = min(ans[i], ans[i - b[j]] + 1);
  }
  cout << ans[n];

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
int ans[100005];
int b[13] = {0, 1, 6, 9, 36, 81, 216, 729, 1296, 6561, 7776, 46656, 59049};
int main() {
  int n, k;
  cin >> n;
  for (int i = 1; i <= n; i++)
    ans[i] = INF;

  for (int j = 12; j >= 1; j--) {
    for (int i = b[j]; i <= n; i++)
      ans[i] = min(ans[i], ans[i - b[j]] + 1);
  }
  cout << ans[n];

  return 0;
}
Problem p03329 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {
  int N;
  cin >> N;
  vector<int> p = {1};
  int a = 6, b = 9;
  while (a < 100000) {
    p.push_back(a);
    a *= 6;
  }
  while (b < 100000) {
    p.push_back(b);
    b *= 9;
  }
  sort(p.begin(), p.end());
  vector<int> z(N + 1, 100000);
  z.at(0) = 0;
  for (int i = 1; i <= N; i++) {
    int j = 0;
    while (i - p.at(j) >= 0) {
      z.at(i) = min(z.at(i), z.at(i - p.at(j)) + 1);
      j++;
    }
  }
  cout << z.at(N) << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int main() {
  int N;
  cin >> N;
  vector<int> p = {1};
  int a = 6, b = 9;
  while (a < 100000) {
    p.push_back(a);
    a *= 6;
  }
  while (b < 100000) {
    p.push_back(b);
    b *= 9;
  }
  sort(p.begin(), p.end());
  vector<int> z(N + 1, 100000);
  z.at(0) = 0;
  for (int i = 1; i <= N; i++) {
    int j = 0;
    while (i - p.at(j) >= 0) {
      z.at(i) = min(z.at(i), z.at(i - p.at(j)) + 1);
      if (j != p.size() - 1) {
        j++;
      } else {
        break;
      }
    }
  }
  cout << z.at(N) << endl;
}
Problem p03329 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <array>
#include <cmath>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <utility>
#include <vector>

using namespace std;
using i64 = long long int;

int main() {
  int N;
  cin >> N;
  vector<int> cost(N + 1, 1000000);

  cost[1] = 1;
  for (int i = 1; i <= N; ++i) {
    cost[i + 1] = min(cost[i + 1], cost[i] + 1);
    int j = 6;
    while (i + j <= N) {
      cost[i + j] = min(cost[i + j], cost[i] + 1);
      j *= 6;
    }
    j = 9;
    while (i + j <= N) {
      cost[i + j] = min(cost[i + j], cost[i] + 1);
      j *= 9;
    }
  }

  cout << cost[N] << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <array>
#include <cmath>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <utility>
#include <vector>

using namespace std;
using i64 = long long int;

int main() {
  int N;
  cin >> N;
  vector<int> cost(N + 1, 1000000);

  cost[0] = 0;
  for (int i = 0; i < N; ++i) {
    cost[i + 1] = min(cost[i + 1], cost[i] + 1);
    int j = 6;
    while (i + j <= N) {
      cost[i + j] = min(cost[i + j], cost[i] + 1);
      j *= 6;
    }
    j = 9;
    while (i + j <= N) {
      cost[i + j] = min(cost[i + j], cost[i] + 1);
      j *= 9;
    }
  }

  cout << cost[N] << endl;

  return 0;
}
Problem p03329 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < n; i++)

int six[7] = {1, 6, 36, 216, 1296, 7776, 46656};
int nine[6] = {1, 9, 81, 729, 6561, 59049};

int func(int num, int ans) {
  if (num < 6)
    return ans + num;
  int i6, i9;
  i6 = max((int)(upper_bound(six, six + 7, num) - six) - 1, 0);
  i9 = max((int)(upper_bound(nine, nine + 7, num) - nine) - 1, 0);
  ans++;
  return min(func(num - six[i6], ans), func(num - nine[i9], ans));
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n;
  cin >> n;

  cout << func(n, 0) << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < n; i++)

int six[7] = {1, 6, 36, 216, 1296, 7776, 46656};
int nine[6] = {1, 9, 81, 729, 6561, 59049};

int func(int num, int ans) {
  if (num < 6)
    return ans + num;
  int i6, i9;
  i6 = upper_bound(six, six + 7, num) - six;
  i6 = max(0, i6 - 1);
  i9 = upper_bound(nine, nine + 6, num) - nine;
  i9 = max(0, i9 - 1);
  ans++;
  return min(func(num - six[i6], ans), func(num - nine[i9], ans));
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n;
  cin >> n;

  cout << func(n, 0) << endl;

  return 0;
}
Problem p03329 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define rep2(i, x, n) for (int i = x, i##_len = (n); i < i##_len; ++i)
#define all(n) begin(n), end(n)
using ll = long long;
using P = pair<int, int>;
using vi = vector<int>;
using vl = vector<ll>;
using vs = vector<string>;
using vc = vector<char>;
using vb = vector<bool>;
vi dir = {-1, 0, 1, 0, -1, -1, 1, 1, -1};

int main() {
  int n;
  cin >> n;
  vi dp(n + 1, 1e9);
  dp[0] = 0;
  rep(i, n + 1) {
    dp[i + 1] = min(dp[i + 1], dp[i] + 1);
    int x = 6;
    while (i + x <= n) {
      dp[i + x] = min(dp[i + x], dp[i] + 1);
      x *= 6;
    }
    x = 9;
    while (i + x <= n) {
      dp[i + x] = min(dp[i + x], dp[i] + 1);
      x *= 9;
    }
  }
  cout << dp[n] << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define rep2(i, x, n) for (int i = x, i##_len = (n); i < i##_len; ++i)
#define all(n) begin(n), end(n)
using ll = long long;
using P = pair<int, int>;
using vi = vector<int>;
using vl = vector<ll>;
using vs = vector<string>;
using vc = vector<char>;
using vb = vector<bool>;
vi dir = {-1, 0, 1, 0, -1, -1, 1, 1, -1};

int main() {
  int n;
  cin >> n;
  vi dp(n + 1, 1e9);
  dp[0] = 0;
  rep(i, n) {
    dp[i + 1] = min(dp[i + 1], dp[i] + 1);
    int x = 6;
    while (i + x <= n) {
      dp[i + x] = min(dp[i + x], dp[i] + 1);
      x *= 6;
    }
    x = 9;
    while (i + x <= n) {
      dp[i + x] = min(dp[i + x], dp[i] + 1);
      x *= 9;
    }
  }
  cout << dp[n] << endl;
}
Problem p03329 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int N;
vector<int> V;
int dp[100010];

int main() {
  scanf("%d", &N);
  int tmp = 1;
  while (tmp <= N)
    V.push_back(tmp), tmp *= 6;
  tmp = 1;
  while (tmp <= N)
    V.push_back(tmp), tmp *= 9;

  for (int i = 1; i <= N; i++)
    dp[i] = 1001001001;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < V.size(); j++)
      dp[i + V[j]] = min(dp[i + V[j]], dp[i] + 1);
  }
  printf("%d\n", dp[N]);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int N;
vector<int> V;
int dp[100010];

int main() {
  scanf("%d", &N);
  int tmp = 1;
  while (tmp <= N)
    V.push_back(tmp), tmp *= 6;
  tmp = 1;
  while (tmp <= N)
    V.push_back(tmp), tmp *= 9;

  for (int i = 1; i <= N; i++)
    dp[i] = 1001001001;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < V.size(); j++)
      if (i + V[j] <= N)
        dp[i + V[j]] = min(dp[i + V[j]], dp[i] + 1);
  }
  printf("%d\n", dp[N]);
  return 0;
}
Problem p03329 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>
using namespace std;
const int INF = 1e8;
std::vector<int> c;
std::vector<int> dp(11111);

int main() {
  int N, i, j;

  std::cin >> N;

  c.push_back(1);
  for (i = 6; i <= N; i = i * 6)
    c.push_back(i);
  for (i = 9; i <= N; i = i * 9)
    c.push_back(i);

  fill(dp.begin(), dp.end(), INF);
  dp[0] = 0;

  for (i = 0; i <= N; i++) {
    for (j = 0; j <= c.size(); j++) {
      if (i >= c[j]) {
        dp[i] = min(dp[i], dp[i - c[j]] + 1);
      }
    }
  }

  std::cout << dp[N] << '\n';

  return 0;
}


Fixed submission:
#include <iostream>
#include <vector>
using namespace std;
const int INF = 1e8;
std::vector<int> c;
std::vector<int> dp(111111);

int main() {
  int N, i, j;

  std::cin >> N;

  c.push_back(1);
  for (i = 6; i <= N; i = i * 6)
    c.push_back(i);
  for (i = 9; i <= N; i = i * 9)
    c.push_back(i);

  fill(dp.begin(), dp.end(), INF);
  dp[0] = 0;

  for (i = 0; i <= N; i++) {
    for (j = 0; j <= c.size(); j++) {
      if (i >= c[j]) {
        dp[i] = min(dp[i], dp[i - c[j]] + 1);
      }
    }
  }

  std::cout << dp[N] << '\n';

  return 0;
}
Problem p03329 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll MOD = 1000000007;
const ll MOD2 = 998244353;
const double PI = acos(-1);
const ll INF = 1e18;
typedef pair<ll, ll> P;
typedef vector<ll> vl;
typedef vector<vl> vvl;
#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define rep(i, n) FOR(i, 0, n)
string abc = "abcdefghijklmnopqrstuvwxyz";
string ABC = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
struct edge {
  ll to, cost;
};

vl memo(100001, -1);

ll dfs(ll x) {
  if (x == 0) {
    return 0;
  }
  if (x == 1 || x == 6 || x == 9) {
    memo[x] = 1;
    return 1;
  }
  ll a = 6, b = 9;
  ll t = x;
  while (x >= a) {
    t = min(dfs(x - a) + 1, t);
    a *= 6;
  }
  while (x >= b) {
    t = min(dfs(x - b) + 1, t);
    b *= 9;
  }
  memo[x] = t;
  return t;
}

int main() {
  ll n;
  cin >> n;
  ll x = dfs(n);
  cout << x << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll MOD = 1000000007;
const ll MOD2 = 998244353;
const double PI = acos(-1);
const ll INF = 1e18;
typedef pair<ll, ll> P;
typedef vector<ll> vl;
typedef vector<vl> vvl;
#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define rep(i, n) FOR(i, 0, n)
string abc = "abcdefghijklmnopqrstuvwxyz";
string ABC = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
struct edge {
  ll to, cost;
};

vl memo(100001, -1);

ll dfs(ll x) {
  if (memo[x] != -1) {
    return memo[x];
  }
  if (x == 0) {
    return 0;
  }
  if (x == 1 || x == 6 || x == 9) {
    memo[x] = 1;
    return 1;
  }
  ll a = 6, b = 9;
  ll t = x;
  while (x >= a) {
    t = min(dfs(x - a) + 1, t);
    a *= 6;
  }
  while (x >= b) {
    t = min(dfs(x - b) + 1, t);
    b *= 9;
  }
  memo[x] = t;
  return t;
}

int main() {
  ll n;
  cin >> n;
  ll x = dfs(n);
  cout << x << endl;
}
Problem p03329 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>

using namespace std;

#define M_MAX 12
#define N_MAX 59049
#define INFTY 1 << 29

int main() {
  int n, m = 12;
  int coin[13] = {0, 1, 6, 9, 36, 81, 216, 729, 1296, 6561, 7776, 46656, 59049};
  int T[N_MAX + 1];

  cin >> n;

  // 最大値で初期化
  for (int i = 0; i < N_MAX; ++i) {
    T[i] = INFTY;
  }

  T[0] = 0;
  for (int i = 1; i <= m; ++i) {
    for (int j = 0; j + coin[i] <= n; ++j) {
      T[j + coin[i]] = min(T[j + coin[i]], T[j] + 1);
    }
  }
  cout << T[n] << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>

using namespace std;

#define M_MAX 12
#define N_MAX 100000
#define INFTY 1 << 29

int main() {
  int n, m = 12;
  int coin[13] = {0, 1, 6, 9, 36, 81, 216, 729, 1296, 6561, 7776, 46656, 59049};
  int T[N_MAX + 1];

  cin >> n;

  // 最大値で初期化
  for (int i = 0; i < N_MAX; ++i) {
    T[i] = INFTY;
  }

  T[0] = 0;
  for (int i = 1; i <= m; ++i) {
    for (int j = 0; j + coin[i] <= n; ++j) {
      T[j + coin[i]] = min(T[j + coin[i]], T[j] + 1);
    }
  }
  cout << T[n] << endl;
  return 0;
}
Problem p03329 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
// #define int long long
int MAX_N = 100100;
int INF = 1e9;

int main() {
  int N;
  cin >> N;
  vector<int> coin(1, 1);
  vector<int> dp(MAX_N, INF);
  dp[0] = 0;
  for (int i = 6; i <= MAX_N; i *= 6)
    coin.push_back(i);
  for (int i = 9; i <= MAX_N; i *= 9)
    coin.push_back(i);
  sort(coin.begin(), coin.end());

  for (int i = 0; i <= N; i++) {
    for (int j = 0; j < coin.size(); j++) {
      if (dp[i + coin[j]] > dp[i] + 1) {
        dp[i + coin[j]] = dp[i] + 1;
      }
    }
  }
  cout << dp[N] << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
// #define int long long
int MAX_N = 100100;
int INF = 1e9;

int main() {
  int N;
  cin >> N;
  vector<int> coin(1, 1);
  vector<int> dp(MAX_N, INF);
  dp[0] = 0;
  for (int i = 6; i <= MAX_N; i *= 6)
    coin.push_back(i);
  for (int i = 9; i <= MAX_N; i *= 9)
    coin.push_back(i);
  sort(coin.begin(), coin.end());

  for (int i = 0; i <= N; i++) {
    for (int j = 0; j < coin.size(); j++) {
      if (i + coin[j] >= MAX_N)
        continue;
      if (dp[i + coin[j]] > dp[i] + 1) {
        dp[i + coin[j]] = dp[i] + 1;
      }
    }
  }
  cout << dp[N] << endl;
  return 0;
}
Problem p03329 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
using namespace std;
int dp[100001];
int main(void) {
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  for (int i = 0; i < N; i++)
    dp[i] = N;
  dp[N] = 0;
  int a[11] = {6, 36, 216, 1296, 7776, 46656, 9, 81, 729, 6561, 59049};
  sort(a, a + 11);
  for (int i = 10; i >= 0; i--) {
    for (int j = 1; j <= N; j++) {
      int m = j / a[i];
      for (int k = 1; k <= m; k++) {
        dp[j - k * a[i]] = min(dp[j - k * a[i]], dp[j] + k);
      }
    }
  }
  int ret = N;
  for (int i = 0; i <= N; i++) {
    ret = min(ret, dp[i] + i);
  }
  cout << ret << endl;
}

Fixed submission:
#include <algorithm>
#include <iostream>
using namespace std;
int dp[100001];
int main(void) {
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  for (int i = 0; i < N; i++)
    dp[i] = N;
  dp[N] = 0;
  int a[11] = {6, 36, 216, 1296, 7776, 46656, 9, 81, 729, 6561, 59049};
  sort(a, a + 11);
  for (int i = 10; i >= 0; i--) {
    for (int j = 1; j <= N; j++) {
      int m = j / a[i];
      for (int k = 1; k <= min(m, 8); k++) {
        dp[j - k * a[i]] = min(dp[j - k * a[i]], dp[j] + k);
      }
    }
  }
  int ret = N;
  for (int i = 0; i <= N; i++) {
    ret = min(ret, dp[i] + i);
  }
  cout << ret << endl;
}
Problem p03329 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define p(s) cout << (s) << endl
#define REP(i, n, N) for (int i = n; i < N; i++)
#define RREP(i, n, N) for (int i = N - 1; i >= n; i--)
#define CK(n, a, b) ((a) <= (n) && (n) < (b))
#define F first
#define S second
typedef long long ll;
using namespace std;
const int inf = 1e9 + 7;

int N;
vector<int> vt;
int ans;
int dp[100010];
int main() {
  cin >> N;
  REP(i, 0, N + 1) { dp[i] = inf; }
  int now = 1;
  vt.push_back(1);
  for (int i = 6; i <= N; i *= 6) {
    vt.push_back(i);
    // p(i);
  }
  for (int i = 9; i <= N; i *= 9) {
    vt.push_back(i);
    // p(i);
  }
  sort(vt.begin(), vt.end());
  ans = N;
  dp[0] = 0;
  REP(i, 0, N) {
    for (auto v : vt)
      dp[i + v] = min(dp[i + v], dp[i] + 1);
  }
  p(dp[N]);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define p(s) cout << (s) << endl
#define REP(i, n, N) for (int i = n; i < N; i++)
#define RREP(i, n, N) for (int i = N - 1; i >= n; i--)
#define CK(n, a, b) ((a) <= (n) && (n) < (b))
#define F first
#define S second
typedef long long ll;
using namespace std;
const int inf = 1e9 + 7;

int N;
vector<int> vt;
int ans;
int dp[100010];
int main() {
  cin >> N;
  REP(i, 0, N + 1) { dp[i] = inf; }
  int now = 1;
  vt.push_back(1);
  for (int i = 6; i <= N; i *= 6) {
    vt.push_back(i);
    // p(i);
  }
  for (int i = 9; i <= N; i *= 9) {
    vt.push_back(i);
    // p(i);
  }
  sort(vt.begin(), vt.end());
  ans = N;
  dp[0] = 0;
  REP(i, 0, N) {
    for (auto v : vt)
      if (i + v <= N)
        dp[i + v] = min(dp[i + v], dp[i] + 1);
  }
  p(dp[N]);
  return 0;
}
Problem p03329 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define forx(i, a, b) for (int i = (a); i < (b); i++)
#define rep(j, n) for (int j = 0; j < (n); j++)

typedef long long ll;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int n;
  cin >> n;
  bool dp[31][100001];
  dp[0][0] = true;
  rep(i, 31) rep(j, 100001) {
    if (dp[i][j] == true) {
      dp[i + 1][j] = true;
      int s = 1;
      for (; s + j <= 100000;) {
        dp[i + 1][j + s] = true;
        s *= 6;
      }
      s = 9;
      for (; s + j <= 100000;) {
        dp[i + 1][j + s] = true;
        s *= 9;
      }
    }
  }
  rep(i, 31) if (dp[i][n] == true) {
    cout << i << endl;
    break;
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define forx(i, a, b) for (int i = (a); i < (b); i++)
#define rep(j, n) for (int j = 0; j < (n); j++)

typedef long long ll;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int n;
  cin >> n;
  bool dp[35][100010];
  dp[0][0] = true;
  rep(i, 31) rep(j, 100001) {
    if (dp[i][j] == true) {
      dp[i + 1][j] = true;
      int s = 1;
      for (; s + j <= 100000;) {
        dp[i + 1][j + s] = true;
        s *= 6;
      }
      s = 9;
      for (; s + j <= 100000;) {
        dp[i + 1][j + s] = true;
        s *= 9;
      }
    }
  }
  rep(i, 31) if (dp[i][n] == true) {
    cout << i << endl;
    break;
  }
  return 0;
}
Problem p03329 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int INF = 1e9 + 7, MOD = 1e9 + 7;
const long long LINF = 1e18;
const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
int Pow6[10] = {}, Pow9[10] = {};
int Func(int n, int Ans) {
  if (n < 6) {
    return Ans + n;
  }
  int Cmp6, Cmp9;
  for (int i = 0; i < 10; i++) {
    if (Pow6[i] <= n) {
      Cmp6 = Pow6[i];
    } else
      break;
  }
  for (int i = 0; i < 10; i++) {
    if (Pow9[i] <= n) {
      Cmp9 = Pow9[i];
    } else
      break;
  }
  Ans++;
  return min(Func(n - Cmp6, Ans), Func(n - Cmp9, Ans));
}
signed main() {
  int Cmp = 1;
  Pow6[0] = 1, Pow9[0] = 1;
  for (int i = 1; i < 100; i++) {
    if (Cmp * 6 <= 100000) {
      Pow6[i] = Cmp * 6;
      Cmp *= 6;
    } else {
      break;
    }
  }
  Cmp = 1;
  for (int i = 1; i < 100; i++) {
    if (Cmp * 9 <= 100000) {
      Pow9[i] = Cmp * 9;
      Cmp *= 9;
    } else {
      break;
    }
  }
  int n;
  cin >> n;
  cout << Func(n, 0) << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int INF = 1e9 + 7, MOD = 1e9 + 7;
const long long LINF = 1e18;
const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
int Pow6[10] = {}, Pow9[10] = {};
int Func(int n, int Ans) {
  if (n < 6) {
    return Ans + n;
  }
  int Cmp6, Cmp9;
  for (int i = 0; i < 10; i++) {
    if (Pow6[i] <= n) {
      Cmp6 = Pow6[i];
    } else
      break;
  }
  for (int i = 0; i < 10; i++) {
    if (Pow9[i] <= n) {
      Cmp9 = Pow9[i];
    } else
      break;
  }
  Ans++;
  return min(Func(n - Cmp6, Ans), Func(n - Cmp9, Ans));
}
signed main() {
  int Cmp = 1;
  Pow6[0] = 1, Pow9[0] = 1;
  for (int i = 1; i < 100; i++) {
    if (Cmp * 6 <= 100000) {
      Pow6[i] = Cmp * 6;
      Cmp *= 6;
    } else {
      break;
    }
  }
  Cmp = 1;
  for (int i = 1; i < 100; i++) {
    if (Cmp * 9 <= 100000) {
      Pow9[i] = Cmp * 9;
      Cmp *= 9;
    } else {
      break;
    }
  }
  // for (int i = 0; i < 10; i++) {
  // 	cout << Pow6[i] << " ";
  // }
  // cout << endl;
  // for (int i = 0; i < 10; i++) {
  // 	cout << Pow9[i] << " ";
  // }
  // cout << endl;
  Pow6[7] = INF, Pow9[6] = INF;
  int n;
  cin >> n;
  cout << Func(n, 0) << endl;
  return 0;
}
Problem p03329 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>
#define LL long long
int N;
int ans;
std::vector<int> M;
int D[100010];
int main() {
  std::cin >> N;
  for (int i = 1; i <= N; i++) {
    D[i] = 100010;
  }
  int m = 1;
  while (m <= N) {
    for (int i = 0; i < N; i++) {
      D[i + m] = std::min(D[i + m], D[i] + 1);
    }
    m *= 6;
  }
  m = 9;
  while (m <= N) {
    for (int i = 0; i < N; i++) {
      D[i + m] = std::min(D[i + m], D[i] + 1);
    }
    m *= 9;
  }
  std::cout << D[N] << std::endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
#define LL long long
int N;
int ans;
std::vector<int> M;
int D[1000000];
int main() {
  std::cin >> N;
  for (int i = 1; i <= N; i++) {
    D[i] = 100010;
  }
  int m = 1;
  while (m <= N) {
    for (int i = 0; i < N; i++) {
      D[i + m] = std::min(D[i + m], D[i] + 1);
    }
    m *= 6;
  }
  m = 9;
  while (m <= N) {
    for (int i = 0; i < N; i++) {
      D[i + m] = std::min(D[i + m], D[i] + 1);
    }
    m *= 9;
  }
  std::cout << D[N] << std::endl;
  return 0;
}
Problem p03329 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <functional>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int n;
  cin >> n;
  int dp[n + 1];
  for (int i = 2; i <= n; i++)
    dp[i] = n;

  vector<int> nums(1);
  nums[0] = 1;
  int tmp;

  for (int i = 1;; i++) {
    tmp = pow(6, i);
    if (tmp <= n)
      nums.push_back(tmp);
    else
      break;
  }
  for (int i = 1;; i++) {
    tmp = pow(9, i);
    if (tmp <= n)
      nums.push_back(tmp);
    else
      break;
  }
  sort(nums.begin(), nums.end());

  dp[0] = 0;
  dp[1] = 1;
  dp[6] = 1;
  dp[9] = 1;

  for (int i = 2; i <= n; i++) {
    for (auto x = nums.begin(); x != nums.end(); x++) {
      tmp = i - *x;
      if (tmp >= 0)
        dp[i] = min(dp[i], dp[tmp] + 1);
      else
        break;
    }
  }

  cout << dp[n] << "\n";

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <functional>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int n;
  cin >> n;
  int dp[n + 1];
  for (int i = 2; i <= n; i++)
    dp[i] = n;

  vector<int> nums(1);
  nums[0] = 1;
  int tmp;

  for (int i = 1;; i++) {
    tmp = pow(6, i);
    if (tmp <= n)
      nums.push_back(tmp);
    else
      break;
  }
  for (int i = 1;; i++) {
    tmp = pow(9, i);
    if (tmp <= n)
      nums.push_back(tmp);
    else
      break;
  }
  sort(nums.begin(), nums.end());

  dp[0] = 0;
  dp[1] = 1;

  for (int i = 2; i <= n; i++) {
    for (auto x = nums.begin(); x != nums.end(); x++) {
      tmp = i - *x;
      if (tmp >= 0)
        dp[i] = min(dp[i], dp[tmp] + 1);
      else
        break;
    }
  }

  cout << dp[n] << "\n";

  return 0;
}
Problem p03329 (C++)

Buggy submission (status: Runtime Error):
// Dijkstra
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> P;
#define MOD 1e9 + 7;
#define INF (1 << 30)
#define REP(i, m, n) for (int i = (int)m; i < (int)n; ++i)
#define rep(i, n) REP(i, 0, n)

typedef vector<vector<P>> Graph;
vector<int> dijkstra(int s, Graph &G) {
  priority_queue<P, vector<P>, greater<P>> que;
  int n = G.size();
  vector<int> dist(n);
  dist.assign(n, INF);
  dist[s] = 0;
  que.push(P(0, s));

  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.second;
    rep(i, G[v].size()) {
      if (dist[G[v][i].first] > dist[v] + G[v][i].second) {
        dist[G[v][i].first] = dist[v] + G[v][i].second;
        que.push(P(dist[G[v][i].first], G[v][i].first));
      }
    }
  }
  return dist;
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int n;
  cin >> n;
  Graph edge(n + 1);
  rep(i, n + 1) {
    edge[i].push_back(P(i + 1, 1));

    int x = 6;
    while (i + x <= n)
      x *= 6;
    edge[i].push_back(P(i + x / 6, 1));
    int y = 9;
    while (i + y <= n)
      y *= 9;
    edge[i].push_back(P(i + y / 9, 1));
  }
  vector<int> dist = dijkstra(0, edge);
  cout << dist[n] << '\n';
  return 0;
}

Fixed submission:
// Dijkstra
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> P;
#define MOD 1e9 + 7;
#define INF (1 << 30)
#define REP(i, m, n) for (int i = (int)m; i < (int)n; ++i)
#define rep(i, n) REP(i, 0, n)

typedef vector<vector<P>> Graph;
vector<int> dijkstra(int s, Graph &G) {
  priority_queue<P, vector<P>, greater<P>> que;
  int n = G.size();
  vector<int> dist(n);
  dist.assign(n, INF);
  dist[s] = 0;
  que.push(P(0, s));

  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.second;
    rep(i, G[v].size()) {
      if (dist[G[v][i].first] > dist[v] + G[v][i].second) {
        dist[G[v][i].first] = dist[v] + G[v][i].second;
        que.push(P(dist[G[v][i].first], G[v][i].first));
      }
    }
  }
  return dist;
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int n;
  cin >> n;
  Graph edge(100010);
  rep(i, 100010) {
    edge[i].push_back(P(i + 1, 1));

    int x = 6;
    while (i + x <= n)
      x *= 6;
    edge[i].push_back(P(i + x / 6, 1));
    int y = 9;
    while (i + y <= n)
      y *= 9;
    edge[i].push_back(P(i + y / 9, 1));
  }
  vector<int> dist = dijkstra(0, edge);
  cout << dist[n] << '\n';
  return 0;
}
Problem p03329 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <map>

using namespace std;

map<int, int> memo;

int count(int n) {
  if (memo.find(n) != memo.end())
    return memo[n];

  int m6 = 6;
  int m9 = 9;
  while (m6 * 6 <= n) {
    m6 *= 6;
  }
  while (m9 * 9 <= n) {
    m9 *= 9;
  }

  int c6 = 1 + count(n - m6);
  int c9 = 1 + count(n - m9);
  memo[n] = min(c6, c9);
  return min(c6, c9);
}

int main() {
  memo[1] = 1;
  memo[2] = 2;
  memo[3] = 3;
  memo[4] = 4;
  memo[5] = 5;
  memo[6] = 1;
  memo[7] = 2;
  memo[8] = 3;
  memo[9] = 1;
  int n;
  cin >> n;
  cout << count(n) << endl;
}


Fixed submission:
#include <iostream>
#include <map>

using namespace std;

map<int, int> memo;

int count(int n) {
  if (memo.find(n) != memo.end())
    return memo[n];

  int m6 = 6;
  int m9 = 9;
  while (m6 * 6 <= n) {
    m6 *= 6;
  }
  while (m9 * 9 <= n) {
    m9 *= 9;
  }

  int c6 = 1 + count(n - m6);
  int c9 = 1 + count(n - m9);
  memo[n] = min(c6, c9);
  return min(c6, c9);
}

int main() {
  memo[0] = 0;
  memo[1] = 1;
  memo[2] = 2;
  memo[3] = 3;
  memo[4] = 4;
  memo[5] = 5;
  memo[6] = 1;
  memo[7] = 2;
  memo[8] = 3;
  memo[9] = 1;
  int n;
  cin >> n;
  cout << count(n) << endl;
}
Problem p03329 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

int findSumOfDigits6(int n) {
  int sum = 0;
  while (n > 0) {
    sum += n % 6;
    n /= 6;
  }
  return sum;
}
int findSumOfDigits9(int n) {
  int sum = 0;
  while (n > 0) {
    sum += n % 9;
    n /= 9;
  }
  return sum;
}
int main() {
  int N;
  cin >> N;
  int kaisuu = N;
  for (int i = 0; i <= N; ++i) {
    for (int j = 0; j <= N - i; ++j) {
      int x = findSumOfDigits6(i);
      int y = findSumOfDigits9(j);
      int s = N - i - j + x + y;
      kaisuu = min(kaisuu, s);
    }
  }
  cout << kaisuu << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int findSumOfDigits6(int n) {
  int sum = 0;
  while (n > 0) {
    sum += n % 6;
    n /= 6;
  }
  return sum;
}
int findSumOfDigits9(int n) {
  int sum = 0;
  while (n > 0) {
    sum += n % 9;
    n /= 9;
  }
  return sum;
}
int main() {
  int N;
  cin >> N;
  int kaisuu = N;
  for (int i = 0; i <= N; ++i) {
    int x = findSumOfDigits6(i);
    int y = findSumOfDigits9(N - i);
    int s = x + y;
    kaisuu = min(kaisuu, s);
  }
  cout << kaisuu << endl;
}
Problem p03329 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define pb push_back
using ll = long long;
using P = pair<int, int>;

const int MAX = 110000;
int dp[MAX];

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < MAX; ++i)
    dp[i] = n;
  dp[0] = 0;
  // dp[i]を計算
  for (int i = 1; i <= n; ++i) {
    for (int pow6 = 1; pow6 <= n; pow6 *= 6)
      dp[n] = min(dp[i], dp[i - pow6] + 1);
    for (int pow9 = 1; pow9 <= n; pow9 *= 9)
      dp[n] = min(dp[i], dp[i - pow9] + 1);
  }
  cout << dp[n] << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define pb push_back
using ll = long long;
using P = pair<int, int>;

const int MAX = 110000;
int dp[MAX];

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < MAX; ++i)
    dp[i] = n;
  dp[0] = 0;
  // dp[i]を計算
  for (int i = 1; i <= n; ++i) {
    for (int pow6 = 1; pow6 <= i; pow6 *= 6)
      dp[i] = min(dp[i], dp[i - pow6] + 1);
    for (int pow9 = 1; pow9 <= i; pow9 *= 9)
      dp[i] = min(dp[i], dp[i - pow9] + 1);
  }
  cout << dp[n] << endl;
}
Problem p03329 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  long long n;
  cin >> n;

  vector<long long> dp(1e9, 0);

  long long a = 1, b = 6, c = 9;

  for (long long i = 1; i <= n; i++) {
    if (i >= b * 6)
      b = b * 6;
    if (i >= c * 9)
      c = c * 9;

    if (i <= 5)
      dp[i] = i;
    else if (i <= 8)
      dp[i] = min(dp[i - a] + 1, dp[i - b] + 1);
    else {
      dp[i] = min(dp[i - a] + 1, dp[i - b] + 1);
      dp[i] = min(dp[i], dp[i - c] + 1);
    }
  }

  cout << dp[n] << endl;
  return 0;
}

Fixed submission:
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  long long n;
  cin >> n;

  vector<long long> dp(1e6 + 1, 0);

  long long a = 1, b = 6, c = 9;

  for (long long i = 1; i <= n; i++) {
    if (i >= b * 6)
      b = b * 6;
    if (i >= c * 9)
      c = c * 9;

    if (i <= 5)
      dp[i] = i;
    else if (i <= 8)
      dp[i] = min(dp[i - a] + 1, dp[i - b] + 1);
    else {
      dp[i] = min(dp[i - a] + 1, dp[i - b] + 1);
      dp[i] = min(dp[i], dp[i - c] + 1);
    }
  }

  cout << dp[n] << endl;
  return 0;
}
Problem p03329 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, N) for (int i = 0; i < (N); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
using namespace std;
const long long MOD = 1e9 + 7;
const long long INF = 1e12;
const int inf = 1e9;
const int mod = 1e9 + 7;
typedef long long ll;
typedef pair<ll, int> P;
typedef set<int> S;
int main() {
  int n;
  cin >> n;
  // cout<<n<<endl;
  int dp[100010];
  rep(i, 1000010) dp[i] = inf;
  dp[0] = 0;
  for (int i = 1; i <= n; i++) {
    // cout<<"Yes"<<n<<endl;
    dp[i] = dp[i - 1] + 1;
    for (int j = 6; j <= i; j *= 6) {
      if (i - j >= 0) {
        dp[i] = min(dp[i - j] + 1, dp[i]);
      }
    }
    for (int j = 9; j <= i; j *= 9) {
      if (i - j >= 0) {
        dp[i] = min(dp[i - j] + 1, dp[i]);
      }
    }
  }
  // cout<<endl;
  // cout<<"dp end"<<n<<endl;
  // ep(i,n+1) cout<<dp[i]<<" ";
  // cout<<endl<<n<<endl;
  // cout<<endl;
  cout << dp[n] << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, N) for (int i = 0; i < (N); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
using namespace std;
const long long MOD = 1e9 + 7;
const long long INF = 1e12;
const int inf = 1e9;
const int mod = 1e9 + 7;
typedef long long ll;
typedef pair<ll, int> P;
typedef set<int> S;
int main() {
  int n;
  cin >> n;
  // cout<<n<<endl;
  int dp[100010];
  for (int i = 0; i < 100010; i++)
    dp[i] = inf;
  dp[0] = 0;
  for (int i = 1; i <= n; i++) {
    // cout<<"Yes"<<n<<endl;
    dp[i] = dp[i - 1] + 1;
    for (int j = 6; j <= i; j *= 6) {
      if (i - j >= 0) {
        dp[i] = min(dp[i - j] + 1, dp[i]);
      }
    }
    for (int j = 9; j <= i; j *= 9) {
      if (i - j >= 0) {
        dp[i] = min(dp[i - j] + 1, dp[i]);
      }
    }
  }
  // cout<<endl;
  // cout<<"dp end"<<n<<endl;
  // ep(i,n+1) cout<<dp[i]<<" ";
  // cout<<endl<<n<<endl;
  // cout<<endl;
  cout << dp[n] << endl;
  return 0;
}
Problem p03329 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {
  int N;
  cin >> N;
  vector<int> dp(N);
  dp[0] = 0;
  for (int i = 1; i <= N; ++i) {
    dp[i] = i;
    int power = 1;
    while (power <= i) {
      dp[i] = min(dp[i], dp[i - power] + 1);
      power *= 6;
    }
    power = 1;
    while (power <= i) {
      dp[i] = min(dp[i], dp[i - power] + 1);
      power *= 9;
    }
  }
  cout << dp[N] << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int main() {
  int N;
  cin >> N;
  vector<int> dp(N + 1);
  dp[0] = 0;
  for (int i = 1; i <= N; ++i) {
    dp[i] = i;
    int power = 1;
    while (power <= i) {
      dp[i] = min(dp[i], dp[i - power] + 1);
      power *= 6;
    }
    power = 1;
    while (power <= i) {
      dp[i] = min(dp[i], dp[i - power] + 1);
      power *= 9;
    }
  }
  cout << dp[N] << endl;
}
Problem p03329 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bits/stdc++.h>
#include <iostream>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
#include <string>
using namespace std;

int main() {
  int N, min, dp[100000000] = {};
  vector<int> v;
  cin >> N;
  dp[1] = 1;
  v.push_back(1);
  for (int i = 6; i <= N; i *= 6) {
    dp[i] = 1;
    v.push_back(i);
  }
  for (int i = 9; i <= N; i *= 9) {
    dp[i] = 1;
    v.push_back(i);
  }
  for (int i = 1; i <= N; i++) {
    for (int j = 0; j < v.size(); j++) {
      if (dp[i + v[j]] == 0) {
        dp[i + v[j]] = dp[i] + 1;
      } else if (dp[i + v[j]] > dp[i] + 1) {
        dp[i + v[j]] = dp[i] + 1;
      }
    }
  }
  cout << dp[N] << endl;
}

Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
#include <iostream>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
#include <string>
using namespace std;

int main() {
  int N, min, dp[300000] = {};
  vector<int> v;
  cin >> N;
  dp[1] = 1;
  v.push_back(1);
  for (int i = 6; i <= N; i *= 6) {
    dp[i] = 1;
    v.push_back(i);
  }
  for (int i = 9; i <= N; i *= 9) {
    dp[i] = 1;
    v.push_back(i);
  }
  for (int i = 1; i <= N; i++) {
    for (int j = 0; j < v.size(); j++) {
      if (dp[i + v[j]] == 0) {
        dp[i + v[j]] = dp[i] + 1;
      } else if (dp[i + v[j]] > dp[i] + 1) {
        dp[i + v[j]] = dp[i] + 1;
      }
    }
  }
  cout << dp[N] << endl;
}
Problem p03329 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"
using namespace std;

int main() {
  int N;
  cin >> N;

  int ans = N;
  for (int i = 0; i <= N / 6; i++) {
    for (int j = 0; j <= N / 9; j++) {
      int cnt6 = 0;
      int cnt9 = 0;
      int tmp6 = i;
      int tmp9 = j;
      while (tmp6 != 0) {
        cnt6 += tmp6 % 6;
        tmp6 /= 6;
      }
      while (tmp9 != 0) {
        cnt9 += tmp9 % 9;
        tmp9 /= 9;
      }
      int cnt1 = N - i * 6 - j * 9;
      if (cnt1 >= 0)
        ans = min(ans, cnt1 + cnt6 + cnt9);
    }
  }

  cout << ans << endl;
}

Fixed submission:
#include "bits/stdc++.h"
using namespace std;

int main() {
  int N;
  cin >> N;

  int ans = N;
  for (int i = 0; i * 6 <= N; i++) {
    for (int j = 0; i * 6 + j * 9 <= N; j++) {
      int cnt6 = 0;
      int cnt9 = 0;
      int tmp6 = i;
      int tmp9 = j;
      while (tmp6 != 0) {
        cnt6 += tmp6 % 6;
        tmp6 /= 6;
      }
      while (tmp9 != 0) {
        cnt9 += tmp9 % 9;
        tmp9 /= 9;
      }
      int cnt1 = N - i * 6 - j * 9;
      if (cnt1 >= 0)
        ans = min(ans, cnt1 + cnt6 + cnt9);
    }
  }

  cout << ans << endl;
}