Advertisement
Ritam_C

One Based Arithmetic

Jan 1st, 2022
1,235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.56 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. typedef long long ll;
  3. typedef unsigned long long ull;
  4. #define pb push_back
  5. #define vll vector<ll>
  6. #define vi vector<int>
  7. #define tests int t; cin >> t; while(t--)
  8. using namespace std;
  9.  
  10. vll a(18);
  11.  
  12. ll dfs(ll n, ll k) {
  13.     ll m = n/a[k];
  14.     n = n%a[k];
  15.     if(n == 0) return m*k;
  16.     return m*k+min(k+dfs(a[k]-n, k-1), dfs(n, k-1));
  17. }
  18.  
  19. int main() {
  20.     ios_base::sync_with_stdio(false);
  21.     cin.tie(NULL); cout.tie(NULL);
  22.     ll n; cin >> n;
  23.     a[0] = 0;
  24.     for(int i = 1; i < 18; i++) {
  25.         a[i] = a[i-1]*10LL+1;
  26.     }
  27.     cout << dfs(n, 17);
  28.     return 0;
  29. }
  30.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement