Recently in the digital DP, feeling still full of harvest! After doing a few questions to think of themselves OJ on a topic, used to write a mathematical method, and now use a digital DP to write it again.
Topic:
1255: Digital Statistics time limit: 1 Sec memory limit: MB
Submitted: $ 4
Submitted State Title Description
The page number of a book is encoded sequentially from the natural number 1 until the natural number N. The pages of the book are arranged according to usual habits,
Each page number does not contain a redundant leading digit 0. For example, the 6th page is represented by the number 6 instead of 06 or 006. Number
The word counting problem requires that the total page number of the given book be n, the number of numbers 0, 1 to be calculated for all pages of the book,
2,...,9.
Input
Give an integer n (1≤n≤2^31-1) that represents the total page number of the book
Output
Output 10 lines, the number of times in the output page number of the K line to the number of k-1, k=1,2,...,10.
Sample input
11
Sample output
1411111111
Links: http://acm.zznu.edu.cn/problem.php?id=1255
Mathematical Methods:
In fact, assuming that every bit is 0-9 and then judged, the results are calculated.
#include <stdio.h>intSlove (intNumintk) { intL, R, M, P =1, a =num; intsum =0; while(a) {R= num%Q; M= a%Ten; L= A/Ten; if(k) {if(K <M) Sum+ = (L +1)*P; Else if(k = =M) Sum+ = L*p + (r+1); Elsesum+ = l*Q; } Else { if(a<Ten) Break; if(k = =M) Sum+ = (l1) *p + (r+1); Elsesum+ = l*Q; } P*=Ten; A/=Ten; } returnsum;}intMain () {intN, ans; inti; scanf ("%d",&N); for(i=0; i<=9; i++) {ans=Slove (n,i); printf ("%d\n", ans); } return 0;}
Digital DP;
This is more general, is the template version, the processing of the data a little attention, and then to determine the state well.
#include <cstdio>#include<cstring>#include<iostream>#include<algorithm>#include<cmath>using namespaceStd;typedefLong LongLL; LL dp[ -][ -][ One][2];//dp[number of digits [number of occurrences] [number] [ how much] [first is 0]intbit[ -]; LL DFS (intPosintCouintNumintFlagBOOLIS0) { if(pos = =-1 ) { returncou; } if(!flag && DP[POS][COU][NUM][IS0]! =-1) returnDP[POS][COU][NUM][IS0]; LL ans=0; intEnd = Flag? Bit[pos]:9; for(intI=0; i<=end; i++) { if(i = = num &&!) (IS0 && i = =0)) Ans+ = DFS (pos-1, cou+1, NUM, flag && i = = end, Is0 && i = =0 ); Elseans+ = DFS (pos-1, cou, NUM, flag && i = = end, Is0 && i = =0 ); } if(!flag) DP[POS][COU][NUM][IS0]=ans; returnans;}voidSolve (LL N) {intLen =0, I, M =N; LL ans; while(n) {Bit[len+ +] = n%Ten; N/=Ten; } for(i=0; i<=9; i++) {ans= DFS (len-1,0I1,1); printf ("%lld\n", ans); }//printf ("\n\n\n");}intMain () {LL A; Memset (DP,-1,sizeof(DP)); while(SCANF ("%lld", &a)! =EOF) {Solve (a); }//printf ("\n\n\n"); return 0;}
Zznu 1255 Numeric statistics (digital DP, mathematical method)