UV 10313 Pay the Price

Source: Internet
Author: User

UVA_10313

After reading RoBa's question, I finally realized it.

This topic involves a conclusion that the number of solutions to generate I with no more than j coins is the same as the number of solutions to generate I with a coin with no more than j coins. A mathematical point is that integer I is split into split scores of up to j integers, which is the same as the split scores of integer I split into several integers whose values do not exceed j. The specific proof uses the Ferrers image nature.

In this way, we can take a two-dimensional array f [I] [j] to show the number of methods to use coins with a nominal value not greater than j to generate a nominal value I. If I use a nominal value j, f [I-j] [j] should be added to the number of corresponding solutions, if we do not use the face value j, then the corresponding number of solutions should be added with f [I] [J-1]. That is to say, the state transition equation is f [I] [j] = f [I-j] [j] + f [I] [J-1].

#include<stdio.h>
#include<string.h>
#define MAXN 310
char b[100];
int N, L1, L2;
long long int f[MAXN][MAXN];
void prepare()
{
int i, j;
N = 300;
memset(f, 0, sizeof(f));
f[0][0] = 1;
for(i = 0; i <= N; i ++)
for(j = 1; j <= N; j ++)
{
if(i - j >= 0)
f[i][j] += f[i - j][j];
if(j - 1 >= 0)
f[i][j] += f[i][j - 1];
}
}
void solve()
{
int i, j;
L1 = L2 = -1;
sscanf(b, "%d%d%d", &N, &L1, &L2);
L1 = L1 > 300 ? 300 : L1;
L2 = L2 > 300 ? 300 : L2;
if(L1 == -1)
printf("%lld\n", f[N][N]);
else
{
if(L2 == -1)
printf("%lld\n", f[N][L1]);
else
{
if(L1 == 0)
printf("%lld\n", f[N][L2]);
else
printf("%lld\n", f[N][L2] - f[N][L1 - 1]);
}
}
}
int main()
{
prepare();
while(gets(b) != NULL)
{
solve();
}
}


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.