HDU 5976 Detachment

Detachment

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


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

题意:给你一个数,将这个数分成互不相等的若干个数是的这些数的乘积最大,求最大乘积。

对于一个数,最有情况是分成2,3,4,5...这种答案最优(不知道怎么证明)。这是就会有三种情况。

1:x=sum[n],那么ans=n!。

2:x+1=sum[n],这时去掉2,n加上1后得到的乘积最大。ans=n!/(2*n).

3:x+k=sum[n],k<n,k一定会抵消,ans=n!/(s[n]-x)。

计算式会用到乘法,需要使用逆元。

#include<stdio.h>  
#include<algorithm>  
#include<string.h>  
using namespace std;
#define ll long long
const int maxm = 100005;
const int mod = 1e9 + 7;
ll s[maxm], c[maxm];
void init()
{
	s[2] = 2, c[2] = 2;
	for (int i = 3;i <= 100000;i++)
	{
		s[i] = s[i - 1] + i;
		c[i] = (c[i - 1] * i) % mod;
	}
}
int e_gcd(int a, int b, int &x, int &y)
{
	if (b == 0)
	{
		x = 1;
		y = 0;
		return a;
	}
	int ans = e_gcd(b, a%b, x, y);
	int temp = x;
	x = y;
	y = temp - (a / b)*y;
	return ans;
}
int cal(int a, int m)
{
	int x, y, ans;
	int gcd = e_gcd(a, m, x, y);
	if (gcd != 1) return -1;
	m = abs(m);
	ans = x % m;
	if (ans <= 0) ans += m;
	return ans;
}
int main()
{
	int ans, t, a, m, x, n;
	init();
	scanf("%d", &t);
	while (t--)
	{
		scanf("%d", &x);
		if (x == 1)
		{
			printf("1\n");
			continue;
		}
		n = lower_bound(s + 2, s + 2 + 100000, x) - s;
		//printf("%d %lld\n", n,s[n]);
		if (s[n] == x)
			printf("%lld\n", c[n]);
		else
		{
			if (s[n] - x == 1)
			{
				ans = cal(2 * n, mod);
				printf("%lld\n", (c[n + 1] * ans)%mod);
			}
			else
			{
				ans = cal(s[n] - x, mod);
				printf("%lld\n", (c[n] * ans)%mod);
			}
		}
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值