HDU 3555 Bomb (digital DP AH)

Source: Internet
Author: User

Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=3555


Problem DescriptionThe 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 "a", 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?

Inputthe first line of input consists of an integer t (1 <= T <= 10000), indicating the number of test cases. For each test case, there'll be is an integer N (1 <= n <= 2^63-1) as the description.

The input terminates by end of file marker.

Outputfor each test case, output an integer indicating the final points of the power.
Sample Input
3150500

Sample Output
0115Hintfrom 1 to $, the numbers that include the Sub-sequence "49", "149", "249", "349", "449", "490", "491 "," 492 "," 493 "," 494 "," 495 "," 496 "," 497 "," 498 "," 499 ", so the answer is 15.

Author[email protected]
Source2010 acm-icpc multi-university Training Contest (--host by WHU)


Test instructions

Find out how many numbers in the number 0 to n contain ' 49 '!

Ps:

Digital DP

DP[I][J]: J State of the number of length I
Dp[i][0]: Number of scenarios with length I but not 49
DP[I][1]: Number of schemes with a length of I and no 49 but starting with 9
DP[I][2]: Number of scenarios with a length of I and 49

(GO) state transitions are as follows
dp[i][0] = dp[i-1][0] * 10-dp[i-1][1]; Not include 49 if it does not contain 49 and can be filled in 0-9 but minus dp[i-1][1] because 4 will and 9 constitute the
dp[i][1] = dp[i-1][0]; Not including starts with 9 This is just a 9 on the number that doesn't include 49 .
dp[i][2] = dp[i-1][2] * + dp[i-1][1];//include 49 already contains 49 of the number can be filled 0-9, or 9 the beginning of the fill 4

And then you start counting from high .

when counting to a bit, add dp[i-1][2] * Digit[i] is obviously right, because this one can fill 0-(digit[i]-1)
If this person is next to 49, then add dp[i-1][0] * Digit[i] is also clearly right.
If this one is not next to 49, but Digit[i] is bigger than 4, then when this one fills 4, you have to add dp[i-1][1]

The code is as follows:

#include <cstdio> #include <cstring> #include <iostream> #include <algorithm>using namespace Std;typedef __int64 LL; LL Dp[27][3];int C[27];//dp[i][j]: J State of the number of lengths of I//dp[i][0]: the length is I but does not contain 49 of the number of scenarios//dp[i][1]: The length is I and does not contain 49 but the number of numbers starting with 9//dp[i]    [2]: The length is I and contains 49 of the scheme number void init () {memset (dp,0,sizeof (DP));    Dp[0][0] = 1;        for (int i = 1; i <=; i++) {dp[i][0] = dp[i-1][0]*10-dp[i-1][1];        DP[I][1] = dp[i-1][0]*1;    DP[I][2] = dp[i-1][2]*10+dp[i-1][1];    }}int cal (LL n) {int k = 0;    Memset (C,0,sizeof (c));        while (n) {c[++k] = n%10;    n/=10;    } c[k+1] = 0; return k;}    void solve (int len, LL n) {int flag = 0;//Mark whether the presence of a-ll ans = 0;        for (int i = len; I >= 1; i--) {ans+=c[i]*dp[i-1][2];        if (flag) {ans+=c[i]*dp[i-1][0]; } else if (C[i] > 4) {//This bit is not preceded by 49, but C[i] is larger than 4, then add dp[i-1][1 when this bit is filled 4] ans+=dp[i-1]        [1]; } if (c[i+1]==4 &&        c[i]==9) {flag = 1; }} printf ("%i64d\n", ans);}    int main () {int t;    LL N;    Init ();    scanf ("%d", &t);        while (t--) {scanf ("%i64d", &n);        int len = cal (n+1);    Solve (len, n); } return 0;}



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

HDU 3555 Bomb (digital DP AH)

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.