2016 ACM/ICPC 大连区域赛 F—Detachment【贪心+逆元】

本文深入探讨了一道经典的算法题目,即如何将一个整数n拆分成若干个不相同整数之和,使得这些整数的乘积达到最大。文章详细分析了贪心算法的应用,并提供了具体的代码实现,帮助读者理解并解决类似问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://acm.hdu.edu.cn/contests/contest_showproblem.php?pid=1006&cid=736

Detachment

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


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,a2a1+a2
ai≠aj
a1∗a2*...).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(n<=1e9),让你把n拆成若干个不相同的数的和,且这些数的积是所有拆分方法中最大的。输出这些数的最大积对1e9+7取模。

分析:

要取乘积的最大,要把数尽量分成多个【贪心】,例如2+3+4+。。。。+ans,如果和为n,那就输出ans的阶乘;

如果比n大1,输出3*4*....*(ans-1)*(ans+1)

其他情况差为x输出2*....*(x-1)*(x+1)*......*ans

代码:

#include<bits/stdc++.h>
#define ll long long
#define mod 1000000007
using namespace std;
ll a[100055];
ll n,ans;
bool ok(ll mid)
{
    return (mid+1)*mid/2-1>=n;
}
ll power(ll aa,ll  n)   //a的n次方mod
{
    ll ans=1;
    aa=aa%mod;
    while (n)
    {
        if(n&1)
        ans=(ans*aa)%mod;
        n>>=1;
        aa=(aa*aa)%mod;
    }
    return ans;
}
int main()
{
    ll t,i,j,l,r,pos,mid;
    ll tt;
    a[0]=1;
    for(i=1;i<=100005;i++)
        a[i]=(a[i-1]*i)%mod;
    scanf("%lld",&t);
    while(t--)
    {
        scanf("%lld",&n);
        l=1;
        r=n;
        if(n<=4)
        {
            printf("%lld\n",n);
            continue;
        }
        while(l<=r)
        {
            mid=(l+r)>>1;
            if(ok(mid))
            {
                r=mid-1;
                ans=mid;
            }
            else
                l=mid+1;
        }
        pos=ans;
        ans=ans*(ans+1)/2-1-n;
        if(ans==0)
        {
            printf("%lld\n",a[pos]);
            continue;
        }
        if(ans==1)
        {
            tt=a[pos-1]*(pos+1)%mod;
            tt=tt*power(2,mod-2)%mod;
            printf("%lld\n",tt);
            continue;
        }
        else
        {
            tt=a[pos];
            tt=tt*power(ans,mod-2)%mod;
            printf("%lld\n",tt);
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值