HDU5976 Detachment (预处理前缀和+贪心+逆元)

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,a2a1,a2 , … (x= a1+a2a1+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 ( aiajai≠aj when i≠j).
2.Make this multidimensional space size s as large as possible (s= a1a2a1∗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)
InputThe 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^9OutputMaximum 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=a1+a2+...(ai≠aj),使得每个小部分的乘积s=a1*a2*...最大

 

 

贴两个比较好的分析:

http://m.blog.csdn.net/u014665013/article/details/71158093

http://www.cnblogs.com/WHLdbk/p/6051706.html

 

代码如下:

#include <map>
#include <set>
#include <cmath>
#include <vector>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
const int MAXN=45000;
const int MOD=1e9+7;
ll sum[MAXN];
ll f[MAXN];
ll inv[MAXN];
void init()
{
   sum[1]=0,f[1]=1,inv[1]=1;
   for(int i=2;i<=45000;i++)
   {
        inv[i]=(MOD-MOD/i)*inv[MOD%i]%MOD;
        f[i]=(f[i-1]*i)%MOD;
        sum[i]=sum[i-1]+i;
   }
}
int main()
{
    ll n,ans,k;
    init();
    ll t;
    scanf("%lld",&t);
    while(t--)
    {
    scanf("%lld",&n);

        if(n<5){printf("%lld\n",n);continue;};
         ll l=0;
         ll r=45001;
         ll mid;
          while(l+1<r)
          {
            mid=(l+r)/2;
            if(sum[mid]>n)
             r=mid;
            else
            l=mid;
          }
       k=n-sum[l];
       if(2+k>l)ans=(f[l]*inv[2]%MOD)*(2+k)%MOD;
       else  ans=(f[l]*inv[l+1-k]%MOD)*(l+1)%MOD;
       printf("%lld\n",(ans+MOD)%MOD);
    }
    return 0;
}

 

posted @ 2017-08-29 21:14  hinata_hajime  阅读(540)  评论(0编辑  收藏  举报