HDU 6148 (Digital DP)

### HDU 6148 topic Link ###

Subject to the effect:

As we all know, the degree of the Bears are very fond of numbers.

It has recently invented a new digital: Valley Number, the same number as the valley.




When a number, from left to right to see the last figure does not appear to increment and then decrement the "peak" phenomenon, it is known as Valley Number. It can be incremented, decremented can also be incremented before decreasing again. Equal situation may arise in the process of increasing or decreasing.

For example, 1,10,12,212,32122 are Valley Number.

121,12331,21212 not.

Want to know the degree of Bear Valley Number is not greater than the number N of the number.

Note that the leading zero is not legitimate.

 

analysis:

1, the number of bits DP, it is easy to know that the first dimension represents the number of bits, a second dimension represents the previous number, and due to the recording slope (i.e., gradual increase and decrease), so to add a third dimension represents the current digit before, is increasing or decreasing or not (if and only if there are two different numbers, will have incremented or decremented)

2, the leading zero flag necessary to add lead, and according to the sample, this problem is also asserted if the numbers 0 to leading zero, it is determined that it is unnecessary to be zero.

3, then the meaning of the questions, if they meet the current number of increments before and strictly less than the number of bits on a number of hours before continue.

4, note lead when == true, is no gradual increase and decrease, because until the first non-zero number was counted. That is, when the case if the lead == true when the number of bits is not 0, then a digital bit enumerated from the beginning, it may be pre == - 1 when combined together (i.e., not a start number, at this time pre == - 1, and also lead to true)

 

code show as below:

 

#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
typedef long long ll;
const ll mod = (ll) 1000000007;
char s[108];
int t,n;
int a[108];
ll dp[108][12][3];
ll dfs(int pos,int pre,int k,bool lead,bool limit){ //Pos of bits, the first number is a pre, before the slope of the number k (k == 0 is unknown, i.e. only a few; k == 1, pre incremented and the previous number; k == 2, pre previous decrement), lead leading zero mark, limit the number of bits limit flag 
    IF (POS == 0 ) return Lead? 0 : . 1 ;
     IF !! (limit && DP [POS] [pre] [K] = - . 1 ) return DP [POS ] [pre] [K]; // memory search 
    int up limit = A [POS]:? . 9 ; 
    LL RES = 0 ;
     for ( int I = 0 ; I <= up; I ++ ) {
         IF (K == . 1 && I <pre) Continue ;
         IF (Lead ==true&&i==0) res=(res+dfs(pos-1,i,k,true,limit&&i==a[pos])%mod+mod)%mod;
        else if(lead) res=(res+dfs(pos-1,i,k,false,limit&&i==a[pos])%mod+mod)%mod;
        else if(i==pre) res=(res+dfs(pos-1,i,k,false,limit&&i==a[pos])%mod+mod)%mod;
        else res=(res+dfs(pos-1,i,(i<pre?2:1),false,limit&&i==a[pos])%mod+mod)%mod;
    }
    if(!limit) dp[pos][pre][k]=res;//记忆化
    return res;
}
int main()
{
    //freopen("test.in","r",stdin);
    //freopen("test.out","w",stdout);
    memset(dp,-1,sizeof(dp));
    scanf("%d",&t);
    while(t--)
    {
        scanf("%s",s);
        int len=strlen(s);
        for(int i=0;i<len;++ i) a [len-i] = s [i] - '0';
        printf("%lld\n",dfs(len,-1,0,true,true) );
    }
}

 

Guess you like

Origin www.cnblogs.com/Absofuckinglutely/p/11414030.html