Detachment HDU - 5976(贪心+逆元+二分)

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

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

解题思路:

【题意】

给定一个自然数x,让你给出一种拆分方式x=a1+a2+...(ai≠aj),使得每个小部分的乘积s=a1*a2*...最大

扫描二维码关注公众号,回复: 2301943 查看本文章

【类型】
贪心构造
【分析】

此题关键在于得出如何能使乘积s最大

按照以往经验,必然是取一段连续自然数能够使得乘积最大,而这段连续自然数可从2开始(为啥不从1开始?从1开始还不如将这个1给这段连续自然数的最后一个数),于是我们可以得到形如2+3+4+...+k(k=2,3,...)的式子,而x是10^9内的任意整数,我们不可能恰好能够凑成连续自然数之和,可能会多出△x

而这个△x的值,我可以保证它的范围为0≤△x≤k,相信大于等于0还是好理解的,为什么会小于等于k呢?因为当它大于k时,原式不是可以增加一项?即2+3+4+...+k+(k+1)

那么多出来的△x怎么处理呢?显然是从后往前均摊给连续自然数中的(k-1)个数,为啥从后往前?因为若我们从前往后,总是会使连续自然数重复,不好处理

于是,在我们分配完△x之后,我们大致会得到下述两种式子:

①2*3*...*(i-1)*(i+1)*...*k*(k+1)

②3*4*...*i*(i+1)*...*k*(k+2)

显然,我们要计算此结果,可以借助阶乘,而阶乘中缺失的项,我们除掉就可以了,那么便会涉及除法取模,显然需要用到乘法逆元

做法讲解完毕,以下是为什么连续段乘积最大的大概证明:

简而言之,假如有一个数让你拆成两个要求积最大,肯定是拆成两个一样的,如果拆成n个,肯定就是拆成这个数/n,如果没说几个,肯定都拆成几个3就拆成几个三,剩下的拆成2.这个题要求不能一样的,所以肯定就是2,3,4这样排列了,从2开始让它变小,这样数量会最多,每次加一就是让他越来越接近。所以用前缀和记录,如果有剩余的话,肯定是从后往前逐个+1,而且剩余的那个数最多=2,3,4...最大的那个数,举个例子会发现有两种情况:1.比如2*3*4*5余下5,相等。最优就是3*4*5*7,就是每个数都加一遍,然后会剩下一个1,加给最后一个,总的来说就是 除以2,乘上t+2,(t是最后一个数的值);2.不相等,2,3,4,5,余2,最优就是2,3,5,6,就是用3的阶乘*6的阶乘除以4的阶乘,因为数量太多,不能一个一个乘,就用前缀积相除,数字太大,就要用逆元了。。然后用二分优化下。。

这题找到贪心策略后,要知道数据量大的话要用前缀积+逆元求,用前缀和优化找余数。。二分也是优化这个的

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
const int maxn = 1e5+5;
const int mod = 1e9+7;
LL mul[maxn],sum[maxn];
void init(){
	mul[1]=1;
	sum[1]=0;
	for(int i=2;i<maxn;i++){
		sum[i]=sum[i-1]+i;
		mul[i]=mul[i-1]*i%mod;
	}
}
LL inv(LL a,LL b){
	LL ans=1;
	while(b){
		if(b&1) ans=ans*a%mod;
		b>>=1;
		a=a*a%mod;
	}
	return ans%mod;
}
int main(){
	int T;
	scanf("%d",&T);
    init();
    while(T--){
    	LL x;
    	scanf("%lld",&x);
    	if(x==1){
    		printf("1\n");
    		continue;
		}
		int l=2,r=maxn,pos;
		while(l<=r){
			int mid=(l+r)>>1;
			if(sum[mid]<=x){
				pos=mid;
				l=mid+1;
			}
			else r=mid-1;
		}
		int num=x-sum[pos];
		LL ans=0;
		if(num==pos)
		   ans=(mul[pos]*inv(2,mod-2)%mod*(pos+2))%mod;
		else 
		   ans=(mul[pos+1]*inv(mul[pos+1-num],mod-2)%mod*mul[pos-num])%mod;
		printf("%lld\n",ans);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/islittlehappy/article/details/81148776