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.
Author [email protected]
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 <iostream> # include <string. h> 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 case of 49 If the next high position is either 49 or not, but the top position is 9, you can add 4 to the first highest position.} 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 <CAL (n) <Endl ;}return 0 ;}