HDU 1028 Ignatius and the Princess III(生成函数)

Description

n n 的本质不同拆分数

Input

多组用例,每组用例输入一整数n(1n120)

Output

对于每组用例,输出 n n 的本质不同拆分数

Sample Input

4
10
20

Sample Output

5
42
627

Solution

n的拆分数为 p(n) p ( n ) ,由五边形数定理有 p(n)=p(n1)+p(n2)p(n5)p(n7)+... p ( n ) = p ( n − 1 ) + p ( n − 2 ) − p ( n − 5 ) − p ( n − 7 ) + . . .

O(nn) O ( n n ) 即可筛出 p(1) p ( 1 ) ~ p(n) p ( n )

Code

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<ctime>
using namespace std;
typedef long long ll;
typedef pair<int,int>P;
const int INF=0x3f3f3f3f,maxn=123;
int n,f[maxn]; 
int main()
{
    for(int i=1;i<=120;i++)f[i]=0;
    f[0]=1;
    for(int i=1;i<=120;i++)
    {
        for(int j=1,w=1;w<=i;w+=3*j+1,j++)
            if(j&1)f[i]+=f[i-w];
            else f[i]-=f[i-w];
        for(int j=1,w=2;w<=i;w+=3*j+2,j++)
            if(j&1)f[i]+=f[i-w];
            else f[i]-=f[i-w];
    }
    while(~scanf("%d",&n))printf("%d\n",f[n]);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值