HDU3555 Bomb (digital DP) __ algorithm

Source: Internet
Author: User

The Digital DP Entry topic (I'll just do the entry-level topic 0 0) and find out how many numbers within n don't contain a contiguous 49 number. Similar to another question http://acm.hdu.edu.cn/showproblem.php?pid=2089, copy the code. Another problem, n long ago: transmission door.

Pre-preprocessing, set DP[I][J] represents the number of schemes (J can be 0) for the number of J, and it is not difficult to get dp[i][j] = Sigma (dp[i-1][k)) (0<=k<=9,k≠4 and J≠9).

Simulate the summation of the principle, assuming n = 23495, then len = 5; The first round is to find out the total scheme number of 1xxxx and 0xxxx, that is, the total number of schemes less than 20000; the second cycle is to find the total number of 20xxx and 21xxx and 22xxx; The third time to find out 230xx,231xx,232xx,233xx; 2340x,2341x,2342x,2343x,2344x,2345x,2346x,2347x,2348x, the next is 2349x, This time there are 49 and then there is no meaning, so the cycle of termination.

#include <iostream> #include <cstring> using namespace std;
typedef long Long LL;
LL DP[25][10];
int digit[20];
    void Init () {dp[0][0] = 1;
            for (int i = 1; I <= i++) {for (int j = 0; J <= 9; j +) for (int k = 0; k <= 9; k++)
                {if (k = = 9&&j = 4) continue;
            DP[I][J] + = dp[i-1][k];
    }} void Dig (LL x,int &len) {memset (digit,0,sizeof digit);
        while (x) {Digit[++len] = x%10;
    x/= 10;
    } ll Cal (ll N) {ll ans = 0;
    int len = 0;
    Dig (N,len); for (int i = len; I >= 1; i--) {for (int j = 0; J < Digit[i]; J +) {ans = d
        P[I][J];
    } if (digit[i] = = 9&&digit[i+1] = = 4) break;
return ans;
    int main () {init ();
    int T;
    LL N;
    cin>>t;
        while (t--) {cin>>n;
    Cout<<n+1-cal (n+1) <<endl; }
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.