1. 程式人生 > >1036C Classy Numbers(數位dp)

1036C Classy Numbers(數位dp)

描述

Let’s call some positive integer classy if its decimal representation contains no more than 33 non-zero digits. For example, numbers 44, 200000200000, 1020310203 are classy and numbers 42314231, 102306102306, 72774200007277420000 are not.

You are given a segment [L;R][L;R]. Count the number of classy integers xx such that L≤x≤RL≤x≤R.

Each testcase contains several segments, for each of them you are required to solve the problem separately.

Input

The first line contains a single integer TT (1≤T≤1041≤T≤104) — the number of segments in a testcase.

Each of the next TT lines contains two integers LiLi and RiRi (1≤Li≤Ri≤10181≤Li≤Ri≤1018).

Output

Print TT lines — the ii-th line should contain the number of classy integers on a segment [Li;Ri][Li;Ri].

input

4
1 1000
1024 1024
65536 65536
999999 1000001

output

1000
1
0
2

思路

先說題意,給你t組資料,詢問區間[L,R]內非0數字出現次數<=3次的數的個數.

定義狀態:dp[i][j]表示前i位數字,非0數字出現j次的方案數。

模板修改:

num代表當前這一位,為0數字出現的次數.

所以當pos列舉完之後,直接判斷num

是否<=3即可.

程式碼

#include <bits/stdc++.h>
using namespace std;
#define mem(a, b) memset(a, b, sizeof(a))
typedef long long ll;
const ll N = 3e5 + 10;
ll dp[20][20], a[20];
ll dfs(ll pos, ll num, ll limit)
{
    if (pos == -1)
        return num <= 3;
    if (!limit && ~dp[pos][num])
        return dp[pos][num];
    ll up = limit ? a[pos] : 9, ans = 0;
    for (ll i = 0; i <= up; i++)
        ans += dfs(pos - 1, num + (i != 0), limit && (i == up));
    return limit ? ans : dp[pos][num] = ans;
}
ll solve(ll x)
{
    ll pos = 0;
    while (x)
    {
        a[pos++] = x % 10;
        x /= 10;
    }
    return dfs(pos - 1, 0, 1);
}
int main()
{
    ll n, m, t;
    mem(dp, -1);
    scanf("%lld", &t);
    while (t--)
    {
        scanf("%lld%lld", &n, &m);
        printf("%lld\n", solve(m) - solve(n - 1));
    }
    return 0;
}