Hangzhou Electric 1284 Coin Exchange Problem "total Knapsack solution"

Source: Internet
Author: User

Solution idea: Because the state transfer equation for the complete backpack F[v]=max (F[v],f[v-c[i]]+w[i]) has documented all of the backpack composition scheme, but usually ask for the maximum value, now requires the total number of programs

That is F[v]=sum (F[v],f[v-c[i]+w[i]]),

Problem description in a country only 1 points, 2 points, 3 cent coins, the exchange of money n into coins there are many kinds of laws. Please compile the procedure to calculate the total number of different methods. Input has only one positive integer n,n less than 32768 per line. Output corresponds to each input and the number of redemption methods. Sample Input2934 12553 Sample Output718831 13137761
#include <stdio.h> #include <string.h>long long  f[50000];int a[4];long long sum (long long A,long long b) {    return a+b;} int main () {    int n,i,v;    while (scanf ("%d", &n)!=eof)    {        memset (f,0,sizeof (f));        F[0]=1;        A[1]=1;        a[2]=2;        a[3]=3;        for (i=1;i<=3;i++)        {for            (v=a[i];v<=n;v++)            f[v]=sum (F[v],f[v-a[i]]);        }        printf ("%i64d\n", F[n]);    }    }

  

Hangzhou Electric 1284 Coin Exchange Problem "total Knapsack solution"

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.