fork(1) download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. // Returns the number of digits 3,6,9 that appear from 1 to x
  5. long long countClaps(long long x) {
  6. if (x == 0) return 0;
  7.  
  8. long long res = 0;
  9. long long pow10 = 1;
  10.  
  11. while (x / pow10 > 0) {
  12. long long higher = x / (pow10 * 10);
  13. int current = (x / pow10) % 10;
  14. long long lower = x % pow10;
  15.  
  16. // Counts the number of times is 3, 6, or 9 at the current position
  17. for (int d : {3, 6, 9}) {
  18. if (current > d) {
  19. res += (higher + 1) * pow10;
  20. } else if (current == d) {
  21. res += higher * pow10 + lower + 1;
  22. } else {
  23. res += higher * pow10;
  24. }
  25. }
  26. pow10 *= 10;
  27. }
  28. return res;
  29. }
  30.  
  31. int main() {
  32. long long a, b;
  33. cin >> a >> b;
  34.  
  35. // Count the number of times the digits 3, 6, 9 appear from a to b
  36. cout << countClaps(b) - countClaps(a - 1) << endl;
  37. return 0;
  38. }
Success #stdin #stdout 0s 5324KB
stdin
999999 100000000
stdout
238200006