【按位DP】hdu 3555

來源:互聯網
上載者:User

dp[][0]表示不包含49並且以非4結尾的個數

dp[][1]表示不包含49並且以4結尾的個數

dp[][2]表示包含49的個數

所以轉移方程是:

dp[k][0]=dp[k-1][0]*9+dp[k-1][1]*8;
dp[k][1]=dp[k-1][0]+dp[k-1][1];

dp[k][2]=dp[k-1][1]+dp[k-1][2]*10;

實現:從最高位開始,固定每一位,枚舉數量。注意處理如49,549.。。之類的特殊數,要n+1

#include <list>#include <map>#include <set>#include <queue>#include <string>#include <deque>#include <stack>#include <algorithm>#include <iostream>#include <iomanip>#include <cstdio>#include <cmath>#include <cstdlib>#include <limits.h>#include <time.h>#include <string.h>using namespace std;#define LL long long#define PI acos(-1.0)#define MAX INT_MAX#define MIN INT_MIN#define eps 1e-10#define FRE freopen("a.txt","r",stdin)#define N 25LL dp[21][3];int num[21];int state(int now,int n){    if(now==0){        if(n==4)return 1;        return 0;    }    if(now==1){        if(n==9)return 2;        if(n==4)return 1;        return 0;    }    return 2;}int main(){    int t;    scanf("%d",&t);    while(t--){        LL n;        scanf("%I64d",&n);        n++;        int i,j,k;        int cnt=1;        while(n){            num[cnt++]=n%10;            n/=10;        }        int len=cnt-1;        int b=0;        int now=0;        LL ans=0;        for(i=len;i>=1;i--){            b++;            for(j=0;j<num[i];j++){                memset(dp,0,sizeof(dp));                dp[b][state(now,j)]=1;                for(k=b+1;k<=len;k++){                    dp[k][0]=dp[k-1][0]*9+dp[k-1][1]*8;                    dp[k][1]=dp[k-1][0]+dp[k-1][1];                    dp[k][2]=dp[k-1][1]+dp[k-1][2]*10;                }                ans+=dp[len][2];            }            now=state(now,num[i]);        }        printf("%I64d\n",ans);    }    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.