|
| 1 | + |
| 2 | +//------------------------------------------------------------------------------ |
| 3 | +// #include <iostream> |
| 4 | +// #include <vector> |
| 5 | +#include <bits/stdc++.h> |
| 6 | +// #include <cmath> |
| 7 | +// #include <algorithm> |
| 8 | +// #include <unordered_map> |
| 9 | +// #include <map> |
| 10 | +// #include <set> |
| 11 | +// #include <unordered_set> |
| 12 | +//------------------------------------------------------------------------------ |
| 13 | +using namespace std; |
| 14 | +//------------------------------------------------------------------------------ |
| 15 | +#define FastIO ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); |
| 16 | +#define v(Type) vector<Type> |
| 17 | +#define w(T) \ |
| 18 | + int T; \ |
| 19 | + cin >> T; \ |
| 20 | + while (T--) |
| 21 | +#define int long long int |
| 22 | +#define mod 1000000007ll |
| 23 | +#define endl "\n" |
| 24 | +//------------------------------------------------------------------------------ |
| 25 | +// Any fucntion can be called using Math.function_name(); |
| 26 | +//------------------------------------------------------------------------------ |
| 27 | +class Math |
| 28 | +{ |
| 29 | +public: |
| 30 | + //Returns gcd of two numbers |
| 31 | + int gcd(int a, int b) |
| 32 | + { |
| 33 | + return (a % b == 0) ? b : gcd(b, a % b); |
| 34 | + } |
| 35 | + |
| 36 | + //Returns lcm of two numbers |
| 37 | + int lcm(int a, int b) |
| 38 | + { |
| 39 | + return a * (b / gcd(a, b)); |
| 40 | + } |
| 41 | + |
| 42 | + // Returns flag array isPrime |
| 43 | + // isPrime[i] = true (if i is Prime) |
| 44 | + // isPrime[i] = false (if i is not Prime) |
| 45 | + vector<bool> *seiveOfEratosthenes(const int N) |
| 46 | + { |
| 47 | + vector<bool> *isPrime = new vector<bool>(N + 1, true); |
| 48 | + (*isPrime)[0] = (*isPrime)[1] = false; |
| 49 | + for (int i = 2; i * i <= N; ++i) |
| 50 | + if ((*isPrime)[i]) |
| 51 | + for (int j = i * i; j <= N; j += i) |
| 52 | + (*isPrime)[j] = false; |
| 53 | + |
| 54 | + return isPrime; |
| 55 | + } |
| 56 | + |
| 57 | + //Returns (x ^ n) |
| 58 | + int pow(const int &x, int n) |
| 59 | + { |
| 60 | + if (n == 0) |
| 61 | + return 1; |
| 62 | + int h = pow(x, n / 2); |
| 63 | + return (n & 1) ? h * h * x : h * h; |
| 64 | + } |
| 65 | + |
| 66 | + //Returns (x ^ n) % M |
| 67 | + int pow(const int &x, int n, const int &M) |
| 68 | + { |
| 69 | + if (n == 0) |
| 70 | + return 1; |
| 71 | + int h = pow(x, n / 2) % M; |
| 72 | + return (n & 1) ? (h * h * x) % M : (h * h) % M; |
| 73 | + } |
| 74 | + |
| 75 | + //Returns all Primes <= N |
| 76 | + vector<int> *primesUptoN(const int N) |
| 77 | + { |
| 78 | + vector<bool> *isPrime = seiveOfEratosthenes(N); |
| 79 | + vector<int> *Primes = new vector<int>; |
| 80 | + if (2 <= N) |
| 81 | + (*Primes).push_back(2); |
| 82 | + for (int i = 3; i <= N; i += 2) |
| 83 | + if ((*isPrime)[i]) |
| 84 | + (*Primes).push_back(i); |
| 85 | + return Primes; |
| 86 | + } |
| 87 | + |
| 88 | +} Math; |
| 89 | +//------------------------------------------------------------------------------ |
| 90 | + |
| 91 | +v(bool) isPrime = *Math.seiveOfEratosthenes(100); |
| 92 | + |
| 93 | +static int dp[11][2][100][4001]; |
| 94 | +int solve(string &s, int pos, bool tight, int sum, int rem, int &k) |
| 95 | +{ |
| 96 | + if (pos == s.length()) |
| 97 | + return isPrime[sum] and rem == 0; |
| 98 | + |
| 99 | + if (dp[pos][tight][sum][rem] != -1) |
| 100 | + return dp[pos][tight][sum][rem]; |
| 101 | + |
| 102 | + int ub = (tight) ? s[pos] - '0' : 9; |
| 103 | + int ans = 0; |
| 104 | + for (int i = 0; i <= ub; i++) |
| 105 | + ans += solve(s, pos + 1, tight and (i == ub), sum + i, (rem * 10 + i) % k, k); |
| 106 | + |
| 107 | + return dp[pos][tight][sum][rem] = ans; |
| 108 | +} |
| 109 | +int f(string &s, int k) |
| 110 | +{ |
| 111 | + for (int pos = 0; pos < s.length(); pos++) |
| 112 | + for (int tight = 0; tight <= 1; tight++) |
| 113 | + for (int sum = 0; sum <= 9 * s.length(); sum++) |
| 114 | + for (int rem = 0; rem < k; rem++) |
| 115 | + dp[pos][tight][sum][rem] = -1; |
| 116 | + int ans = solve(s, 0, 1, 0, 0, k); |
| 117 | + return ans; |
| 118 | +} |
| 119 | + |
| 120 | +bool check(int x) |
| 121 | +{ |
| 122 | + int sum = 0; |
| 123 | + while (x) |
| 124 | + { |
| 125 | + sum += x % 10; |
| 126 | + x /= 10; |
| 127 | + } |
| 128 | + return isPrime[sum]; |
| 129 | +} |
| 130 | + |
| 131 | +void solve() |
| 132 | +{ |
| 133 | + int a, b; |
| 134 | + cin >> a >> b; |
| 135 | + |
| 136 | + a--; |
| 137 | + string r = to_string(b); |
| 138 | + string l = to_string(a); |
| 139 | + |
| 140 | + int k; |
| 141 | + cin >> k; |
| 142 | + |
| 143 | + int ans = 0; |
| 144 | + if (k <= 4000) |
| 145 | + ans = f(r, k) - f(l, k); |
| 146 | + else |
| 147 | + { |
| 148 | + a++; |
| 149 | + int s = (a % k == 0) ? a : k + a - a % k; |
| 150 | + // cout << s << endl; |
| 151 | + while (s <= b) |
| 152 | + { |
| 153 | + ans += check(s); |
| 154 | + s += k; |
| 155 | + } |
| 156 | + } |
| 157 | + cout << ans << endl; |
| 158 | +} |
| 159 | + |
| 160 | +//------------------------------------------------------------------------------ |
| 161 | +int32_t main() |
| 162 | +{ |
| 163 | + FastIO; |
| 164 | + |
| 165 | + // w(T) |
| 166 | + solve(); |
| 167 | + |
| 168 | + return 0; |
| 169 | +} |
| 170 | +//------------------------------------------------------------------------------ |
0 commit comments