fork download
  1. #include <bits/stdc++.h>
  2. #define ll long long
  3. #define ld long double
  4. #define nl "\n"
  5. #define OO 0x3f3f3f3f
  6. using namespace std;
  7.  
  8. void ta2to2a()
  9. {
  10. ios_base::sync_with_stdio(false), cout.tie(nullptr), cin.tie(nullptr);
  11. #ifndef ONLINE_JUDGE
  12. freopen("input.txt", "r", stdin);
  13. freopen("output.txt", "w", stdout);
  14. #endif
  15. }
  16. ll n;
  17. vector<ll> factiorals(16);
  18. ll binary(ll x)
  19. {
  20. return __builtin_popcountll(x);
  21. }
  22. ll powerfulNumbers(int i, ll sum, ll count)
  23. {
  24. if (i == 16 || factiorals[i] > n)
  25. {
  26. return (binary(n - sum) + count);
  27. }
  28. return min(powerfulNumbers(i + 1, sum, count), powerfulNumbers(i + 1, sum + factiorals[i], count + 1));
  29. }
  30. void solve()
  31. {
  32. cin >> n;
  33. cout << powerfulNumbers(3, 0, 0) << nl;
  34. }
  35. int main()
  36. {
  37. ta2to2a();
  38. int t = 1;
  39. cin >> t;
  40. ll x = 1LL;
  41. for (int i = 1; i <= 15; i++)
  42. {
  43. x *= i;
  44. factiorals[i] = x;
  45. }
  46. while (t--)
  47. {
  48. solve();
  49. }
  50. }
Success #stdin #stdout 0s 5312KB
stdin
Standard input is empty
stdout
0