HDU --- (3555) bomb (Digital dp (Getting Started ))

Source: Internet
Author: User
Tags define local
Bomb

Time Limit: 2000/1000 MS (Java/others) memory limit: 131072/65536 K (Java/Others)
Total submission (s): 7921 accepted submission (s): 2778


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 input3150500

 

Sample output0115 HintFrom 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]

 

Source 2010 ACM-ICPC multi-university training Contest (12) -- host by whu question: Give You A number N, You need to count 1 to n appears contains 49 numbers: for example, 498,549, 49 ..... for this question: when I see a blog referencing this image and think it is clear, I will reference it .. our numerical analysis of the length of the I-1 is undoubtedly so concentrated (of course only around 49 Wow) first analysis: the length of the I-1 so for the I Length header is 49, the format must be: 49 ****? 49 ****(? It may be 9) the first letter of guarantee 9, so its format must be: 9 *****? 9 *****(? It may be 4) header position 49, so its format is :*******? *******(? May be 9) We may use DP [I] [2] to indicate that the header is 49, and DP [I] [1] to indicate that the header is 9, DP [I] [0] indicates that the header is not 49, so we can find a rule: DP [I-1] [2] Move one digit forward, that is, the original bit becomes ten, the shift from 10 to bits. The formation of DP [I] [2], but note: When DP [I-1] [2], actually I said above ,? It may be 9, so when we move one forward ,? The possibility of being 9 is removed. So DP [I-1] [2] * 10 (when moving one digit) needs to subtract the pattern DP [I-1] [1] starting with 9, so get: (1) DP [I] [2] = DP [I-1] [2] * 10-dp [I-1] [1]; for the I-bit header is 9 then the back only needs to meet not 49 can, exactly meet DP [I] [0]; (2) So DP [I] [1] = d [I-1] [0]; you can also analyze the content of the header that is not 49... DP [I] [0] = DP [I-1] [0] * 10 + dp [I-1] [1]; so a preprocessing equation is obtained: DP [I] [2] = DP [I-1] [2] * 10-dp [I-1] [1]; DP [I] [1] = d [I-1] [0]; DP [I] [0] = DP [I-1] [0] * 10 + dp [I-1] [1]; Code: For details, see the code:
1 // # define local 2 # include <cstdio> 3 # include <cstring> 4 # define ll _ int64 5 using namespace STD; 6 const int maxn = 25; 7 ll dp [maxn] [3] = {0}; 8 int NN [maxn]; 9 int main () 10 {11 12 # ifdef local13 freopen ("test. in "," r ", stdin); 14 # endif15 int cas, I; 16 LL N; 17 scanf (" % d ", & CAS ); 18/* Common Mode preprocessing of digital DP */19 DP [0] [0] = 1; 20 for (I = 1; I <= 20; I ++) 21 {22 DP [I] [0] = DP [I-1] [0] * 10-dp [I-1] [1]; 23 DP [I] [1] = DP [I-1] [0]; 24 DP [I] [2] = DP [I-1] [2] * 10 + dp [I-1] [1]; 25} 26 while (CAS --) 27 {28 scanf ("% i64d", & N); 29 I = 0; 30 N + = 1; 31 memset (NN, 0, sizeof (NN )); 32 While (n> 0) 33 {34 NN [++ I] = n % 10; 35 N/= 10; 36} 37 ll ans = 0; 38 bool tag = 0; 39 int num = 0; 40 for (; I> = 1; I --) 41 {42 ans + = DP [I-1] [2] * NN [I];/* calculate the number starting with 49 */43 If (TAG) {44 ans + = DP [I-1] [0] * NN [I];/* when the current surface appears 49, then any number that appears later must be calculated */45} 46 If (! Tag & NN [I]> 4) 47 {48 ans + = DP [I-1] [1];/* If the start of 49 does not appear, as long as the header is greater than 5, then there must be a 49 */49} 50 if (num = 4 & NN [I] = 9) 51 tag = 1; 52 num = nn [I]; 53} 54 printf ("% i64d \ n", ANS); 55} 56 return 0; 57}
View code

 

HDU --- (3555) bomb (Digital dp (Getting Started ))

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.