Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <cstring>
- using namespace std;
- long long dp[20][4][2];
- string S;
- long long go(int pos, int count, bool tight) {
- if (pos == S.length()) {
- return 1;
- }
- if (dp[pos][count][tight] != -1) {
- return dp[pos][count][tight];
- }
- long long ans = 0;
- int upper_bound = tight ? (S[pos] - '0') : 9;
- for (int digit = 0; digit <= upper_bound; ++digit) {
- int new_count = count + (digit > 0);
- if (new_count <= 3) {
- ans += go(pos + 1, new_count, tight && (digit == upper_bound));
- }
- }
- return dp[pos][count][tight] = ans;
- }
- long long solve(long long n) {
- if (n == 0) return 0;
- S = to_string(n);
- memset(dp, -1, sizeof(dp));
- return go(0, 0, true);
- }
- int main() {
- ios_base::sync_with_stdio(false);
- cin.tie(NULL);
- int T;
- cin >> T;
- while (T--) {
- long long L, R;
- cin >> L >> R;
- cout << solve(R) - solve(L - 1) << "\n";
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement