hdu 2189 悼念512汶川大地震遇难同胞——来生一起走 (母函数)

http://acm.hdu.edu.cn/showproblem.php?pid=2189 题意:n个人可以分成若干组,每组人数都为素数,问有多少种分法。 思路:母函数。先预处理素数,记得多处理一点...

/* ***********************************************
Author :111qqz
Created Time :2016年02月26日 星期五 16时24分17秒
File Name :code/hdu/2189.cpp
************************************************ */
 1#include <cstdio>muhanshu
 2#include <cstring>
 3#include <iostream>
 4#include <algorithm>
 5#include <vector>
 6#include <queue>
 7#include <set>
 8#include <map>
 9#include <string>
10#include <cmath>
11#include <cstdlib>
12#include <ctime>
13#define fst first
14#define sec second
15#define lson l,m,rt<<1
16#define rson m+1,r,rt<<1|1
17#define ms(a,x) memset(a,x,sizeof(a))
18typedef long long LL;
19#define pi pair < int ,int >
20#define MP make_pair
 1using namespace std;
 2const double eps = 1E-8;
 3const int dx4[4]={1,0,0,-1};
 4const int dy4[4]={0,-1,1,0};
 5const int inf = 0x3f3f3f3f;
 6const int N=155;
 7int pri[N];
 8int a[N],tmp[N];
 9int cnt;
10int n;
1bool judge ( int n)
2{
3    if (n<=3) return true;
4    for ( int i = 2 ; i*i <= n ; i++)
5    {
6	if (n%i==0) return false;
7    }
    return true;
} 
 1void pre()
 2{
 3    cnt  = 0;
 4    for ( int i = 2 ; i <= 200 ;  i++)
 5    {
 6	if (judge(i))
 7	    pri[++cnt] = i;
 8    }
 9}
10int main()
11{
12	#ifndef  ONLINE_JUDGE 
13	freopen("code/in.txt","r",stdin);
14  #endif
1	int T;
2	pre();
3	scanf("%d",&T);
4	while (T--)
5	{
6	    scanf("%d",&n);
1	    ms(a,0);
2	    for ( int i = 0 ; i <= n ; i+=2 )
3	    {
4		a[i] = 1;
5		tmp[i] =  0;
6	    }
 1	    for ( int i = 2 ; pri[i] <= n ; i++)
 2	    {
 3		for ( int j = 0 ; j <= n ; j++)
 4		{
 5		    for ( int k = 0 ; k + j <= n ; k +=pri[i])
 6		    {
 7			tmp[j+k] +=a[j];
 8		    }
 9		}
10		for ( int j = 0 ; j <= n ; j ++)
11		{
12		    a[j] = tmp[j];
13		    tmp[j] =  0;
14		}
15	    }
	    printf("%d\n",a[n]);
	}
1  #ifndef ONLINE_JUDGE  
2  fclose(stdin);
3  #endif
4    return 0;
5}