hdu 5976 Detachment(数论,一个结论)

题目链接

Detachment

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 581    Accepted Submission(s): 197


Problem Description
In a highly developed alien society, the habitats are almost infinite dimensional space.
In the history of this planet,there is an old puzzle.
You have a line segment with x units’ length representing one dimension.The line segment can be split into a number of small line segments:  a1,a2 , … (x=  a1+a2 +…) assigned to different dimensions. And then, the multidimensional space has been established. Now there are two requirements for this space:  
1.Two different small line segments cannot be equal (  aiaj  when i≠j).
2.Make this multidimensional space size s as large as possible (s=  a1a2 *...).Note that it allows to keep one dimension.That's to say, the number of ai can be only one.
Now can you solve this question and find the maximum size of the space?(For the final number is too large,your answer will be modulo 10^9+7)
 

Input
The first line is an integer T,meaning the number of test cases.
Then T lines follow. Each line contains one integer x.
1≤T≤10^6, 1≤x≤10^9
 

Output
Maximum s you can get modulo 10^9+7. Note that we wants to be greatest product before modulo 10^9+7.
 

Sample Input
  
  
1 4
 

Sample Output
  
  
4
 



参考博客

题意:

  给出一个数N,让你把它拆成若干各不相同的数Ai,满足ΣAi=N,使得ΠAi(累乘)最大。


题解:

此题关键在于得出如何能使乘积s最大

按照以往经验,必然是取一段连续自然数能够使得乘积最大,而这段连续自然数可从2开始(为啥不从1开始?从1开始还不如将这个1给这段连续自然数的最后一个数),于是我们可以得到形如2+3+4+...+k(k=2,3,...)的式子,而x是10^9内的任意整数,我们不可能恰好能够凑成连续自然数之和,可能会多出△x

而这个△x的值,我可以保证它的范围为0≤△x≤k,相信大于等于0还是好理解的,为什么会小于等于k呢?因为当它大于k时,原式不是可以增加一项?即2+3+4+...+k+(k+1)

那么多出来的△x怎么处理呢?显然是从后往前均摊给连续自然数中的(k-1)个数,为啥从后往前?因为若我们从前往后,总是会使连续自然数重复,不好处理

于是,在我们分配完△x之后,我们大致会得到下述两种式子:

①2*3*...*(i-1)*(i+1)*...*k*(k+1)  △x<=k-1

②3*4*...*i*(i+1)*...*k*(k+2).   △x=k

显然,我们要计算此结果,可以借助阶乘,而阶乘中缺失的项,我们除掉就可以了,那么便会涉及除法取模,显然需要用到乘法逆元

做法讲解完毕,以下是为什么连续段乘积最大的大概证明:


注意上文中性质2的证明方向我觉得有点小问题,应该先假设个数多于1个,那么任取ai+1-ai=2,aj+1-aj=2(i<j),当把ai加1,aj+1减去1时结果更优,所以最多只有一个。


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
#include<stack>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define fi first
#define se second
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
const int inf=0x3fffffff;
const ll mod=1000000007;
const int maxn=100000+10;
ll inv[maxn],f[maxn];
ll sum1[maxn];
void gcd(ll a,ll b,ll& d,ll& x,ll& y)
{
    if(!b){d=a,x=1,y=0;}
    else {gcd(b,a%b,d,y,x),y-=x*(a/b);}
}
void init()
{
    f[0]=1;
    sum1[1]=0;
    rep(i,1,maxn)
    {
        f[i]=(f[i-1]*i)%mod;
        //sum1[i]=sum1[i-1]+i;
        ll x=0,y=0,d=0;
        gcd(f[i],mod,d,x,y);
        inv[i]=(x+mod)%mod;
    }
    rep(i,2,maxn) sum1[i]=sum1[i-1]+i;
}
int main()
{
    int cas;
    scanf("%d",&cas);
    init();
    while(cas--)
    {
        ll x;
        scanf("%lld",&x);
        if(x==1||x==2)
        {
            printf("%lld\n",x);
            continue;
        }
        ll sum=0,k=1;
        k=upper_bound(sum1+2,sum1+maxn,x)-sum1;
        if(sum1[k]>x) k--;
        sum=sum1[k];
        ll ans=0;
        ll m=x-sum;
        if(!m)
            ans=f[k]%mod;
        else
        {
            if(m==k)
            {
                ans=f[k]*inv[2]%mod;
                ans=ans*(k+2)%mod;
            }
            else{
                ans=f[k-m]*f[k+1]%mod*inv[k-m+1]%mod;
            }
        }
        printf("%lld\n",ans);
    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值