Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <algorithm>
- using namespace std;
- int main( )
- {
- int n;
- cin >> n;
- int g[n+1];
- g[1] = 0;
- g[2] = 1;
- g[3] = 1;
- for (int i = 4; i <= n; ++i)
- {
- if ((i % 3 == 0) && (i % 2 == 0))
- g[i] = min(g[i-1], min(g[i/3], g[i/2])) + 1;
- else if (i % 2 == 0)
- g[i] = min(g[i-1], g[i/2]) + 1;
- else if (i % 3 == 0)
- g[i] = min(g[i-1], g[i/3]) + 1;
- else
- g[i] = g[i-1] + 1;
- }
- cout << g[n];
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment