zoukankan      html  css  js  c++  java
  • HDU3555 Bomb[数位DP]

    Bomb

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)
    Total Submission(s): 16362    Accepted Submission(s): 5979

    Problem Description
    The counter-terrorists found a time bomb in the dust. But this time the terrorists improve on the time bomb. The number sequence of the time bomb counts from 1 to N. If the current number sequence includes the sub-sequence "49", the power of the blast would add one point.
    Now the counter-terrorist knows the number N. They want to know the final points of the power. Can you help them?
     
    Input
    The first line of input consists of an integer T (1 <= T <= 10000), indicating the number of test cases. For each test case, there will be an integer N (1 <= N <= 2^63-1) as the description.

    The input terminates by end of file marker.
     
    Output
    For each test case, output an integer indicating the final points of the power.
     
    Sample Input
    3 1 50 500
     
    Sample Output
    0 1 15
    Hint
    From 1 to 500, the numbers that include the sub-sequence "49" are "49","149","249","349","449","490","491","492","493","494","495","496","497","498","499", so the answer is 15.

    和 不要62 类似
    注意:
    1. long long
    2.if(a[i+1]==4&&a[i]>=9) flag=1;
    不能+d[i-1][0],因为9的特殊性不可能有比9大的个位数使后面可能出现49,天际线就是49了
    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    #include <cstring>
    #include <map>
    using namespace std;
    const int N=25,INF=1e9+5;
    typedef long long ll;
    inline ll read(){
        char c=getchar();ll x=0,f=1;
        while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
        while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
        return x*f;
    }
    ll n;
    ll d[N][4];
    void dp(){
        d[0][0]=1;
        for(int i=1;i<=20;i++){
            d[i][0]=10*d[i-1][0]-d[i-1][1];//!
            d[i][1]=d[i-1][0];
            d[i][2]=10*d[i-1][2]+d[i-1][1];
            //printf("d %d %d
    ",d[i][0],d[i][2]);
        }
    }
    ll sol(ll n){
        int a[N],len=0,flag=0;
        ll ans=0;
        while(n) a[++len]=n%10,n/=10;
        a[len+1]=0;
        for(int i=len;i>=1;i--){
            ans+=d[i-1][2]*a[i];
            
            if(flag) ans+=a[i]*d[i-1][0];
            else if(a[i]>4) ans+=d[i-1][1];//maybe 49
            if(a[i+1]==4&&a[i]==9) flag=1;//cannot +d[i-1][0],cause skyline and flag=1
            //printf("%d %d %d
    ",i,ans,flag);
        }
        if(flag) ans++;
        return ans;
    }
    int main(){
        dp();
        int T=read();
        while(T--){
            n=read();
            printf("%lld
    ",sol(n));
        }
    }
  • 相关阅读:
    迭代器和生成器
    案例:复制大文件
    案例:使用seek倒查获取日志文件的最后一行
    Leetcode165. Compare Version Numbers比较版本号
    Leetcode137. Single Number II只出现一次的数字2
    Leetcode129. Sum Root to Leaf Numbers求根到叶子节点数字之和
    Leetcode116. Populating Next Right Pointers in Each Node填充同一层的兄弟节点
    Leetcode114. Flatten Binary Tree to Linked List二叉树展开为链表
    Leetcode113. Path Sum II路径总和2
    C++stl中vector的几种常用构造方法
  • 原文地址:https://www.cnblogs.com/candy99/p/6060774.html
Copyright © 2011-2022 走看看