HDU 1398 Square Coins
好吧。。 这题是母函数的同一类型题。。。。。暴力也可以过
#include<stdio.h> int sq[18],m1[305],m2[305]; void chart( ) { for( int i = 0; i < 305; ++i ) m1[i] = m2[i] = 0; for( int i = 0; i < 18; ++i ) sq[i] = i * i; m1[ 0 ] = 1; for( int i = 1; i <= 17; ++i ) { for( int j = 0; j <= 300; ++j ) for( int k = 0; ( j + k * sq[i] <= 300 ); ++k ) m2[j + k *sq[i]] += m1[j]; for( int j = 0; j <= 300; ++j ) m1[j] = m2[j],m2[j] = 0; } } int main( ) { chart( ); int n; while( scanf( "%d",&n ),n ) printf( "%d\n",m1[n] ); return 0; }
下面是背包解法,没有数量限制的背包都是用完全背包解法
#include<stdio.h> #include<string.h> int v[18],dp[305],n; void DP( ) { memset( dp,0,sizeof( dp ) ); for( int i = 0; i < 18; ++i ) v[i] = i * i; dp[0] = 1; for( int i = 1; i <= 17; ++i ) for( int j = v[i]; j <= 300; ++j ) dp[j] += dp[j-v[i]]; } int main( ) { DP( ); while( scanf( "%d",&n ),n ) printf( "%d\n",dp[n] ); return 0; }
本人还是新手 ,转载请注明来自Lvsi‘s home