[ACM] hdu 3555 Bomb (Digital DP, count the total number of "49" contained in 1-N)

Source: Internet
Author: User

[ACM] hdu 3555 Bomb (Digital DP, count the total number of "49" contained in 1-N)

BombTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others) Total Submission (s): 7187 Accepted Submission (s): 2512

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 between des the sub-sequence "49", the power of the blast wocould 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 will be 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 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. 

Authorfatboy_cw @ WHU
ACM-ICPC Multi-University Training Contest (12) -- Host by WHU
Recommendzhouzeyong | We have carefully selected several similar problems for you: 3554 3556 3557 3558 3559


Solution:

How many numbers contain "49" from integer 1 to N? For example, N = 500, then "49", "149", "249", "349", "449", "490", "491", "492 ", "493", "494", "495", "496", "497", "498", "499 ",
So the answer is 15.

In addition to solving the problem of digital DP, there was no idea of digital DP at the beginning .. Then I went to the Internet to find a problem-solving report. I read many articles and did not know what I was talking about .....

Later I found a good solution report with Google: http://www.cnblogs.com/liuxueyang/archive/2013/04/14/3020032.html

Let's talk about the overall idea:

Assume that N = 591 is counted in the following order:

1 ~ 499 determine the digit 5, and the number of statistics is smaller than it

500 ~ 589 determine 9, and the number of statistics is smaller than it

590 determine the value of 1. The number of statistics is smaller than that of 1.

Finally, determine if it meets the requirement of 591.

After three cycles, we can find all the numbers that match the meaning of the question. This is the secret of the digital DP in this question.

Another example is 1249.

1 ~ 999

1000 ~ 1199

1200 ~ 1239

1240 ~ 1248

1249


Dp [I] [j] indicates the number of I (that is, the number of I-bits) IN THE STATUS j.

There are three statuses in this question:

① Dp [I] [0] indicates the number of numbers with an I length and No 49 contained

② Dp [I] [1] indicates the number of numbers with the length of I and 49 not included and the first (highest) on the left being 9.

③ Dp [I] [2] indicates the number of I-contained numbers.

Table preprocessing, 0 <= I <= 21 (21 bits are enough), mainly processing status transfer

Dp [I] [0] = dp [I-1] [0] * 10-dp [I-1] [1]; // dp [I] [0] can be added with a number at a high position, but 49XXX may occur, minus
Dp [I] [1] = dp [I-1] [0]; // Add 9 in the upper position without 49
Dp [I] [2] = dp [I-1] [2] * 10 + dp [I-1] [1]; // In the case of 49, add one or more digits in the upper position, but not 49 in the upper position. If the upper position is 9, add 4 in the upper position.

Paste the Statistical Code:

For (int I = len; I> = 1; I --) // each time you determine a bit {ans + = dp [I-1] [2] * bit [I]; // The Low Position contains 49. The high position can be 1, 2, 3 .... bit [I] can all be used. bit [I] represents several numbers, such as bit [I] = 5, which means there are five numbers, 0, 1, 2, 3, 4, smaller than 5. If (! Has) {if (bit [I]> 4) ans + = dp [I-1] [1]; // low high bit is 9, the above 4 can be} else ans + = (dp [I-1] [0]) * bit [I]; // if there are 49, just choose, for example, in 495, 490, 491, 492, 493, 494, and, the above sentence plagued me for more than a day. Why not write (d [I-1] [0] + dp [I-1] [2]) * bit [I] it, the previous has appeared 49 // so low any choice can, dp [I-1] [0] is those low don't appear 49, dp [I-1] [2] is those low appear 49, it is reasonable to say should // Add ah, BUT !!! Pay attention to the first sentence of the loop ans + = dp [I-1] [2] * bit [I]; the front has added the dp [I-1] [2] low position of the // 49, ah, want to cry ........ if (bit [I + 1] = 4 & bit [I] = 9) has = true;} if (has) ans ++;
The length of the number of len. bit [I] indicates the number (starting from 1) from the right to the left.

It takes too much time to figure out the statistical code.

For the I bit, it is better to understand the length of I, first add the length of all the numbers of I-1 contains 49, that is, ans + = dp [I-1] [2] * bit [I]; Why multiply by bit [I? For example, if bit [I] = 5, there are five options for the number smaller than 5, namely, 0, 1, 2, 3, 4, there are dp [I-1] [2] numbers that match the meaning of each selected length for the I-1, so there is dp [I-1] [2] * bit [I], so we can find all the numbers whose I-bits are smaller than 5. For 590, We can find

1 ~ 499 how many of these numbers match the meaning of the question. There is also a I-1 in the number of digits if the high is 9, then the I-th as long as it is 4 can also match the meaning of the question, so with if (bit [I]> 4) ans + = dp [I-1] [1];

Code:

# Include
 
  
# Include
  
   
Using namespace std; // dp [I] [0] indicates the number of I not including 49. // dp [I] [1] indicates the number of I not including 49 and the highest bit is 9 // dp [I] [2] indicates the number of long dp [22] [3] whose length is I containing 49; int bit [21]; long n; void init () {dp [0] [0] = 1, dp [0] [1] = 0, dp [0] [2] = 0; for (int I = 1; I <= 21; I ++) {dp [I] [0] = dp [I-1] [0] * 10-dp [I-1] [1]; // dp [I] [0] adding a number at a high level is acceptable, but 49XXX may occur, remove dp [I] [1] = dp [I-1] [0]; // Add 9 dp [I] [2] = dp [I-1] [2] * 10 + dp [I-1] [1] without 49; // In the case of 49, either a high position or 49 is not included, but the high position is 9. You can add 4 to the highest position in the front.} lon G long cal (long n) {int len = 0; while (n) {bit [++ len] = n % 10; n/= 10 ;} long ans = 0; bit [len + 1] = 0; bool has = false; for (int I = len; I> = 1; I --) // each time determine a {ans + = dp [I-1] [2] * bit [I]; // low contains 49, high casually A 1, 2, 3 .... bit [I] can all be used. bit [I] represents several numbers, such as bit [I] = 5, which means there are five numbers, 0, 1, 2, 3, 4, smaller than 5. If (! Has) {if (bit [I]> 4) ans + = dp [I-1] [1]; // low high bit is 9, the above 4 can be} else ans + = (dp [I-1] [0]) * bit [I]; // if there are 49, just choose, for example, in 495, 490, 491, 492, 493, 494, and, the above sentence plagued me for more than a day. Why not write (d [I-1] [0] + dp [I-1] [2]) * bit [I] it, the previous has appeared 49 // so low any choice can, dp [I-1] [0] is those low don't appear 49, dp [I-1] [2] is those low appear 49, it is reasonable to say should // Add ah, BUT !!! Pay attention to the first sentence of the loop ans + = dp [I-1] [2] * bit [I]; the front has added the dp [I-1] [2] low position of the // 49, ah, want to cry ........ if (bit [I + 1] = 4 & bit [I] = 9) has = true;} if (has) ans ++; return ans ;} // take "491" as an example. First, find the number that matches the meaning of the question in all the numbers smaller than "400". Then, after determining "4", calculate that all values are smaller than "490, then find all the smaller than 491 // I = 3 find the number 049 149 249 349 // I = 2 find the number 449 // I = 1 find the number 490 // itself contains 49 So find out 491int main () {init (); int t; cin> t; while (t --) {cin> n; cout <
   
    


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.