hdu4651 Partition

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4651



Partition


Problem Description
How many ways can the numbers 1 to 15 be added together to make 15? The technical term for what you are asking is the "number of partition" which is often called P(n). A partition of n is a collection of positive integers (not necessarily distinct) whose sum equals n.

Now, I will give you a number n, and please tell me P(n) mod 1000000007.
 

Input
The first line contains a number T(1 ≤ T ≤ 100), which is the number of the case number. The next T lines, each line contains a number n(1 ≤ n ≤ 10 5) you need to consider.

 

Output
For each n, output P(n) in a single line.
 

Sample Input
  
  
4 5 11 15 19
 

Sample Output
  
  
7 56 176 490
 

题意:

1,2,……n中元素任意求和和为n的个数即为P(n),元素可以重复,要求求出P(n);


举例:n=5;  

  • 5
  • 4 + 1
  • 3 + 2
  • 3 + 1 + 1
  • 2 + 2 + 1
  • 2 + 1 + 1 + 1
  • 1 + 1 + 1 + 1 + 1

所以P(5)=7;


参考网址: https://en.wikipedia.org/wiki/Partition_(number_theory)  五边形数定理


P(n)=P(n-1)+P(n-2)-P(n-5)-P(n-7)+P(n-12)+……

不难得到

P(n)=∑(-1)^k * P(n-q[j])

其中q[j]为五边形数

k=1    q[1]=1,q[2]=2;

k=2    q[3]=5,q[4]=7;

k=3    q[5]=12,q[6]=14;

…………

k=n   q[2*k-1]=k*(3*k-1)/2 , q[2*k]=k*(3*k+1)/2;

然后套公式即可


#include <cstdio>
#include <iostream>
#include <cstring>
#include<cmath>
#include <string>
#include <queue>
#include <vector>
#include <set>
#include <stdlib.h>
#include <time.h>
//#include <map>
#include <algorithm>
using namespace std;
typedef long long LL;
const int INF=0x3f3f3f3f;
const double pi=acos(-1);
#define N 1005
//map<string,int>q;
//priority_queue<int>pq;
//priority_queue<int ,vector<int>,greater<int>>pq;
#define mod 1000000007
LL p[100010];
int q[100010];
int main()
{
    int k=1,t,n;
    q[0]=0;
    for(int i=1; q[k-1]<=100000; i++)
    {
        q[k++]=i*(3*i-1)/2;
        q[k++]=i*(3*i+1)/2;
    }
    //for(int i=0;i<10;i++)
      //  cout<<q[i]<<endl;

    p[0]=1;
    p[1]=1;
    p[2]=2;
    for(int i=3; i<=100000; i++)
    {
        p[i]=0;
        for(int j=1; i-q[j]>=0; j++)
        {
            k=0;
            if(j%2==0) k=j/2;
            else k=(j+1)/2;
            if(k%2==0) p[i]=(p[i]-p[i-q[j]])%mod;
            else p[i]=(p[i]+p[i-q[j]])%mod;
        }
    }
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        printf("%lld\n",(p[n]+mod)%mod);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值