百度之星複賽--Valley Numer----數位dp

Valley Numer

Accepts: 548
Submissions: 1125
Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 32768/32768 K (Java/Others)
Problem Description

衆所周知,度度熊非常喜歡數字。

它最近發明了一種新的數字:Valley Number,像山谷一樣的數字。

當一個數字,從左到右依次看過去數字沒有出現先遞增接着遞減的“山峯”現象,就被稱作 Valley Number。它可以遞增,也可以遞減,還可以先遞減再遞增。在遞增或遞減的過程中可以出現相等的情況。

比如,1,10,12,212,32122都是 Valley Number。

121,12331,21212則不是。

度度熊想知道不大於N的Valley Number數有多少。

注意,前導0是不合法的。

Input

第一行爲T,表示輸入數據組數。

每組數據包含一個數N。

● 1≤T≤200

● 1≤length(N)≤100

Output

對每組數據輸出不大於N的Valley Number個數,結果對 1 000 000 007 取模。

Sample Input
3
3
14
120
Sample Output
3
14
119

題目鏈接:http://bestcoder.hdu.edu.cn/contests/contest_showproblem.php?cid=777&pid=1005

題目很容易就能看出來這個一個數位dp,比較麻煩,但是還好,難度一般。

經典的數位DP,可以將狀態設計成四維,這裏我用的三維。

當前數字長度len ,最後一位數字digit,是否已經在遞增序列裏increased 是否和當前前綴相同same_prefix

轉移時處理好這些狀態就好了。

代碼:

#include <cstdio>
#include <cstring>
#include <iostream>
#define LL long long
using namespace std;
const LL mod=1e9+7;
char s[200];
int d[200],len;
LL dp[200][20][20];
LL dfs(int pos,bool flag,bool first,int now,int pre){
    if(pos==-1)
        return 1;
    if(!flag&&!first&&~dp[pos][now][pre]){
        return dp[pos][now][pre];
    }
    LL ans=0;
    int limit=flag?d[pos]:9;
    for(int i=0;i<=limit;i++){
        if(i==0&&first){
            ans+=dfs(pos-1,flag&&i==limit,first,now,pre);
            ans%=mod;
        }
        else{
            if(now>pre){
                if(i<now)
                    continue;
            }
            if(now==i)
                ans+=dfs(pos-1,flag&&i==limit,first&&i==0,i,pre);
            else
                ans+=dfs(pos-1,flag&&i==limit,first&&i==0,i,now);
            ans%=mod;
        }
    }
    if(!flag&&!first)
        dp[pos][now][pre]=ans;
    return ans;
}
int main(){
    int t;
    scanf("%d",&t);
    memset(dp,-1,sizeof(dp));
    while(t--){
        scanf("%s",s);
        len=strlen(s);
        for(int i=0;i<len;i++){
            d[i]=s[i]-'0';
        }
        for(int i=0;i<len/2;i++){
            swap(d[i],d[len-1-i]);
        }
        LL ans=dfs(len-1,1,1,10,10)-1;
        ans=(ans%mod+mod)%mod;
        cout<<ans<<endl;
    }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章