标签:数位 codeforce || dfs target for ref signed include
很普通的数位dp题呐,\(state\)表示\(>0\)的数位个数。
#include<bits/stdc++.h>
#define int long long
using namespace std;
int dp[20][5],a[20],cnt;
int dfs(int pos,int sta,bool lim)
{
if(pos==0 || sta==3) return 1;
if(!lim && dp[pos][sta]!=-1) return dp[pos][sta];
int up=lim?a[pos]:9,ans=0;
for(int i=0;i<=up;i++)
{
if(!i) ans+=dfs(pos-1,sta,lim && i==up);
else ans+=dfs(pos-1,sta+1,lim && i==up);
}
if(!lim) dp[pos][sta]=ans;
return ans;
}
int solve(int x)
{
memset(dp,-1,sizeof(dp)); cnt=0;
while(x) {a[++cnt]=x%10; x/=10;}
return dfs(cnt,0,1);
}
signed main()
{
int t,l,r;
scanf("%lld",&t);
while(t--)
{
scanf("%lld%lld",&l,&r);
printf("%lld\n",solve(r)-solve(l-1));
}
return 0;
}
标签:数位 codeforce || dfs target for ref signed include
原文地址:https://www.cnblogs.com/violetholmes/p/14257480.html