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 ( ai≠ajai≠aj when i≠j). 
2.Make this multidimensional space size s as large as possible (s= a1∗a2a1∗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) 

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,求出所有序列中元素乘积最大的值。

贪心的策略:先把这个数字分成一个从2开始到y结束的集合,它们的和小于等于x,当小于x的时候要把差值 val 依次分到各个元素上,那么就从 y 开始到 2 ,依次每个数字加 1 直到 val 等于 0 ,过程中可能出现分配完一轮之后 val 还没有等于 0 ,那就继续从最大的开始分。分完的结果分为两种:

①2*3*...*(i-1)*(i+1)*...*k*(k+1)

②3*4*...*i*(i+1)*...*k*(k+2)

所以求解结果为  阶乘乘以缺失的数的逆元。

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<math.h>
#include<string>
#include<map>
#include<vector>
#define ll long long
using namespace std;
const double pi = acos(-1);
const ll inf=1e18;
const ll mod=1e9+7;
const int maxn=1e6+10;
ll invf[maxn],factor[maxn];
ll ex_gcd(ll a,ll b,ll &x,ll &y){
    int ret,tmp;
    if(b==0){
        x=1;
        y=0;
        return a;
    }
    ret = ex_gcd(b, a%b, x, y);
    tmp =x;
    x=y;
    y=tmp-a/b*y;
    return ret;
}
void init(){
    factor[0]=1;
    for(int i=1;i<=1000000;i++){
        factor[i]=factor[i-1]*i%mod;
    }
    ll x,y;
    invf[1000000]=ex_gcd(factor[1000000],mod,x,y);
    invf[1000000]=x;
    for(int i=1000000-1;i>=0;i--){
        invf[i]=invf[i+1]*(i+1)%mod;
    }
}
bool judge(ll x,ll y){
    if((y+1)>=(x*(x+1))/2)return true;
    return false;
}
int main(){
    int t;
    scanf("%d",&t);
    init();
    while(t--){
        ll x;
        scanf("%lld",&x);
        if(x==1){
            printf("1\n");continue;
        }
        ll l=2,r=x,ans=0;
        while(l<=r){
             int mid=(l+r)/2;
             if(judge(mid,x)){l=mid+1;ans=mid;}
             else r=mid-1;
            }
        ll val = x - ((ans*(ans+1))/2-1);
        ll res;
        if(val==0){
            res=factor[ans];
        }
        else if(val==ans-1){
            res=(factor[ans+1]*invf[2])%mod;
        }
        else if(val>ans-1){
            res=((factor[ans+2]*invf[ans+1-val+ans])%mod)*((factor[ans-val+ans]*invf[2])%mod);
        }
        else if(val<ans-1){
            res=((factor[ans+1]*invf[ans-val+1])%mod)*(factor[ans-val]%mod);
        }
        printf("%lld\n",res%mod);
    }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值