首页 > 代码库 > HDU 5976 Detachment 【贪心】 (2016ACM/ICPC亚洲区大连站)
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): 192Problem DescriptionIn 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)
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^9
OutputMaximum s you can get modulo 10^9+7. Note that we wants to be greatest product before modulo 10^9+7.
Sample Input1 4
Sample Output4
Source2016ACM/ICPC亚洲区大连站-重现赛(感谢大连海事大学)
Recommendwange2014 | We have carefully selected several similar problems for you: 6010 6009 6008 6007 6006
Statistic | Submit | Discuss | Note
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=5976
题目大意:
给一个数N(N<=109),让你把它拆成若干各不相同的数Ai,ΣAi=N,要求ΠAi(累乘)最大。
题目思路:
【贪心】
首先肯定要把位数拆的尽量多,手写了20以内的拆法。
发现以2为首相的递增序列累乘最大,所以我的想法就是把N拆成2+3+...+x<=n,
先找到x,之后算一下n还多了多少,就把后面依次+1,变成2+3+...+y+(y+2)+(y+3)+...+(x+1)。
这时候它们的累乘是最大的。
(特殊情况是从2到x都加1之后还剩余1,这时候把最后一项再加1,变成3+4+...+x+(x+2)
1 // 2 //by coolxxx 3 /* 4 #include<iostream> 5 #include<algorithm> 6 #include<string> 7 #include<iomanip> 8 #include<map> 9 #include<stack> 10 #include<queue> 11 #include<set> 12 #include<bitset> 13 #include<memory.h> 14 #include<time.h> 15 #include<stdio.h> 16 #include<stdlib.h> 17 #include<string.h> 18 #include<math.h> 19 //#include<stdbool.h> 20 #define min(a,b) ((a)<(b)?(a):(b)) 21 #define max(a,b) ((a)>(b)?(a):(b)) 22 #define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b)) 23 */ 24 #include<bits/stdc++.h> 25 #pragma comment(linker,"/STACK:1024000000,1024000000") 26 #define abs(a) ((a)>0?(a):(-(a))) 27 #define lowbit(a) (a&(-a)) 28 #define sqr(a) ((a)*(a)) 29 #define mem(a,b) memset(a,b,sizeof(a)) 30 #define eps (1e-8) 31 #define J 10000 32 #define mod 1000000007 33 #define MAX 0x7f7f7f7f 34 #define PI 3.14159265358979323 35 #define N 45004 36 using namespace std; 37 typedef long long LL; 38 double anss; 39 LL aans; 40 int cas,cass; 41 int n,m,lll,ans; 42 LL a[N],ni[N]; 43 LL mi(LL x,LL y) 44 { 45 LL z=1; 46 while(y) 47 { 48 if(y&1)z=(z*x)%mod; 49 x=(x*x)%mod; 50 y>>=1; 51 } 52 return z; 53 } 54 void init() 55 { 56 int i; 57 a[1]=1; 58 ni[1]=1; 59 for(i=2;i<N;i++) 60 { 61 a[i]=(a[i-1]*i)%mod; 62 ni[i]=(-(mod/i)*a[mod%i])%mod; 63 } 64 } 65 int main() 66 { 67 #ifndef ONLINE_JUDGE 68 freopen("1.txt","r",stdin); 69 // freopen("2.txt","w",stdout); 70 #endif 71 int i,j,k; 72 int x,y,z; 73 init(); 74 for(scanf("%d",&cass);cass;cass--) 75 // for(scanf("%d",&cas),cass=1;cass<=cas;cass++) 76 // while(~scanf("%s",s)) 77 // while(~scanf("%d%d",&n,&m)) 78 { 79 scanf("%d",&n); 80 if(n<5) 81 { 82 printf("%d\n",n); 83 continue; 84 } 85 m=n+n+2; 86 LL l,r,mid; 87 l=2;r=45000; 88 while(l<r) 89 { 90 mid=(l+r+1)>>1; 91 if(mid*mid+mid<=m)l=mid; 92 else r=mid-1; 93 } 94 m-=l*l+l; 95 m/=2; 96 if(m==l) 97 { 98 aans=a[l]*(l+2)%mod*mi(2,mod-2)%mod; 99 } 100 else 101 { 102 x=l+1-m; 103 aans=a[l+1]*mi(x,mod-2)%mod; 104 } 105 printf("%lld\n",aans); 106 } 107 return 0; 108 } 109 /* 110 // 111 112 // 113 */
HDU 5976 Detachment 【贪心】 (2016ACM/ICPC亚洲区大连站)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。