fork(1) download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. class LastDigit
  4. {
  5. int d = 0;
  6. long long int f(long long int x)
  7. {
  8.  
  9. long long int sum = 0;
  10. while(x != 0)
  11. {
  12. d++;
  13. sum = sum + x;
  14. //cout << sum << " ";
  15. x =x/10;
  16. }
  17. return sum;
  18. }
  19. public:
  20. long long findX(long long S)
  21. {
  22. //return f(900000000000000000);
  23. long long int i;
  24. f(S);
  25. int dig = d;
  26. int d1 = dig -1;
  27. int ans = -1;
  28. double a1 = (double)(pow(10,d1) -1) / (9*pow(10,d1-1));
  29. d1++;
  30. double a2 = (double)(pow(10,d1) -1) / (9*pow(10,d1-1));
  31. a1 = 1/a1;
  32. a2 = 1/a2;
  33. a1 = S*a1;
  34. a2 = S*a2;
  35. for(i=a1 - 100 ; i <= a1 + 100 ; i++)
  36. if(f(i) - S == (long long )0)
  37. return i;
  38. for(i = a2 - 100 ;i <= a2 + 100 ; i++)
  39. if(f(i) - S == (long long )0)
  40. return i;
  41. return ans;
  42.  
  43. }
  44. };
  45. int main()
  46. {
  47. class LastDigit ld;
  48. cout << ld.findX(837592744927492746);
  49. return 0;
  50. }
Success #stdin #stdout 0s 3468KB
stdin
Standard input is empty
stdout
-1