# Detachment

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

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

【题意】

【类型】

【分析】

①2*3*...*(i-1)*(i+1)*...*k*(k+1)

②3*4*...*i*(i+1)*...*k*(k+2)

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 1e5 + 5;
const int Mod = 1e9 + 7;
typedef long long ll;
ll mul[maxn], sum[maxn];// inv[maxn];
void init()
{
mul[1] = 1;
sum[1] = 0;  //这里让sum【1】 = 0，是为了下面二分。让最大是几，下表就是几
{
sum[i] = sum[i-1] + i;
mul[i] = (i*mul[i-1]) % Mod;
//  inv[i] = (Mod-Mod/i) * inv[Mod%i] % Mod;  //线性求逆元。。
}
}
ll inv(ll a, int b)  //用费马小定理求逆元
ll ans = 1;
while(b)
{
if(b&1) ans = (ans*a) % Mod;
a = (a*a) % Mod;
b >>= 1;
}
return ans;
}
int main()
{
int t, x;
init();
scanf("%d", &t);
while(t--)
{
scanf("%d", &x);
if(x == 1)  //不要忘记这里。。。
{
puts("1");
continue;
}
int l = 2, r = maxn, mid, p;
while(l <= r)
{
//  cout << l << ' ' << r << endl;
mid = (l+r)/2;
if(sum[mid] <= x) p = mid, l = mid + 1;  //二分快速找到可以到哪个数
else r = mid - 1;
}
// cout << p << endl;
int num = x - sum[p];  //求余数
ll ans = 0;
if(num == p)  //两种情况
ans = (mul[p]*inv(2,Mod-2)%Mod*(p+2))%Mod;
else
ans = (mul[p+1]*inv(mul[p+1-num], Mod-2)%Mod*mul[p-num])%Mod; //具体看上面红字
printf("%I64d\n", ans);
}
return 0;
}