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 4Sample Output
4题意:给一个数字x,求出一个无重复数字都为正数序列,序列的和等于x,求出所有序列中元素乘积最大的值。
贪心的策略:先把这个数字分成一个从2开始到y结束的集合,它们的和小于等于x,当小于x的时候要把差值 val 依次分到各个元素上,那么就从 y 开始到 2 ,依次每个数字加 1 直到 val 等于 0 ,过程中可能出现分配完一轮之后 val 还没有等于 0 ,那就继续从最大的开始分。分完的结果分为两种:
①2*3*...*(i-1)*(i+1)*...*k*(k+1)
②3*4*...*i*(i+1)*...*k*(k+2)
所以求解结果为 阶乘乘以缺失的数的逆元。
#include<stdio.h> #include<iostream> #include<algorithm> #include<string.h> #include<math.h> #include<string> #include<map> #include<vector> #define ll long long using namespace std; const double pi = acos(-1); const ll inf=1e18; const ll mod=1e9+7; const int maxn=1e6+10; ll invf[maxn],factor[maxn]; ll ex_gcd(ll a,ll b,ll &x,ll &y){ int ret,tmp; if(b==0){ x=1; y=0; return a; } ret = ex_gcd(b, a%b, x, y); tmp =x; x=y; y=tmp-a/b*y; return ret; } void init(){ factor[0]=1; for(int i=1;i<=1000000;i++){ factor[i]=factor[i-1]*i%mod; } ll x,y; invf[1000000]=ex_gcd(factor[1000000],mod,x,y); invf[1000000]=x; for(int i=1000000-1;i>=0;i--){ invf[i]=invf[i+1]*(i+1)%mod; } } bool judge(ll x,ll y){ if((y+1)>=(x*(x+1))/2)return true; return false; } int main(){ int t; scanf("%d",&t); init(); while(t--){ ll x; scanf("%lld",&x); if(x==1){ printf("1\n");continue; } ll l=2,r=x,ans=0; while(l<=r){ int mid=(l+r)/2; if(judge(mid,x)){l=mid+1;ans=mid;} else r=mid-1; } ll val = x - ((ans*(ans+1))/2-1); ll res; if(val==0){ res=factor[ans]; } else if(val==ans-1){ res=(factor[ans+1]*invf[2])%mod; } else if(val>ans-1){ res=((factor[ans+2]*invf[ans+1-val+ans])%mod)*((factor[ans-val+ans]*invf[2])%mod); } else if(val<ans-1){ res=((factor[ans+1]*invf[ans-val+1])%mod)*(factor[ans-val]%mod); } printf("%lld\n",res%mod); } }