HDU - 5976 Detachment 贪心

19 篇文章 0 订阅
6 篇文章 0 订阅

Detachment

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


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
 

Source


刚开始读错题意了。。。。

好吧,读对也过不了=.=

贪心

构造2,3,4,5,6一直到总和不超过x

举个例更好说

x=30

那么就是2,3,4,5,6,5

最后一个5重复了,是用前面的数去补他还是,用他去补前面的数,举了几个例子是往前补更优,证明不详。。。。。。

所以就这样贪心的构造,

求乘积时可以先处理出来阶乘,然后取模除一除

#include<bits/stdc++.h>
using namespace std;
long long mo=1e9+7;
long long f[1000005];
long long poww(long long a,long long x)
{
	long long ans=1,t=a;
	while(x)
	{
		if(x&1)
		ans*=t;
		t*=t;
		ans%=mo;
		t%=mo;
		x>>=1;
	}
	return ans;
}
void init()
{
	f[0]=1;
	for(int i=1;i<=1000000;i++)
	{
		f[i]=(f[i-1]*i)%mo;
	}
	return ;
}
long long solve(long long x,long long y)
{
	return (x*poww(y,mo-2))%mo;
}
int main()
{
	init();
	int T;
	long long x;
	scanf("%d",&T);
	while(T--)
	{
		scanf("%lld",&x);
		if(x==1)
		{
			printf("1\n");
			continue;
		}
		long long le=0,ri=x,mid,res,sum,tt;
		while(le<=ri)
		{
			mid=(le+ri)>>1;
			if((mid+2)*(mid-1)/2<=x)
			{
				res=mid;
				le=mid+1;
			}
			else
			ri=mid-1;
		}
	//	cout<<res<<endl;
		sum=(res+2)*(res-1)/2;
		tt=x-sum;
		if(tt==res)
		{
			printf("%lld\n",(solve(f[res],2)*(res+2))%mo);
		}
		else
		{
		//	cout<<res<<" "<<tt<<endl;
			printf("%lld\n",(solve(f[res+1],res-tt+1)%mo));
		}
	}
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值