首页 > 代码库 > HDU 5976 Detachment

HDU 5976 Detachment

Detachment



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= 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

 

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
const int MAXN=1e5+5;
const LL MOD=1e9+7;
int has[MAXN],sum[MAXN],inv[MAXN],tot;
LL pro[MAXN];
int get_inv(int x)
{
    if(x==1)return 1;
    return (MOD-MOD/x)*get_inv(MOD%x)%MOD;
}
void table()
{
    has[2]=5;
    tot=2;
    while(has[tot]<=1e9)
    {
        tot++;
        has[tot]=has[tot-1]+tot+1;
    }
    sum[1]=2;
    for(int i=2;sum[i-1]<=1e9;i++)
        sum[i]=sum[i-1]+i+1;
    inv[1]=pro[1]=1;
    for(int i=2;i<MAXN;i++)
        (pro[i]=pro[i-1]*i)%=MOD,inv[i]=get_inv(pro[i]);
    for(int i=2;i<MAXN;i++)
        inv[i]=(MOD-MOD/i)*inv[MOD%i]%MOD;
}
void solve(int x)
{
    int num=upper_bound(has+2,has+tot,x)-has-1;
    int more=x-sum[num];
    int l,r;
    l=2+more/num;
    r=l+num-1;
    LL res;
    if(more%num)
        res=pro[r+1]*inv[l-1]%MOD*inv[r+1-more%num]%MOD;
    else
        res=pro[r]*inv[l-1]%MOD;
    printf("%lld\n",res);
}
int main()
{
    table();
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int x;
        scanf("%d",&x);
        if(x==1||x==2||x==3||x==4)
        {
            printf("%d\n",x);
            continue;
        }
        solve(x);
    }
    return 0;
}

 

HDU 5976 Detachment