传送门:HDU 5976
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 109+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≤106, 1≤x≤109
Output
Maximum s you can get modulo 109+7. Note that we wants to be greatest product before modulo 109+7.
Sample Input
1
4
Sample Output
4
题意:
给定一个自然数x,让你给出一种拆分方式x=a1+a2+…(ai≠aj),使得每个小部分的乘积s=a1a2…最大;
题解:
数论的思想。一般要使乘积最大,那么先拆成两个不同数时,这两个数一定是接近n/2,然后每一个数又可以拆成两个数。但要这些数都不相同,所以最好的情况肯定是n等于从2开始一直到某个数k的连续和,如9=2+3+4,这样肯定是最优的,那么n-sum(2+…+k)怎么处理呢(多的数)?应该从后往前分,因为从前往后分会有重复,这里要二分来找 。
找到贪心策略后,因为数据量大,所以要用前缀积+逆元求,用前缀和优化找余数。
具体题解可见:
数论:正整数分解使得乘积最大
AC代码:
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
const ll mod=1e9+7;
const int N=1e5+5;
int t;
ll x;
ll sum[N],mul[N];
void init()
{
mul[1]=1;
sum[1]=0;
for(int i=2;i<N;i++)
{
sum[i]=sum[i-1]+i;//前缀和
mul[i]=(mul[i-1]*i)%mod;//前缀积
}
}
ll inv(ll a,ll n)//快速幂求逆元
{
ll ans=1,base=a;
while(n)
{
if(n&1)
{
ans=ans*base%mod;
}
base=base*base%mod;
n>>=1;
}
return ans;
}
int main()
{
init();
scanf("%d",&t);
while(t--)
{
scanf("%I64d",&x);
if(x==1)
{
printf("1\n");
continue;
}
ll l=1,h=N,p;
while(l<=h)//二分找最接近x的前缀和
{
ll mid=(l+h)>>1;
if(sum[mid]<=x)
{
p=mid;
l=mid+1;
}
else
{
h=mid-1;
}
}
ll k=x-sum[p];//多余的数
ll temp=0;
if(k==p)//两种情况
{
temp=mul[p]*(p+2)%mod*inv(2,mod-2)%mod;
}
else
{
temp=mul[p+1]*inv(mul[p+1-k],mod-2)%mod*mul[p-k]%mod;
}
printf("%I64d\n",temp);
}
return 0;
}