fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define pb push_back
  5. #define all(x) x.begin(), x.end()
  6. #include <ext/pb_ds/assoc_container.hpp>
  7. #include <ext/pb_ds/tree_policy.hpp>
  8. using namespace __gnu_pbds;
  9. template <typename T> using o_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
  10. template <typename T, typename R> using o_map = tree<T, R, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
  11. typedef long long ll;
  12.  
  13. #define inf 1e9
  14. #define MOD 1000000007
  15. #define vint vector<int>
  16. #define vll vector<ll>
  17. #define no cout << "NO" << endl;
  18. ll sum (ll n ) {
  19. if (n < 10 ) return n*(n+1) /2 ;
  20. string s = to_string (n);
  21. ll sz = s.size() ;
  22. ll d = pow (10 , sz-1) ;
  23. ll x = n /d ;
  24. ll ans1 = 45 * pow (10 , sz -2 ) *(sz-1 ) *x ;
  25. ll ans2 = x*(x-1) * d ;
  26. ll ans3 = x * (n % d +1) ;
  27. return ans1 + ans2 + ans3 + sum(n % d ) ;
  28.  
  29. }
  30.  
  31. void solve(){
  32. int n ; cin >> n ;
  33. cout << sum(n) << endl ;
  34. }
  35.  
  36.  
  37.  
  38. int main() {
  39. ios::sync_with_stdio(false);
  40. cin.tie(nullptr);
  41.  
  42. #ifndef ONLINE_JUDGE
  43. freopen("input.txt", "r", stdin);
  44. freopen("output.txt", "w", stdout);
  45. #endif
  46.  
  47. int t = 1;
  48.  
  49. cin >> t ;
  50. while (t--) {
  51. solve();
  52. }
  53. return 0;
  54. }
  55.  
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
0