码迷,mamicode.com
首页 > 其他好文 > 详细

整数划分(允许相同),时间复杂度O(n*sqrt(n))

时间:2017-04-28 20:33:25      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:pen   ace   span   stream   efi   names   algorithm   51nod   long   

基准时间限制:1 秒 空间限制:131072 KB
将N分为若干个整数的和,有多少种不同的划分方式,例如:n = 4,{4}  {1,3}  {2,2}  {1,1,2} {1,1,1,1},共5种。由于数据较大,输出Mod 10^9 + 7的结果即可。
Input
输入1个数N(1 <= N <= 50000)。
Output
输出划分的数量Mod 10^9 + 7。
Input示例
4
Output示例
5

五边形数定理
技术分享

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<algorithm>
#include<iostream>
#include<queue>
#include<map>
#include<cmath>
#include<set>
#include<stack>
#define ll long long
#define max(x,y) (x)>(y)?(x):(y)
#define min(x,y) (x)>(y)?(y):(x)
#define cls(name,x) memset(name,x,sizeof(name))
using namespace std;
const int inf=1<<28;
const int maxn=50010;
const int maxm=110;
const int mod=1e9+7;
const double pi=acos(-1.0);
int dp[maxn];
int main()
{
    //freopen("in.txt","r",stdin);
    int n;
    while(~scanf("%d",&n))
    {
        cls(dp,0);
        dp[0]=1;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;;j++)
            {
                if(i-j*(j*3-1)/2>=0)
                dp[i]=((dp[i]+((j+1)%2==0?1:-1)*dp[i-j*(j*3-1)/2])%mod+mod)%mod;
                if(i-j*(j*3+1)/2>=0)
                dp[i]=((dp[i]+((j+1)%2==0?1:-1)*dp[i-j*(j*3+1)/2])%mod+mod)%mod;
                else break;
            }
        }
        printf("%d\n",dp[n]);
    }
    return 0;
}

 

整数划分(允许相同),时间复杂度O(n*sqrt(n))

标签:pen   ace   span   stream   efi   names   algorithm   51nod   long   

原文地址:http://www.cnblogs.com/mgz-/p/6782820.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!