POJ 3208 Apocalypse Someday(數位dp),pojapocalypse

來源:互聯網
上載者:User

POJ 3208 Apocalypse Someday(數位dp),pojapocalypse

題意:輸出第n個包含連續三個6的數

思路:

dp[i][0]表示i位元中首位不為6且不含666的數的數量
dp[i][1]表示i位元中首位連續1個6並且不含666的數的數量
dp[i][2]表示i位元中首位連續2個6並且不含666的數的數量
dp[i][3]表示i位元中含有666的數的數量

寫出遞推關係即可,再確定出帶求的數有多少位,再從高位到低位逐次確定

//132K16MS#include<cstdio>#include<cstring>#include<algorithm>#include<iostream>using namespace std;typedef long long ll;ll dp[15][4];const int trans[4][10]={    {0,0,0,0,0,0,0,0,0,0},    {3,3,3,3,3,3,0,3,3,3},    {3,3,3,3,3,3,1,3,3,3},    {3,3,3,3,3,3,2,3,3,3}};int main(){    dp[0][0]=1;    for(int i=1;i<=15;i++){        dp[i][0]=(dp[i-1][0]+dp[i-1][1]+dp[i-1][2])*9;        dp[i][1]=dp[i-1][0];        dp[i][2]=dp[i-1][1];        dp[i][3]=dp[i-1][3]*10+dp[i-1][2];    }    int T;    scanf("%d",&T);    while(T--){        ll n;        scanf("%I64d",&n);        int dig=0;        int cur=3;        while(dp[dig][3]<n) dig++;        while(dig){            ll s=0;            for(int i=0;i<10;i++){                ll tmps=0;                for(int j=3;j>=trans[cur][i];j--){                    tmps+=dp[dig-1][j];                }                if(s+tmps>=n){                    printf("%d",i);                    cur=trans[cur][i];                    break;                }                s+=tmps;            }            n-=s;            dig--;        }        putchar('\n');    }    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.