fork download
  1. #include <iostream>
  2. using namespace std;
  3. using ll = long long;
  4. const int N = 1e6 + 1;
  5.  
  6. int dp[N][10];
  7.  
  8.  
  9. ll f(ll x) {
  10. ll ret = 1;
  11. while (x) {
  12. ret *= max(1ll, x % 10);
  13. x /= 10;
  14. }
  15. return ret;
  16. }
  17.  
  18. ll g(ll x) {
  19. return (x < 10 ? x : g(f(x)));
  20. }
  21.  
  22. int main() {
  23.  
  24. for (int i = 1; i < N; i++) {
  25. dp[i][g(i)]++;
  26. for (int j = 1; j <= 9; j++)
  27. dp[i][j] += dp[i - 1][j];
  28. }
  29.  
  30. int q; cin >> q;
  31.  
  32. while (q--) {
  33. int l, r, k; cin >> l >> r >> k;
  34. cout << dp[r][k] - dp[l - 1][k] << endl;
  35. }
  36.  
  37. return 0;
  38. }
Success #stdin #stdout 0.05s 42552KB
stdin
4
82 94 6
56 67 4
28 59 9
39 74 4

stdout
3
1
1
5