Detachment HDU - 5976 前缀+逆元+贪心

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


//14 2+3+4+5  120
//15 2+3+4+6  144
//16 2+3+5+6  180
//17 2+4+5+6  240
//18 3+4+5+6  360
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int mod=1e9+7;
const int M=1e5+10;
ll sum[M];//前缀和
ll inv[M],f[M];
int t;
ll x;
void init()
{
    sum[1]=0;inv[1]=1;f[1]=1;
    for(int i=2;i<M;i++)
    {
        inv[i]=(mod-mod/i)*inv[mod%i]%mod;//预处理线性求逆元
        sum[i]=sum[i-1]+i;
        f[i]=(f[i-1]*i)%mod;
    }
}
int main()
{
    scanf("%d",&t);
    init();
    while(t--)
    {
        scanf("%lld",&x);
        if(x<5)
        {
            printf("%lld\n",x);
            continue;
        }
        ll l=0,r=M-1;
        while(r-l>1)
        {
            int mid=(l+r)>>1;
            if(sum[mid]>x)
                r=mid;
            else
                l=mid;
        }
        ll k=x-sum[l],ans;
        if(2+k>l)
        {
            ans=(f[l]*inv[2]%mod*(2+k))%mod;
        }
        else
        {
            ans=(f[l+1]*inv[l-k+1]%mod)%mod;
        }
        printf("%lld\n",ans);
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值