UVA-11038 how many O ' s? (Digital DP)

Source: Internet
Author: User

How many O ' s?

Test instructions is the number of the number within the interval of 0, such as 100 has two 0.

Digital DP Bar, Dp[i][j][k], I clearly indicates the current position, J indicates the number of 0 found, and K indicates the number of 0 to find. Because the number of 0 in the number of up to 32, so you can enumerate 32 K cases, with the digital DP to find.

#include <cstdio>#include<cstring>#include<algorithm>#include<iostream>using namespacestd;#definell Long Longintdig[ -];ll dp[ +][ +][ +];intBigcnt;ll DFS (intPosintCntintFlagintLim) {if(pos = =-1)returnCNT = =0; if(CNT <0)return 0; if(!flag &&!lim && dp[pos][cnt][bigcnt]! =-1)returndp[pos][cnt][bigcnt]; intEnd = Lim? Dig[pos]:9; LL ret=0;  for(inti =0; I <= End; i++) {        if(Flag &&!i) ret + = DFS (pos-1, bigcnt,1, Lim && (i = =End)); Else if(Flag && i) ret + = DFS (pos-1, bigcnt,0, Lim && (i = =End)); Else if(!flag && i) ret + = DFS (pos-1Cnt0, Lim && (i = =End)); Else if(!flag &&!i) ret + = DFS (pos-1, CNT-1,0, Lim && (i = =End)); }    if(!lim &&!flag) dp[pos][cnt][bigcnt] =ret; returnret;} ll func (ll num) {LL ret=0; if(num = =-1)return-1; intn =0;  while(num) {dig[n+ +] = num%Ten; Num/=Ten; }     for(inti =1; I <= +; i++) {bigcnt=i; RET+ = i * DFS (n-1I1,1); }    returnret;}intMain () {ll A, B; Memset (DP,-1,sizeof(DP));  while(~SCANF ("%lld%lld", &a, &b)) {if(A <0) Break; printf ("%lld\n", func (b)-Func (A-1)); }}

UVA-11038 how many O ' s? (Digital DP)

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.