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;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章