Topic links: https://www.luogu.com.cn/problem/P2602
This question is to the effect that a certain number of requirements within the interval 0-9 arise.
Statistics were counted number memory section 1 1, 2 1, 3 1 ......, there is a 2, 2 2, ......, a 1 9-9,2, ...... of the number of number, then the accumulated demand: * the number of times is accumulated.
Note that leading processing final frontier of 0 and DFS.
num: Which digital cnt: number of times now: the number of times that has occurred
recording a first dimension dp pos, recording a second dimension now, the third dimension record cnt, which kept ans.
AC Code:
1 #include<cstdio> 2 #include<iostream> 3 #include<algorithm> 4 #include<cstring> 5 using namespace std; 6 typedef long long ll; 7 ll dp[20][20][20],ans[50]; 8 int a[50]; 9 ll DFS(int pos,int now,int limit,int lead,int cnt,int num){ 10 if(pos==0) return now==cnt;//注意边界处理 11 if(!limit&&!lead&&dp[pos][now][cnt]!=-1) return dp[pos][now][cnt]; 12 int up=limit?a[pos]:9; 13 ll ans=0; 14 for(int i=0;i<=up;i++){ 15 if(lead&&i==0) ans+=DFS(pos-1,now,limit&&i==a[pos],1,cnt,num); 16 else ans+=DFS(pos-1,now+(i==num),limit&&i==a[pos],0,cnt,num); 17 } 18 if(!lead&&!limit) dp[pos][now][cnt]=ans; 19 return ans; 20 } 21 void solve(ll x,int flag){ 22 int len=0; 23 memset(dp,-1,sizeof(dp)); 24 while(x){ 25 a[++len]=x%10; 26 x/=10; 27 } 28 for(int I = 0 ; I <= . 9 ; I ++ ) { 29 for ( int J = . 1 ; J <= len; J ++) { // the number of enumeration occurring 30 IF (In Flag) ANS [I] + = J * the DFS (len, 0 , . 1 , . 1 , J, I); 31 is the else ANS [I] - = J * the DFS (len, 0 , . 1 , . 1 , J, I); 32 } 33 is } 34 is } 35 int main () { 36 LL n-, m; 37 [ Scanf ( " %% LLD LLD ",&n,&m); 38 solve(n-1,0); solve(m,1); 39 for(int i=0;i<=9;i++) printf("%lld ",ans[i]); 40 return 0; 41 }