Zznu 1255 Numeric statistics (digital DP, mathematical method)

Source: Internet
Author: User

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.


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,


Give an integer n (1≤n≤2^31-1) that represents the total page number of the book


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
Sample output

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.