(Digital DP 1.2) HDU 3555 Bomb (statistics 1~n, containing 49 of the number of numbers)

Source: Internet
Author: User

Topic:

BombTime limit:2000/1000 MS (java/others) Memory limit:131072/65536 K (java/others)
Total submission (s): 9273 Accepted Submission (s): 3275


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)
Recommendzhouzeyong | We have carefully selected several similar problems for you:3554 3556 3557 3558 3559


Topic Analysis:

Digital DP.


These are state transition equations.

DP[I][0]=DP[I-1][0]*10-DP[I-1][1]; The representation does not contain 49 (when the number of digits increases by one, excluding 49 increases by 10 times times, but minus one starting with 9)
DP[I][1]=DP[I-1][0]; The representation does not contain 49, but begins with 9 (when the number of digits increases by one, it grows in those that do not begin with 9)
DP[I][2]=DP[I-1][2]*10+DP[I-1][1]; The representative contains 49 (when the number of digits is increased by one, it has already included a 49 increase of 10 times times, plus the number that now begins with 4 and preceded by 9 groups of Composition 49)



Dthe state of P is 2-dimensional dp[len][3]
Dp[len][0] Represents the number of scenarios with a length of Len without 49
Dp[len][1] Represents the number of scenarios with a length of Len without 49 but with a number beginning with 9
dp[len][2] For a length of Len containing 49 of the scheme number

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:

/* * hdu355_1.cpp * * Created on:2015 April 13 * author:administrator * * #include <iostream> #include <cstdio& gt; #include <cstring>using namespace std;const int maxn = 25;typedef Long Long ll;ll dp[maxn][3];//This problem has a larger number of programs, so Use long long/** * initialization to calculate the relationship between them */void init () {memset (dp,0,sizeof (DP));DP [0][0] = 1;int i;for (i = 1; i < MAXN; ++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][2]*10 + dp[i-1][1];}} ll solve (ll N) {ll ans = 0;int digit[maxn];int len = 0;while (n > 0) {Digit[++len] = N%10;n/= 10;} DIGIT[LEN+1] = 0;int I;bool flag = False;int last = 0;for (i = len; i > 0; i) {//traverse each digit ans + = dp[i-1][2]*digit[i];if (flag = = True) {ans + = dp[i-1][0]*digit[i];//if its high position exists at 49, then the low no matter what number there is}if (flag = = False && Digit[i] > 4) {//location, and the first is greater than or equal to 5, The presence of at least one of the lower 9 of the number of cases ans + = dp[i-1][1];} if (last = = 4 && digit[i] = = 9) {flag = true;} last = Digit[i];} return ans;} int main () {int t;scanf ("%d", &t), Init (), while (t--) {ll n;scanf ("%i64d", &n); n++;p rintf ("%i64d\n", Solve (n));} return 0;}






(Digital DP 1.2) HDU 3555 Bomb (statistics 1~n, containing 49 of the number of numbers)

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.