"Dream Counting, 2006 dec-Number of Dreams" digital DP

Source: Internet
Author: User

Test instructions: Given two numbers, the number of times in the Q interval [a, b] 0~9 appears respectively. A,b<=10^18

The most bare digital DP bar: At first, there was no memory of the tle. Tat

We can find out the interval [0,b], minus the interval [0,a].

With DFS implementation, record flag (whether the filled bit is coincident with the boundary), zero (currently in prefix 0)

1#include <cstdio>2#include <cstdlib>3#include <cstring>4#include <iostream>5#include <ctime>6#include <queue>7 using namespacestd;8 9typedefLong LongLL;Ten Const intn= -; One intc[n][n][2][2],d[n],sum[n]; A LL Bit[n],ret[n]; -  -LL g (intNowintXintFlagintzero) the { -     if(c[now][x][flag][zero]!=-1)returnC[now][x][flag][zero]; -     if(x==0)return 0; -LL ans=0; +     if(flag) -     { +          for(intI=0; i<d[x];i++)  A         { atAns+=g (now,x-1,0, Zero & (i==0)); -             if(I==now &&!) (i==0&& zero)) ans+=bit[x-1]; -         } -Ans+=g (now,x-1,1, Zero && (d[x]==0)); -         if(Now==d[x] &&! (now==0&& zero)) ans+=ret[x-1];  -     } in     Else -     { to          for(intI=0; i<=9; i++)  +         { -Ans+=g (now,x-1,0, Zero & (i==0));  the             if(I==now &&!) (i==0&& zero)) ans+=bit[x-1]; *         } $     }Panax Notoginseng     //printf ("Now%d x%d flag%d Zero%d =%d\n", Now,x,flag,zero,ans); -c[now][x][flag][zero]=ans; the     returnans; + } A  the voidSolve (LL x,ll tmp) + { -memset (D,0,sizeof(d)); $memset (c,-1,sizeof(c)); $     intL=0; LL y=x; -      while(y) -     { theD[++l]= (int) (y%Ten); -Y/=Ten;Wuyi     } the      for(intI=0; i<= -; i++) ret[i]=x%bit[i]+1; -      for(intI=0; i<=9; i++) sum[i]+=g (i, -,1,1)*tmp; Wu } -  About intMain () $ { -     //freopen ("a.in", "R", stdin); -Freopen ("dream.in","R", stdin); -Freopen ("Dream.out","W", stdout); ALL A, B;intx; +scanf"%lld%lld",&a,&B); thememset (SUM,0,sizeof(sum)); -bit[0]=1; $      for(intI=1; i<= -; i++) bit[i]=bit[i-1]*Ten; theSolve (A-1,-1); theSolve (B,1); the      for(intI=0; i<=9; i++) printf ("%d", Sum[i]); the     return 0; -}

"Dream Counting, 2006 dec-Number of Dreams" 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.