HDU 5976 数学

Detachment

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


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
思路:拆成连续的数字,剩余的从后往前加在每个数字上。

代码:

 1 #include<bits/stdc++.h>
 2 //#include<regex>
 3 #define db double
 4 #include<vector>
 5 #define ll long long
 6 #define vec vector<ll>
 7 #define Mt  vector<vec>
 8 #define ci(x) scanf("%d",&x)
 9 #define cd(x) scanf("%lf",&x)
10 #define cl(x) scanf("%lld",&x)
11 #define pi(x) printf("%d
",x)
12 #define pd(x) printf("%f
",x)
13 #define pl(x) printf("%lld
",x)
14 #define MP make_pair
15 #define PB push_back
16 #define inf 0x3f3f3f3f3f3f3f3f
17 #define fr(i,a,b) for(int i=a;i<=b;i++)
18 const int N=1e5+5;
19 const int mod=1e9+7;
20 const int MOD=mod-1;
21 const db  eps=1e-18;
22 const db  PI=acos(-1.0);
23 using namespace std;
24 ll x;
25 ll F[N],inv[N],Finv[N];
26 bool cal(ll n){
27     if((n*n+n-2)/2<=x) return 1;
28     return 0;
29 }
30 
31 void init(){
32     inv[1] = 1;
33     for(int i = 2; i < 45000; i ++){
34         inv[i] = (mod - mod / i) * 1ll * inv[mod % i] % mod;//单个逆元
35     }
36     F[1] = Finv[1] = 1;
37     for(int i = 2; i < 45000; i ++){
38         F[i] = F[i-1] * 1ll * i % mod;//阶乘
39         Finv[i] = Finv[i-1] * 1ll* inv[i] % mod;//逆元阶乘
40     }
41 }
42 int main(){
43 //freopen("data.in","r",stdin);
44 //freopen("data.out","w",stdout);
45     int t;
46     ci(t);
47     init();
48     while(t--)
49     {
50         cl(x);
51         if(x<5) {pl(x);continue;}
52         ll l=2,r=45000,sum;
53         while(l<r){
54             ll mid=l+(r-l+1)/2;
55             if(cal(mid)) l=mid;
56             else r=mid-1;
57         }
58         ll ans=x-(l*l+l-2)/2;
59         if(ans==l) sum=F[l+2]*inv[2]%mod*inv[l+1]%mod;
60         else sum=F[l+1]*Finv[l-ans+1]%mod*F[l-ans]%mod;
61         pl(sum);
62     }
63     return 0;
64 }
原文地址:https://www.cnblogs.com/mj-liylho/p/7625949.html