首页 > 代码库 > hdoj 5976 Detachment(逆元)

hdoj 5976 Detachment(逆元)

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 ( aiajai≠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) 

InputThe 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^9OutputMaximum 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

 1 /*
 2 题意:给你一个x,找出若干个不同的整数使得和为x,积要最大,
 3 然后求出积mod后的值
 4 题解:对于一个数,拆的因子越小,积越大,因为当a+b=n时
 5 根据二次函数性质知道,当b=a=n/2时,乘积最大,现在不能相等
 6 我们只要靠近即可
 7 所以我们可以求2+3+...+n+s=x,先求出n即2+3+...+n<x<2+3+...+n+(n+1)
 8 然后将某个数向右平移s个单位变为n+1即可
 9 处理出前缀积mod即可
10 ps:因为要把某个数变为另一个数时要用到除法,所以要用逆元
11 不要+1,因为1对乘积没影响,而且占用和,所以没用
12 */
13 #include<iostream>
14 #include<cstdio>
15 #include<cstring>
16 #include<algorithm>
17 using namespace std;
18 #define mod 1000000007
19 typedef long long ll;
20 ll num[100005],top=0,ans[100005],ni[100005];
21 void init(){
22     num[++top]=1;
23     ans[top]=1;
24     ll i,now=0,mul=1;
25     ni[top]=1;
26     for(i=2;now<=1000000000;i++){
27         now+=i;
28         num[++top]=now;//前缀积
29         mul=mul*i%mod;
30         ans[top]=mul;
31         ni[i]=(mod-mod/i)*ni[mod%i]%mod;//逆元
32     }
33 }
34 int main(){
35     init();
36     ll t;
37     scanf("%lld",&t);
38     while(t--){
39         ll x;
40         scanf("%lld",&x);
41         ll s=lower_bound(num+1,num+1+top,x)-num;//找n
42         if(num[s]==x){
43             printf("%lld\n",ans[s]);
44         }
45         else{
46             s--;
47             ll need=x-num[s],la=ans[s];
48             if(need==s){//如果s==n  那就把2拿过来,因为没有1
49                 la=la*ni[2]%mod;
50                 la=la*(s+2)%mod;
51             }
52             else{
53                 la=la*ni[s+1-need]%mod;
54                 la=la*(s+1)%mod;
55             }
56             printf("%lld\n",la);
57         }
58     }
59     return 0;
60 }

 



hdoj 5976 Detachment(逆元)