hdu 5976 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
解题思路:
存在一个数可以将它分成任意数量数相加而得,求分解成的数的最大成绩,如何使其乘积最大?假设有一个数字n分为两个那么要使乘积最大就是n/2*n/2,分成三份就是n/3*n/3
*n/3,类推可得最大的肯定为2*2*2....,但题目要求数字不能相同那么只能分解为2+3+4+5...,但并不是每一个数都能分解成这样,不如当n=6,2+3=5,n多出了一,那么我们把这个1
加入到3上去那么就是2*4=8,最大积。当n不为等差数列和的时候我们只要把最接近n的数列和和n的差值,将差值转换为从后往前每个加一,其实也就是将数列的其中一部分统统后移一个位置
当n=23原先是2+3+4+5+6变成2+3+5+6+7,唯一的变化就是中间少了一个4,后面=多了一个7,因为这道题是多组输入需要预处理,所以只能先求前缀和和前缀积,然后找到需要减去的位置
用乘法逆元除去那个值,最后乘上下一位。最坑的是这里有一个特殊判断,因为数列是从2开始计算,当差值正好和最后一位相同时,如n=19,ans=19-(2+3+4+5)=5,但前面只有4个数,从
后往前加:3+4+5+6此时多了1,再把它从后往前加3+4+5+7.想到这些就很容易了
ps:这个题有毒,同一份代码第一次交tle,再交一次就ac了
实现代码:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int inf = 1000000007;
void exgcd(ll a,ll b,ll& d,ll& x,ll& y)
{
    if(!b){d=a;x=1;y=0;}
    else
    {
        exgcd(b,a%b,d,y,x);
        y-=x*(a/b);
    }
}

ll inv(ll a,ll n)
{
    ll d, x, y;
    exgcd(a,n,d,x,y);
    return (x+n)%n;
}
ll h[50000],j[50000];
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    ll num1 = 0,num2=1,cnt=1,i,t,n,sum,ans;
    h[0] = 1;j[0]=1;
    for(i=2;i<50000;i++){
        num1+=i;
        //cout<<num1<<endl;
        if(num1>inf)
            break;
        h[cnt++]=num1;
        num2=(num2*i)%inf;
        j[cnt-1]=num2;
    }
    cin>>t;
    while(t--){
        cin>>n;
        if(n==1){
            cout<<"1"<<endl;
            continue;
        }
        int k = lower_bound(h+1,h+cnt,n) - h;
        //cout<<"k: "<<k<<endl;
        if(h[k]==n){
            cout<<j[k]<<endl;
            continue;
        }
        else{
            ans = n-h[k-1];
            //cout<<"h[k-1] :"<<h[k-1]<<endl;
            //cout<<"j[k-1] :"<<j[k-1]<<endl;
            //cout<<"ans :"<<ans<<endl;
            //cout<<"k "<<k+2<<endl;
            if(ans==k)
            sum=((j[k-1]*inv(2,inf)%inf)*(k+2))%inf;
            else
            sum = ((j[k-1]*inv(k-ans+1,inf)%inf)*(k+1))%inf;
            cout<<sum<<endl;
        }
    }
    return 0;
}

 

posted @ 2017-07-28 11:50  冥想选手  阅读(366)  评论(1编辑  收藏  举报