Coin Exchange Question Hdu-1284__hdu

Source: Internet
Author: User
Problem Description in a country with only 1 cents, 2 cents, 3 cents, there are many ways to exchange money n coins. Please write a procedure to figure out how many kinds of methods are in common.
Input is only one positive integer per line n,n less than 32768.
Output corresponds to each input, the number of conversion methods.
Sample Input 2934 12553
Sample Output 718831 13137761
Author Smallbeer (CML)
Source Hangzhou ACM Team Training Tournament (VII) method one: (full backpack DP) #include <stdio.h> int dp[35000]; int main () {
int i,j;
Dp[0]=1;
for (i=1;i<=3;i++)
for (j=i;j<=35000;j++)
Dp[j]+=dp[j-i];
int n;
while (scanf ("%d", &n)!=eof) {
printf ("%d\n", Dp[n]);
}
return 0;
}
Method 2: Turn: Thinking: First look can exchange how many three-cent coins, and then when the three-penny coin is 1,2,3, .... n how many 2-cent coins are there, and why it is so determined. For as long as you can redeem a three-penny coin and a two-penny coin, the remaining value must be filled with a coin worth 1. The beginning why S is n/3+1. Because you can think so, suppose n=7, then only contains 3 cents and 1 cents of the combination of the way: 3,3,1, 3,1,1,1,1, so N/3 is actually able to hold three cents in the number of coins. An increase of 1 is due to the fact that all coins can be replaced by 1 cents. Some people will question, then T = (n-3*i)/2 will not repeat it. This is not possible because the value of coins is incremented, and the value of T can be 0 or 1 when I is N/3.   So it won't repeat. #include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace Std; int N; int main ()
{
while (~SCANF ("%d", &n))
{
int s = n/3+1;
for (int i = 0; I <= n/3; i++)
{
int t = (n-3*i)/2;
s + = t;
}
printf ("%d\n", s);
}
return 0;
}
Method 3: (parent function) The meaning of the topic is to give you three denominations are 1 points, 2 points, 3 points, then ask you a money n there are several options can be pieced together to get this amount of money N.

The problem is still pretty good, and we can define the parent function g (x) = (1+x+x^2+x^3--------) * (1+x^2+x^4+x^6+x^8--------) * (1+x^), based on the definition of the masterbatch and the meaning expressed by each term and coefficient of the polynomial. 3+x^6+x^9--------------), the exponent that we use for the expansion of the parent function is the number of coins that can be represented, and the coefficient is the number of schemes that represent the number of coins.

So the problem is to find the corresponding n coefficients on it, this is relatively simple, is the simulation of hand-polynomial expansion, #include <cstring>
#include <cstdlib>
#include <cstdio>
using namespace Std;
const int max=32769;
int Ans[max];
int Tans[max];

Int main ()
{
int i,j,n;
for (int i=0;i<max;i++)
Ans[i]=1;
memset (Tans,0,sizeof (Tans));
for (int k=2;k<=3;k++)
{
for (i=0;i<max;i++)
{
for (j=0;i+j<max;j+=k)
{
Tans[i+j]+=ans[i];
}
}
for (i=0;i<max;i++)
{
Ans[i]=tans[i];
tans[i]=0;
}
}
while (scanf ("%d", &n)!=eof)
{
printf ("%d\n", Ans[n]);
}
return 0;
}

Related Article

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.