母函数-HDU2189

好久没有做母函数了,今天又遇到了一道,顺便再熟悉一下。
HUD2189
意思是一个数用素数和来表示,共有多少种组合数。

#include <iostream>
using namespace std;
int num[151], c1[151], c2[151];
int Isprime( int number )
{
	for ( int i = 2; i <= (int) sqrt( 1.0 * number ); i++ )
		if ( number % i == 0 )
			return(0);
	return(1);
}


int main()
{
	int cas, j, i, n, l, k;
	cin >> cas;
	num[0] = 2; l = 1;
	for ( i = 3; i <= 160; i++ )
		if ( Isprime( i ) )
			num[l++] = i;
	for ( i = 0; i <= 150; i += 2 )
	{
		c1[i] = 1; c2[i] = 0;
	}
	for ( i = 2; i <= l; i++ )
	{
		for ( j = 0; j <= 150; j++ )
			for ( k = 0; k + j <= 150; k += num[i - 1] )
			{
				c2[j + k] += c1[j];
			}
		for ( j = 0; j <= 150; j++ )
		{
			c1[j] = c2[j]; c2[j] = 0;
		}
	}
	while ( cas-- )
	{
		cin >> n;
		cout <<c1[n]<<endl;
	}
	return(0);
}
# acm 

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×