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

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 }
				
时间: 2024-10-14 04:42:54

hdoj 5976 Detachment(逆元)的相关文章

HDU - 5976 Detachment(逆元)

题意:将一个数x拆成a1+a2+a3+……,ai不等于aj,求最大的a1*a2*a3*……. 分析: 1.预处理前缀和前缀积,因为拆成1对乘积没有贡献,所以从2开始拆起. 2.找到一个id,使得2+3+4+……+id - 1(sum[id-1]) < x < 2+3+4+……+id(sum[id).(二分找即可) 则rest = x - sum[id - 1].将rest分配给2+3+4+id-1中的某个数. 3.在保证数字不重复的前提下,分配给的那个数越小越好,证明见4. 因此,应该分配给的

HDU 5976 Detachment 【贪心】 (2016ACM/ICPC亚洲区大连站)

Detachment Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 570    Accepted Submission(s): 192 Problem Description In a highly developed alien society, the habitats are almost infinite dimensiona

HDU 5976 Detachment

Detachment 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 li

HDU 5976 数学,逆元

1.HDU 5976 Detachment 2.题意:给一个正整数x,把x拆分成多个正整数的和,这些数不能有重复,要使这些数的积尽可能的大,输出积. 3.总结:首先我们要把数拆得尽可能小,这样积才会更大(当然不能拆1).所以容易想到是拆成2+3+...+n+s=x,先求出n即2+3+...+n<x<2+3+...+n+(n+1),然后将某个数向右平移s个单位变为n+1即可.注意:(1)预处理出前缀和,前缀积.(2)将某个数移到n+1,要除这个数再乘n+1,这里要用逆元,也要预处理出来. #in

HDU 1576 -- A/B (总结乘法逆元的几种求法)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1576 A/B Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 7264    Accepted Submission(s): 5774 Problem Description 要求(A/B)%9973,但由于A很大,我们只给出n(n=A%99

HDOJ 4869 Turn the pokers

最后的结果中正面向上的奇偶性是一定的,计算出正面向上的范围low,up 结果即为 C(m,low)+ C(m,low+2) +.... + C(m,up) ,用逆元取模 Turn the pokers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 828    Accepted Submission(s): 302 Problem D

【HDOJ】4305 Lightning

1. 题目描述当一个结点lightning后,可以向其周围距离小于等于R的结点传播lightning.然后以该结点为中心继续传播.以此类推,问最终形成的树形结构有多少个. 2. 基本思路生成树级数模板题目.Matrix-Tree定理(Kirchhoff矩阵-树定理):1) G的度数矩阵D[G]是一个n*n的矩阵,并且满足:当i≠j时,dij=0:当i=j时,dij等于vi的度数:2) G的邻接矩阵A[G]也是一个n*n的矩阵, 并且满足:如果vi.vj之间有边直接相连,则aij=1,否则为0.定

数论 HDOJ 5407 CRB and Candies

题目传送门 题意:求LCM (C(N,0),C(N,1),...,C(N,N)),LCM是最小公倍数的意思,C函数是组合数. 分析:先上出题人的解题报告 好吧,数论一点都不懂,只明白f (n + 1)意思是前n+1个数的最小公倍数,求法解释参考HDOJ 1019,2028 这个结论暂时不知道怎么推出来的,那么就是剩下1/(n+1) 逆元的求法了 代码: /************************************************ * Author :Running_Time

HDU5976 Detachment

1 /* 2 HDU5976 Detachment 3 http://acm.hdu.edu.cn/showproblem.php?pid=5976 4 数论 等差数列 5 * 6 * 7 */ 8 #include <cstdio> 9 #include <algorithm> 10 #include <vector> 11 using namespace std; 12 const int mod=1000000007; 13 const int Nmax=5000