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