Ultraviolet A 11038-How Many O & amp; #39; s? (Counting problem)

Source: Internet
Author: User

Question link: 11038-How Many O's? Question: The number of occurrences of 0 between [a. B. Idea: I started thinking about the digital DP at the beginning and found that it was quite complicated .. First, convert the problem to the number of 0-num. If we use the number of B minus the number of a, we only need to use the multiplication and addition principle of count, divide each digit of a number into the left and right sides, calculate the total number based on the multiplication principle, and add all the total numbers to the total number based on the addition principle. Then we will discuss the situation on both sides. For example, if 23045 sets the intermediate bit as mid, if the mid is not 0, and if it is 4, then there are [1-230] cases on the left, on the right side, there are [0-9] cases. If we seek 3, there will be [1-2] types on the left and [0-999] types on the right, because no matter how you obtain the right side, it cannot exceed the number. If the value of mid is 0, [1-22] on the Left can be randomly set to [1-100] on the right. If the value of 23 on the Left can only be set to [0-45] on the right. In this way, the code can be calculated from the low position to the high position:

# Include

 
  
# Include

  
   
Long a, B; long solve (long left) {if (left <0) return 0; long ans = 1, mid, right = 0, j = 1; while (left> = 10) {mid = left % 10; left/= 10; if (mid) ans + = left * j; else ans + = (left-1) * j + right + 1; right = right + mid * j; j * = 10;} return ans;} int main () {while (~ Scanf ("% lld", & a, & B) &! =-1 | B! =-1) {printf ("% lld \ n", solve (B)-solve (a-1);} return 0 ;}

  

 


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.