Hdu 5976Detachment(找规律+逆元 ,好题)*

Detachment
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 3925 Accepted Submission(s): 1134

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


题意:

 给你一个数,将它分成若干个不同的数的和(若干个也可以是一个),输出这若干个数的乘积的最大值。
分析:
找了下规律:
在这里插入图片描述
发现:像5,9,14,20…这类都是以 2 × 3 × . . . × ( n − 1 ) × n 2\times3\times...\times(n-1)\times n 2×3×...×(n1)×n形式(共n-1个数)(关于如何求n直接联立方程组求解)相乘(就称这些数为特征数)。且相乘的个数依次为2,3,4,5,等依次加1。而夹在它们中间的数的相乘的个数与离它最近的前面的特征数的相乘的个数相同(如5到9之间的6,7,8的相乘的个数与5相乘的个数是相同的)。设当前的数与它前面分割的数相差tmp,依次从这n-1个数最后一个数开始往前,依次每个数分一个1,倘若分到了第一个,tmp还有的剩,则再次重最后一个数开始分,直到tmp分完。
最后就是预处理一下,+逆元取模即可。

代码:
#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdio>

using namespace std;
#define ll long long 
const int N=1e5+10;
const int M=5e4+100;
const int mod=1e9+7;
ll sum[M];

void init()
{
	sum[0]=1;
	sum[1]=1;
	for(int i=2;i<M;i++)
		sum[i]=i*sum[i-1]%mod;
	// for(int i=1;i<20;i++)
		// cout<<sum[i]<<endl;
}

ll extend_gcd(ll a,ll b,ll &x, ll &y)
{
	if(b==0)
	{
		x=1;y=0;
		return a;
	}
	else
	{
		ll r=extend_gcd(b,a%b,y,x);
		y-=x*(a/b);
		return r;
	}
}

ll inv(ll a,ll m)
{
	ll x, y;
	ll gcd= extend_gcd(a,m,x,y);
	if(x<0)
	{
		x=(x%mod+mod)%mod;
	}
	return x;
}




int main()
{
	ll t,s;
	init();
	scanf("%lld",&t);
	while(t--)
	{
		scanf("%lld",&s);
		if(s<=4) printf("%d\n",s);
		else
		{
			ll n=(ll)((-1+sqrt(8*s+9.0))/2+0.5);
			while((2+n)*(n-1)/2>s) n--;//求n
			// cout<<n<<endl;
			int tmp=s-(2+n)*(n-1)/2;//表示差值,
			ll mi=2+tmp/(n-1);//表示相乘中的最小的值
			ll mx=tmp%(n-1)?n+tmp/(n-1)+1:n+tmp/(n-1);//表示相乘中的最大的值
			ll temp=2+tmp/(n-1)+n-1-tmp%(n-1);//表示的相乘中的数中间断的那个数
			ll ans=inv(sum[mi-1],mod)*sum[mx]%mod;
			if(temp<=mx)
				ans=( ans * inv(temp,mod) )%mod;
			printf("%lld\n",ans);
		}
		
		
	}
	return 0;
}








  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值