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

处理出前缀积mod即可

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


#include
#include
#include
#include
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;
}


你可能感兴趣的:(数论,贪心)