C. Classy Numbers -cocdeforces1036c -csdn博客
Let’s call some positive integer classy if its decimal representation contains no more than 3 non-zero digits. For example, numbers 4, 200000, 10203 are classy and numbers 4231, 102306, 7277420000 are not.
You are given a segment [L;R]. Count the number of classy integers x such that L≤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 T (1≤T≤10e4) — the number of segments in a testcase.
Each of the next T lines contains two integers Li and Ri (1≤Li≤Ri≤10e18).
Output
Print T lines — the i-th line should contain the number of classy integers on a segment [Li;Ri].
Example
input
4
1 1000
1024 1024
65536 65536
999999 1000001
output
1000
1
0
2
-
题意:如果一个数的十进制表示没有超过3个非零的数的话就称之为classy数,现在给你区间a,b求之间有多少个这样的数
-
题解:数位dp,直接套数位dp的模板即可
#include<bits/stdc++.h>
#define endl '\n'
#define pb push_back
#define mp make_pair
#define _ ios::sync_with_stdio(false)
bool SUBMIT = 1;
typedef long long ll;
using namespace std;
const double PI = acos(-1);
ll n,k[20],sn[20][2][4];
ll dp(ll l,int up,ll check)//up用来判断是否属于是否处于上界,
{//check判断已经有了几个这样的数
if(l<0)return 1;
ll c=sn[l][up][check];
if(c)return c;
ll ans=0;
if(check>=3)return sn[l][up][check]=1;
ll m;
if(up)m=k[l];
else m=9;
for(int i=0;i<=m;i++)
{
ans+=dp(l-1,up&&(i==m),check+(i==0?0:1) );
}
return sn[l][up][check]=ans;
}
ll fun(ll m)
{
ll l=0;
memset(sn,0,sizeof(sn));
while(m>0)//把整数分解
{
k[l++]=m%10;
m/=10;
}
return dp(l-1,1,0);
}
int main()
{
if(!SUBMIT)freopen("i.txt","r",stdin);else _;
cin>>n;
while(n--)
{
ll a,b;cin>>a>>b;
cout<<fun(b)-fun(a-1);<<endl;
}
return 0;
}