"Training Guide"--6.12

Source: Internet
Author: User

uva:11361

Number and with multiples

Given a positive integer A, b, and K, your task is to count the number of n in all the integers that satisfy a≤n≤b, and how many of them satisfy N itself as a multiple of k, and the Numbers (decimal) on each digit of N and the multiples of k?

Analysis: Set F (x) is not more than X non-negative integer to meet the number of the problem set requirements, for this solution of an interval, then the interpretation of this topic F (b) –f (A-1), then the following question is how to solve f (x).

For larger-scale problems here, the value of a and B is large, so here we examine how the larger and smaller numbers of the case have a recursive relationship.

And this relationship is obviously based on the constraints given by the topic, we need to characterize that each state can be divisible by K and the sum of the numbers on each one can be divisible by k, so we need to expand the f (X) definition.

Set F (x,m1,m2) indicates that there is an X-bit "free bit" (the concept of a recursive relationship after the time will give a specific meaning), and this state of a certain number of the result of the M1 K, The number of figures and the result of the remainder K is m2. So for numbers with higher digits but the first few digits are deterministic, we can build a recursive relationship to narrow down the size of the problem. For example, for the figure 22xxx, which represents an integer on [22000,22999], there are three "free bits", that is, the bit can be taken over [0,9]. It can be seen that the solution on this interval corresponds to another smaller state xxx, taking into account the number of digits on the 22xxx to determine the number of answers to this state is F (3,3,1).

For [1,a], we can build a multi-fork tree that iterates through the highest-bit integers to determine the number on that number to complete the recursive relationship we described above to reduce the size of the problem.

So there are the following recursive equations:

In the programming process, it is important to note that for each state of the parameter m1,m2, in the form of recursion may appear negative, just the (? + k)%k operation can be.

Because of the time problem here, the recursive equation is analyzed, and the code is folded.

"Training Guide"--6.12

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.