fork(1) download
  1. #include <bits/stdc++.h>
  2. // #include "pp"
  3. using namespace std;
  4. #define INF numeric_limits<int>::max() // Infinity
  5. #define NINF numeric_limits<int>::min() // Negative Infinity
  6. #define bitcount __builtin_popcount
  7. #define gcd __gcd
  8. #define all(a) a.begin(), a.end()
  9. #define foreach(e, v) for(auto e : (v))
  10. #define sz(a) ((int)(a.size()))
  11. #define pb push_back
  12. #define checkbit(n,b) ( (n >> b) & 1)
  13. typedef vector<int> vi;
  14. typedef vector<vi> vvi;
  15. typedef pair<int,int> ii;
  16. typedef vector<ii> vii;
  17.  
  18. int main()
  19. {
  20. ios_base::sync_with_stdio(false);
  21. string n;
  22. while(cin >> n)
  23. {
  24. if (n.length() < 9)
  25. {
  26. int a = stoi(n);
  27. if (a <= 0)
  28. {
  29. cout << "0\n";
  30. continue;
  31. }
  32. if (a==1)
  33. {
  34. cout << "1\n";
  35. continue;
  36. }
  37. cout << 2*(a-1) << "\n";
  38. continue;
  39. }
  40.  
  41. n = "0" + n;
  42.  
  43. int i = n.length()-1;
  44. --n[i];
  45. for (; i > 0; --i)
  46. {
  47. if (n[i] < '0')
  48. {
  49. n[i] +=10;
  50. n[i-1]--;
  51. }
  52. else
  53. break;
  54. }
  55.  
  56. int carry = 0;
  57. i = n.length()-1;
  58. for (; i > 0; --i)
  59. {
  60. int c = n[i] - '0';
  61. c = carry + c + c;
  62.  
  63. n[i] = '0'+(c%10);
  64. carry = c/10;
  65. }
  66. if (carry)
  67. {
  68. cout << carry;
  69. }
  70. while (n[i] == '0')
  71. ++i;
  72.  
  73. cout << n.substr(i) << "\n";
  74. }
  75. return 0;
  76. }
Success #stdin #stdout 0s 3236KB
stdin
213156484546845216853218415316845648645314543213546514684564564
0
1
20
874545
10000000
1222
stdout
426312969093690433706436830633691297290629086427093029369129126
0
1
38
1749088
19999998
2442