Hdu 1398 Square Coins (primary function, full backpack), hducoins

Source: Internet
Author: User

Hdu 1398 Square Coins (primary function, full backpack), hducoins

Link: hdu 1398

There are 17 currencies with an unlimited denomination of I * I (1 <= I <= 17,

Given a value of n (n <= 300), calculate the number of solutions in which the sum of values is n using the aforementioned currency.

Analysis: You can use the idea of the primary function to pre-process the values less than 300.

You can also use a full backpack to calculate the number of solutions within 300.


Primary function:

# Include <stdio. h> int main () {int c1 [305], c2 [305], I, j, k, n; for (I = 0; I <= 300; I ++) {c1 [I] = 1; c2 [I] = 0 ;}for (I = 2; I <= 17; I ++) {for (j = 0; j <= 300; j ++) for (k = 0; j + k <= 300; k + = I * I) // here I * I is accumulated every time, because the currency denomination I * I c2 [j + k] + = c1 [j]; for (j = 0; j <= 300; j ++) {c1 [j] = c2 [j]; c2 [j] = 0 ;}} while (scanf ("% d ", & n )! = EOF) {if (n = 0) break; printf ("% d \ n", c1 [n]);} return 0 ;}

Full backpack

#include<stdio.h>int main(){    int a[20]={0,1,4,9,16,25,36,49,64,81,100,121,144,169,196,225,256,289},f[305]={0};    int i,n,j;    f[0]=1;    for(i=1;i<=17;i++)        for(j=a[i];j<=300;j++)            f[j]+=f[j-a[i]];          while(scanf("%d",&n)!=EOF){        if(n==0)            break;        printf("%d\n",f[n]);    }    return 0;}



Hdu1398, just learned the master function, I wrote the code for 15 MS and couldn't think of better optimization, but there are a lot of 0MS to submit the list, Niu helps to improve it

What about preprocessing first? This should be faster.

# Include "stdio. h"
# Include "string. h"
Int main ()
{
Int c1 [301];
Int c2 [301];
Int I, j, k;
Int aim;

Memset (c1, 0, sizeof (c1 ));
Memset (c2, 0, sizeof (c2 ));
For (I = 0; I <= 300; I ++) c1 [I] = 1;

For (I = 2; I <= 17; I ++)
{
For (j = 0; j <= 300; j ++)
{
If (c1 [j] = 0)
Continue;
For (k = 0; k + j <= 300; k + = I * I)
C2 [k + j] + = c1 [j];
}
For (j = 0; j <= 300; j ++)
{
C1 [j] = c2 [j];
C2 [j] = 0;
}
}

While (scanf ("% d", & aim), aim)
{
Printf ("% d \ n", c1 [aim]);
}
Return 0;
}


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.