Classy Numbers Codeforces Round 50 (数位DP)

版权声明:本文为博主瞎写的,请随便转载 https://blog.csdn.net/sdut_jk17_zhangming/article/details/82694828

output

standard output

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].

Example

input

4
1 1000
1024 1024
65536 65536
999999 1000001

output

1000
1
0
2

数位DP 直接上板子

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

ll a[20];// 每一位的上限
ll dp[20][29]; //记忆化
ll dfs(ll pos,ll prev,ll limit)
{//     当前第几位  数位条件   是否受上限
    ll i;
    if(pos==0)
    {
        if(prev >= 4) return 0;
        else return 1;
    }
    if(!limit&&dp[pos][prev] != -1) return dp[pos][prev];
    ll up;
    ll ans = 0;
    up = limit?a[pos]:9;
    for(i=0;i<=up;i++)
    {
        if(i==0) ans += dfs(pos-1,prev,limit && i==up);
        else ans += dfs(pos-1,prev+1,limit && i==up);
    }
    if(!limit) dp[pos][prev] = ans;
    return ans;
}

ll solve(ll x)
{
    ll p = 0;
    while(x)
    {
        a[++p] = x%10;
        x /= 10;
    }
    return dfs(p,0,1);
}

int main()
{
    ll t,a,b;
    scanf("%lld",&t);
    memset(dp,-1,sizeof(dp));
    while(t--)
    {
        scanf("%lld%lld",&a,&b);
       // cout<<solve(a-1)<<endl;
       // cout<<solve(b)<<endl;
        printf("%lld\n",solve(b) - solve(a-1));
    }
    return 0;

}

猜你喜欢

转载自blog.csdn.net/sdut_jk17_zhangming/article/details/82694828