HDU 5976 逆元

Detachment

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)


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


题意:给你一个x,找出若干个不同的整数使得和为x,积要最大,然后求出积mod后的值


题解:对于一个数,拆的因子越小,积越大,因为当a+b=n时,根据二次函数性质知道,当b=a=n/2时,乘积最大,现在不能相等,我们只要靠近即可

所以我们可以求2+3+...+n+s=x,先求出n即2+3+...+n<x<2+3+...+n+(n+1),然后将某个数向右平移s个单位变为n+1即可。

处理出前缀积mod即可

ps:因为要把某个数变为另一个数时要用到除法,所以要用逆元。不要+1,因为1对乘积没影响,而且占用和,所以没用。


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define mod 1000000007
typedef long long ll;
ll num[100005],top=0,ans[100005],ni[100005];
void init(){
	num[++top]=1;
	ans[top]=1;
	ll i,now=0,mul=1;
	ni[top]=1;
	for(i=2;now<=1000000000;i++){
		now+=i;
		num[++top]=now;//前缀积
		mul=mul*i%mod;
		ans[top]=mul;
		ni[i]=(mod-mod/i)*ni[mod%i]%mod;//逆元
	}
}
int main(){
	init();
	ll t;
	scanf("%lld",&t);
	while(t--){
		ll x;
		scanf("%lld",&x);
		ll s=lower_bound(num+1,num+1+top,x)-num;//找n
		if(num[s]==x){
			printf("%lld\n",ans[s]);
		}
		else{
			s--;
			ll need=x-num[s],la=ans[s];
			if(need==s){//如果s==n  那就把2拿过来,因为没有1
				la=la*ni[2]%mod;
				la=la*(s+2)%mod;
			}
			else{
				la=la*ni[s+1-need]%mod;
				la=la*(s+1)%mod;
			}
			printf("%lld\n",la);
		}
	}
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值