roblem 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 ( ai≠aj 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
Problem Idea
解题思路:
【题意】
给定一个自然数x,让你给出一种拆分方式x=a1+a2+…(ai≠aj),使得每个小部分的乘积s=a1a2…最大
sn=2+3+4+…x
#include<cstdio>
using namespace std;
#define ll long long int
const ll mod = 1e9 + 7;
const ll maxn = 100010;
ll n;
ll temp,ant;
bool jud(ll x)
{
return ((x*(x+1)/2-1)>=n);//判断此时sn是否超出n
}
ll pow_(ll x,ll n)//因为这里mod为质数,偷懒的求逆元方法
{
ll ans = 1;
x=x%mod;
while(n)
{
if(n&1)
ans=ans*x%mod;
x=x*x%mod;
n>>=1;
}
return ans;
}
ll jie[maxn];
int main()
{
jie[0]=1;
for(int i=1;i<maxn;i++)
jie[i]=(jie[i-1]*i)%mod;//阶乘打表
int t;
scanf("%d",&t);
while(t--)
{
scanf("%lld",&n);
if(n<=4)//前4个直接输出
{
printf("%lld\n",n);
continue;
}
ll l=0,r=n,mid;
while(l<r)//二分寻找最合适的x
{
mid = (l+r)>>1;
if(jud(mid))
r=mid;
else
l=mid+1;
}
l=r;
ant = 1;
temp = (r*(r+1)/2-1);//此时的sn
if(temp==n)//如果刚好满足
{
printf("%lld\n",jie[l]);
continue;
}
l--;//!!!
if(temp == n+1)//如果超了1
{
ant=jie[l];
ant=ant*pow_(2,mod-2)%mod;//我们舍弃 2最后加上 x+1 就好了
ant=(ant*(l+2))%mod;
printf("%lld\n",ant);
}
else
{
ant=jie[l+1];
ant=ant*pow_(temp-n,mod-2)%mod;// 舍弃掉 k ,和x留下 x+k就解决
printf("%lld\n",ant);
}
}
return 0;
}