HDU 1284 coin exchange (MAGIC)

Source: Internet
Author: User

/* (Amazing question) I thought it was a problem with the mother function at the beginning, but I can solve it with the mother function, but it definitely times out. Later, Baidu found the following solution:
# Include <iostream>
# Include <cstdio>
Using namespace STD;
Int A [32768];
Int main ()
{
Int N, I;
A [0] = 0;
For (I = 1; I <32768; I ++)
{
If (I % 6 = 1 & I! = 1)
A [I] = A [I-1] + (I/6 );
Else
A [I] = A [I-1] + (I/6 + 1 );
}
While (scanf ("% d", & N )! = EOF)
Printf ("% d/N", a [n]);
Return 0;
}

 

If (I % 6 = 1 & I! = 1)
A [I] = A [I-1] + (I/6 );
Else a [I] = A [I-1] + (I/6 + 1 );

This general expression.

It is a recursive formula,

First, the step a [I] = A [I-1] is actually adding a 1 cent coin in each case on the basis of a [I,

Then: (I/6) or (I/6 + 1); that is, it does not contain 1 cent coin, why I % 6 = 1 & I! If it is set to 1, do not add 1? I! When it is set to 1, you don't have to say you understand it .. I'm talking about I % 6 = 1. You can combine the coins with 2, 3, 4, 5, and 6 (0) points, so there is no combination when I % 6 = 1, and other I % 6 = 2, I % 6 = 3, I % 6 = 4, I % 6 = 5 will all have and there is only one combination to form it, So Add 1.

Finally, I will go back and talk about (I/6). Divide by 6 because when the number is greater than 6, it can be written as N * 6 + k, and each K will have a combination that matches it, of course, except k = 1, I/6 is N, and N is (6 * n) the number of m which is combined by 2 and 3 Coins minus 1, why subtract 1, because (6 * n) is actually the one mentioned above. I don't know why n is equivalent to m-1, but you can write data and see it.

Coin Exchange Problems
Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 1425 accepted submission (s): 679

Problem description
In a country, only one cent, two cent, and three cent coins can be exchanged into coins. CompileProgramCalculate the total number of exchange methods.
 

Input
Each row has only one positive integer N, and N is less than 32768.
 

Output
The number of exchange methods for each input.
 

Sample Input
2934
12553
 

Sample output
718831
13137761
*/
# Include <iostream>
# Include <cstdio>
Using namespace STD;
Const int FF = 32768;
Int c1 [ff];
Int c2 [ff];
Int main ()
{
Int N, I, J, K;
While (CIN> N)
{
Memset (C1, 0, sizeof (C1 ));
Memset (C2, 0, sizeof (C2 ));
C1 [0] = 1;
For (I = 1; I <= 3; I ++)
{
For (j = 0; j <= N; j ++)
For (k = 0; k + j <= N; k + = I)
{
If (c1 [J])
C2 [J + k] + = c1 [J];
}
For (k = 0; k <= N; k ++)
{
C1 [k] = c2 [k];
C2 [k] = 0;
}

}

Printf ("% d/N", c1 [N]);
}
}

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.