1. 程式人生 > >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): 192
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: a
1
,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= a
1
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 4
Sample Output 4
Source
Recommend wange2014   |   We have carefully selected several similar problems for you:  
6010
 6009 6008 6007 6006 


題目連結:

題目大意:

  給一個數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)


//
//by coolxxx
/*
#include<iostream>
#include<algorithm>
#include<string>
#include<iomanip>
#include<map>
#include<stack>
#include<queue>
#include<set>
#include<bitset>
#include<memory.h>
#include<time.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
//#include<stdbool.h>
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))
*/
#include<bits/stdc++.h>
#pragma comment(linker,"/STACK:1024000000,1024000000")
#define abs(a) ((a)>0?(a):(-(a)))
#define lowbit(a) (a&(-a))
#define sqr(a) ((a)*(a))
#define mem(a,b) memset(a,b,sizeof(a))
#define eps (1e-8)
#define J 10000
#define mod 1000000007
#define MAX 0x7f7f7f7f
#define PI 3.14159265358979323
#define N 45004
using namespace std;
typedef long long LL;
double anss;
LL aans;
int cas,cass;
int n,m,lll,ans;
LL a[N],ni[N];
LL mi(LL x,LL y)
{
	LL z=1;
	while(y)
	{
		if(y&1)z=(z*x)%mod;
		x=(x*x)%mod;
		y>>=1;
	}
	return z;
}
void init()
{
	int i;
	a[1]=1;
	ni[1]=1;
	for(i=2;i<N;i++)
	{
		a[i]=(a[i-1]*i)%mod;
		ni[i]=(-(mod/i)*a[mod%i])%mod;
	}
}
int main()
{
	#ifndef ONLINE_JUDGE
	freopen("1.txt","r",stdin);
//	freopen("2.txt","w",stdout);
	#endif
	int i,j,k;
	int x,y,z;
	init();
	for(scanf("%d",&cass);cass;cass--)
//	for(scanf("%d",&cas),cass=1;cass<=cas;cass++)
//	while(~scanf("%s",s))
//	while(~scanf("%d%d",&n,&m))
	{
		scanf("%d",&n);
		if(n<5)
		{
			printf("%d\n",n);
			continue;
		}
		m=n+n+2;
		LL l,r,mid;
		l=2;r=45000;
		while(l<r)
		{
			mid=(l+r+1)>>1;
			if(mid*mid+mid<=m)l=mid;
			else r=mid-1;
		}
		m-=l*l+l;
		m/=2;
		if(m==l)
		{
			aans=a[l]*(l+2)%mod*mi(2,mod-2)%mod;
		}
		else
		{
			x=l+1-m;
			aans=a[l+1]*mi(x,mod-2)%mod;
		}
		printf("%lld\n",aans);
	}
	return 0;
}
/*
//

//
*/