fzu 2109 Mountain Number 數位DP 記憶化搜尋

來源:互聯網
上載者:User

做法:按位暴力DFS吧...

這裡很明顯沒有考慮當前數字是否是首位,其實不需要啊,因為這種情況發生的時候肯定是那種比邊界數少一位的數字。也就是說如果求1234 那麼這裡要考慮的是 0 ---999的

數字中的mountain, 沒有考慮首位的後果是moutain的形態不同,可是很容易證明,規定位元的數字只有兩種moutain,而這種兩種moutain在 0---999中的個數相同。

#include <iostream>#include <cstdio>#include <cstring>#define LL long longconst int LMT=11;using namespace std;LL dp[LMT][LMT][LMT];int sec[LMT];LL dfs(int pre,int pos,int len,bool tag){    if(dp[pre][pos][len]!=-1&&(!tag||pos==len))return dp[pre][pos][len];    LL res=0;    int end=tag?sec[pos]:9;    if(pos&1)    {        for(int j=end;j>=pre;j--)        res+=dfs(j,pos+1,len,tag&&j==end);    }    else    {        for(int j=min(end,pre);j>=0;j--)        res+=dfs(j,pos+1,len,tag&&j==end);    }    if(!tag)dp[pre][pos][len]=res;    return res;}int work(LL x){    int num[LMT],res=0;    do    {        num[res]=x%10;        res++;    }    while(x/=10);    for(int i=0,j=res-1;j>=0;j--,i++)    sec[i]=num[j];    return res;}int main(void){    int T;    scanf("%d",&T);    memset(dp,-1,sizeof(dp));    for(int i=0;i<LMT;i++)     for(int j=0;j<LMT;j++)     dp[i][j][j]=1;    while(T--)    {        LL a,b,x,y;        scanf("%I64d%I64d",&a,&b);        x=dfs(9,0,work(a-1),1);        y=dfs(9,0,work(b),1);        printf("%I64d\n",y-x);    }    return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.