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;
}