i相信知道做前面那个后这个应该很水了
#include<stdio.h>
int m1[40000],m2[40000];
void chart( )
{
for( int i = 0 ; i <=32780; ++i )
{
m1[i] = 1;
m2[i] = 0;
}
for( int i = 2; i <= 3;++i )
{
for( int j = 0; j <= 32780; ++j )
for( int k = 0; k + j <= 32780; k += i )
m2[j + k] += m1[j];
for( int j = 0; j <= 32780; ++j )
{
m1[j] = m2[j];
m2[j] = 0;
}
}
}
int main( )
{
chart( );
int n;
while( scanf( "%d",&n ) != EOF )
printf( "%d\n",m1[n] );
return 0;
}
用完全背包做
#include<stdio.h>
#include<string.h>
int coin[4] = { 0 , 1, 2 , 3},dp[32780],n;
void DP( )
{
memset( dp,0,sizeof( dp ) );
dp[0] = 1;
for( int i = 1; i <= 3; ++i )
for( int j = coin[i]; j <= n; ++j )
dp[j] += dp[j-coin[i]];
printf( "%d\n",dp[n] );
}
int main( )
{
while( scanf( "%d",&n ) != EOF )
DP( );
return 0;
}