题目链接
其实显而易见,答案是不会很大的。
按贪心的思想就是优先减去该数当前最大的6^k 或者 9^p 做记忆化搜索即可。
#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
using namespace std;
typedef pair < int , int > pii;
typedef vector < int > vec;
typedef vector < pii > veg;
typedef long long ll;
int t;
ll n;
unordered_map < ll , int > f;
int calc(ll x) {
if(x < 6) return x;
if(x == 6 || x == 9) return 1;
if(f.count(x)) return f[x];
int res1 = 0,res2 = 0;
for(ll i = 1;i <= x;i *= 6,res1++); res1--;
for(ll i = 1;i <= x;i *= 9,res2++); res2--;
f[x] = min(calc(x - pow(6,res1)) + 1,calc(x - pow(9,res2)) + 1);
return f[x];
}
void solve() {
cin >> n;
printf("%d\n",calc(n));
}
int main() {
#ifndef ONLINE_JUDGE
freopen("1.in","r",stdin);
#endif
cin >> t; for(;t;t--) solve(); return 0;
}