P2602 [ZJOI2010]数字计数
自己独立做出来的第一道数位dp(虽然一共做了3道qwq
#include <bits/stdc++.h> #define inf 2333333333333333 #define N 1000010 #define p(a) putchar(a) #define For(i,a,b) for(long long i=a;i<=b;++i) //by war //2020.7.14 using namespace std; long long l,r,t; long long f[20][20][20],ans[20]; vector<long long>dim,ki; struct node{ long long k[10]; }g[20][20][20]; void in(long long &x){ long long y=1;char c=getchar();x=0; while(c<'0'||c>'9'){if(c=='-')y=-1;c=getchar();} while(c<='9'&&c>='0'){ x=(x<<1)+(x<<3)+c-'0';c=getchar();} x*=y; } void o(long long x){ if(x<0){p('-');x=-x;} if(x>9)o(x/10); p(x%10+'0'); } long long dfs(long long x,long long st,long long op){ if(!x) return 1; if(~f[x][st][op]){ For(i,0,9) ans[i]+=g[x][st][op].k[i]; return f[x][st][op]; } long long maxx=op?dim[x]:9,r=0,k=0; For(i,0,9) g[x][st][op].k[i]=ans[i]; For(i,0,maxx){ if(st==11 && i==0) dfs(x-1,11,op&(i==maxx)); else{ r+=dfs(x-1,i,op&(i==maxx)); ans[i]+=t*r; k+=r; r=0; } } For(i,0,9) g[x][st][op].k[i]=ans[i]-g[x][st][op].k[i]; return f[x][st][op]=k; } void deal(long long x){ memset(f,-1,sizeof f); memset(g,0,sizeof g); dim.clear(); dim.push_back(666); while(x){ dim.push_back(x%10); x/=10; } dfs(dim.size()-1,11,1); } signed main(){ in(l);in(r); t=1; deal(r); t=-1; deal(l-1); For(i,0,9) o(ans[i]),p(' '); return 0; }