hdu 3555 Bomb

来源:互联网 发布:知乎 网页版 编辑:程序博客网 时间:2024/05/01 20:58
</pre><pre name="code" class="cpp"><pre name="code" class="cpp">数位DP基础题
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;typedef __int64 ll;int t;ll n;ll dp[20][3];void init(){    dp[0][0]=1;    for(int i=1;i<=18;i++){        dp[i][0]=dp[i-1][0]*10-dp[i-1][1];        dp[i][1]=dp[i-1][0];        dp[i][2]=dp[i-1][1]+dp[i-1][2]*10;    }}ll f(ll x){    int tmp[21];    ll ans=0;    for(int i=1;i<=19;i++){        tmp[i]=x%10;        x/=10;    }    bool flag=0;    for(int i=19;i>=1;i--){        ans+=tmp[i]*dp[i-1][2];        if(flag)ans+=tmp[i]*dp[i-1][0];        else{            if(tmp[i]>=5)ans+=dp[i-1][1];        }        if(tmp[i+1]==4&&tmp[i]==9)flag=1;    }    return ans;}int main(){        init();        cin>>t;        while(t--){        scanf("%I64d",&n);        printf("%I64d\n",f(n+1));        }    return 0;}


                                             
0 0