zoukankan      html  css  js  c++  java
  • 整数划分(允许相同),时间复杂度O(n*sqrt(n))

    基准时间限制: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
    ",dp[n]);
        }
        return 0;
    }
  • 相关阅读:
    冲刺(六)
    冲刺(五)
    冲刺(四)
    团队产品开发
    冲刺(三)
    冲刺(二)
    冲刺(一)
    电梯演讲,模型展示
    校园健康行的NABCD分析
    团队博客
  • 原文地址:https://www.cnblogs.com/mgz-/p/6782820.html
Copyright © 2011-2022 走看看