Replace the check for N $ into three coins: 1 $, 2 $, and 3 $.

Source: Internet
Author: User

Problem description:
There are three kinds of coins: 1 $, 2 $, 3 $. given a chique of N $(0 <n <= 10 ^ 9), calculate the number of different ways to exchange the cheque.
Sample input: 1 2 3
Sample outpt: 1 2 3

That is to say, I want to change the check for N $ to 1 $, 2 $, and 3 $. How many exchange methods are there? The question seems simple, the problem is that ACM-ICPCProgramThere is a strict limit on the running time of, for the input is 10 ^ 9 such a large number to calculate its exchange method is not goodAlgorithmIt took me 5 seconds to run the code, and the result still timed out. I am not a computer professional student, and have never studied programming and algorithms. I am willing to join the competition with a learning attitude. Please help me. I am very grateful and have a high score.

Simple:
Long partition (INT m ){
Int v = m/6, I, m = m % 6;
Long c = 3 * V + 3 * V + 1;
If (M = 0) return C;
Else return C + M * V + m-1;
}

very simple. first, consider the case where M is a multiple of 6.
for any y, z
2y + 3z <= m we all have a solution x = M-2y-3z
so as long as the number
2y + 3z <= m, Y> = 0, z> = 0 Number
while M is a multiple of 6, this image is a triangle on the plane, and the three vertices of the triangle are lattice points
we can directly use the area of the lattice triangle to calculate the number of points:
S = E/2 + I-1, E indicates the number of vertices on the boundary, and I indicates the number of internal vertices. (In fact, the number of direct number points is good.)
E is easy to count, so that I + E = 3 * V + 3 * V + 1 can be calculated, (V = m/6)
then, for m, the remainder of 6 is not 0, we only need to calculate
2y + 3z = 6 V + 3 ,....
corresponds to the number of
2y + 3z = 6 V + T
when T = 1, it is V. For T> 1, yes V + 1

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.