fork download
  1. #include <iostream>
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5. bool input_numbers(long long &a, long long &b) {
  6. string line;
  7. getline(cin, line);
  8. stringstream ss(line);
  9. if (!(ss >> a >> b)) {
  10. cout << " Invalid input (not two integers)!" << endl;
  11. return false;
  12. }
  13. string extra;
  14. if (ss >> extra) {
  15. cout << "Invalid input: Only enter exactly 2 numbers per line!" << endl;
  16. return false;
  17. }
  18. if (a < 1 || b > 100000000 || a > b) {
  19. cout << "Invalid input .Please enter numbers within the range and separated by white space (1 <= a <= b <= 100,000,000)!" << endl;
  20. return false;
  21. }
  22. return true;
  23. }
  24.  
  25. long long count_claps_upto(long long n){
  26. if(n <=0) return 0;
  27. string s = to_string (n);
  28. long long total = 0 ;
  29. int L = s.length ();
  30.  
  31. for(int i = 0 ; i< L ; i++){
  32. long long place = pow(10 , L - i -1);
  33. int digit = s[i] -'0'; // convert char to int ;
  34. long long left = i > 0 ? stoll(s.substr (0 , i )) : 0 ;
  35. total += left*place*3;
  36. for(int d = 0 ; d < digit ; d++){
  37. if(d == 3 || d == 6 || d == 9) total+= place ;
  38. }
  39. if(digit == 3 || digit == 6 || digit == 9 ){
  40. long long right = (i == L -1) ? 1 : ( stoll(s.substr (i+1)) +1) ;
  41. total += right ;
  42. }
  43. }
  44. return total ;
  45. }
  46. int main()
  47. {
  48. long long a, b;
  49. if (!input_numbers(a, b)) {
  50. return 1;
  51. }
  52. long long result = count_claps_upto(b) - count_claps_upto(a - 1);
  53. cout << result << endl;
  54. return 0;
  55. }
  56.  
Success #stdin #stdout 0s 5288KB
stdin
999999 10000000
stdout
19200006