Skip to content

Instantly share code, notes, and snippets.

@surinoel
Created September 22, 2019 05:32
Show Gist options
  • Save surinoel/9f4a40b4b703f93e441b7b7f6ce88d89 to your computer and use it in GitHub Desktop.
Save surinoel/9f4a40b4b703f93e441b7b7f6ce88d89 to your computer and use it in GitHub Desktop.
#include <cstring>
#include <iostream>
using namespace std;
int d[1000001];
int go(int n) {
if (n == 1) {
return 0;
}
if (d[n] != -1) {
return d[n];
}
int ans = 1e7;
if (n % 3 == 0) {
int tmp = go(n / 3) + 1;
if (ans > tmp) {
ans = tmp;
}
}
if (n % 2 == 0) {
int tmp = go(n / 2) + 1;
if (ans > tmp) {
ans = tmp;
}
}
if (n - 1 >= 1) {
int tmp = go(n - 1) + 1;
if (ans > tmp) {
ans = tmp;
}
}
d[n] = ans;
return ans;
}
int main(void) {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
memset(d, -1, sizeof(d));
cout << go(n) << '\n';
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment