1036C Classy Numbers 數位DP(記憶化搜尋)
阿新 • • 發佈:2018-12-21
題意:在n到m區間內,有多少個數滿足每位數字非零的不超過3個(前導零無關)。
#include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #include<queue> #include<vector> #include<stack> #include<map> #include<set> #include<cmath> #include<cctype> #include<ctime> #define INF 0x3f3f3f3f #define PI acos(-1.0) typedef long long ll; typedef unsigned long long ull; using namespace std; int a[25]; ll dp[25][25]; ll dfs(int len,int sum,bool limit) { if(len==-1) return sum<4; if(!limit&&dp[len][sum]!=-1) return dp[len][sum]; ll ans=0; int up=limit?a[len]:9; for(int i=0;i<=up;i++) { ans+=dfs(len-1,sum+(i!=0),i==a[len]&&limit); } if(!limit) dp[len][sum]=ans; return ans; } ll fun(ll x) { int len=0; while(x) { a[len++]=x%10; x/=10; } return dfs(len-1,0,1); } int main() { ll n,m,t; cin>>t; while(t--) { cin>>n>>m; memset(dp,-1,sizeof(dp)); cout<<fun(m)-fun(n-1)<<endl; } return 0; }