HDU 4734-digit DP

Source: Internet
Author: User

Http://www.cnblogs.com/Griselda/archive/2013/11/20/3433295.html

The above link is very clear and simple to post:

Digital DP, which is used to learn the digital DP.

< digit dp>

The so-called digital DP is a DP that is shifted based on each bit of the number being considered.

For example, a number smaller than 456 can be considered,

4 5 6

4 5 (0~6)

4 (0~4) (0~9)

(0~3)         (0~9) (0~9)

Then we can consider using dp[len][pre] to indicate the number of qualifying numbers that are Len, beginning with the pre.

So we can get the transfer equation.

For this problem, we can use Dp[len][pre] to denote the length Len and the number of weights less than the pre.

This question uses the memory search, except the boundary condition record Dp[len][pre] The value, the next discovery has already calculated before can return directly;

Initial value: Dp[len][pre] = 0;

DFS (LEN, pre, flag) indicates that the length is Len and does not exceed all qualifying values of the pre. Where flag is used to control the boundary.

When a deep search boundary is found in the DFS process, the Len < 0,pre >=0 is returned 1.

Here's the main idea of why you might want to use a digital DP, or a digital DP, to solve something.

The topic is to give a limit value, to find out the number of conditions under this limit. The process of this value is calculated by each bit, so it is natural that we can calculate each bit separately, and always count to the last bit to produce the result.

Dp[len][pre] Indicates the length Len and the number of weights less than the pre

#include <iostream>#include<string>#include<cstring>#include<cstdlib>#include<cstdio>#include<cmath>#include<algorithm>#include<stack>#include<queue>#include<cctype>#include<vector>#include<iterator>#include<Set>#include<map>#include<sstream>using namespacestd;#defineMem (A, B) memset (A,b,sizeof (a))#definePF printf#defineSF scanf#defineSPF sprintf#definePB Push_back#defineDebug printf ("!\n")#defineMAXN 1000000000 +5#defineMAX (A, b) a>b?a:b#defineBlank pf ("\ n")#defineLL Long Long#defineAll (x) X.begin (), X.end ()#defineINS (x) Inserter (X,x.begin ())#definePqueue priority_queue#defineINF 0x3f3f3f3f#defineLS (rt<<1)#defineRS (rt<<1|1)intn,m;inta[ -];intdp[ -][200000];intDfsintPosintPreBOOLflag) {    if(pre<0)return 0; if(pos<0)returnpre>=0; if(!flag && dp[pos][pre]! =-1)returnDp[pos][pre]; intE = Flag?a[pos]:9; intAns =0;  for(inti =0; i<=e;i++) {ans+ = DFS (pos-1, pre-i* (1<<pos), Flag && i==e); }    if(!flag) Dp[pos][pre] =ans; returnans;}intMain () {intT,kase=1, I,j,k; SF ("%d",&t); MEM (DP,-1);  while(t--) {SF ("%d%d",&n,&m); intLen =0, pre =0, tmp=1;  while(m) {A[len+ +] = m%Ten; M/=Ten; }         while(n) {pre+ = tmp * (n%Ten); TMP*=2; N/=Ten; }        /*for (i=0;i<len;i++) pf ("%d\n", A[i]);        PF ("%d%d\n", len,pre); */PF ("Case #%d:%d\n", Kase++,dfs (len-1, Pre,true)); }    return 0;}

HDU 4734-digit 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.