2016ICPC大连站 F Detachment【数学】

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, … (x= a1+a2a1+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 ( ai≠ajai≠aj when i≠j).
2.Make this multidimensional space size s as large as possible (s= a1∗a2a1∗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

从2开始选取最接近的数,无法叠加时,将多余的部分平均分配给靠后的数字(+1)。
如果恰好剩余的数等于最后一个数,最后一个数+2。

#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <vector>
#include <algorithm>
using namespace std;
#define LL long long
const LL maxn = 45000+44;
const LL mod = 1e9+7;
LL x[maxn];
LL j[maxn];
LL qkm(LL base,LL mi){
    LL ans=1;
    base%=mod;
    while(mi){
        if(mi&1) ans=ans*base;
        base=base*base;
        mi>>=1;
        base%=mod;
        ans%=mod;
    }
    return ans;
}
int main()
{
    x[1]=0;
    LL cnt;
    for(cnt=2;;cnt++){
        x[cnt]=x[cnt-1]+cnt;
        if(x[cnt]>=1e9)  break;
    }
    j[1]=1;
    for(LL i=2;i<=cnt;i++){
        j[i]=j[i-1]*i%mod;
    }
    LL T;scanf("%lld",&T);
    while(T--){
        LL n;scanf("%lld",&n);
        LL now=0;
        LL k=0;
        k=upper_bound(x+1,x+1+cnt,n)-x;
        if(x[k]>n) k--;
        now=x[k];
       // printf("%lld %lld\n",now,k);
        LL shen=n-now;
        LL ans;
        if(n<=4) {
            printf("%lld\n",n);
            continue;
        }
        if(shen==k){
            ans=j[k+2]*qkm(2,mod-2)%mod*qkm(k+1,mod-2)%mod;//!!!!NIYUAN;

        }
        else
        {
            ans=j[k+1]*qkm(k+1-shen,mod-2)%mod;
        }
        printf("%lld\n",ans);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值