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拆成多个不同的数相加,如X = a1 + a2 + a3 + … + an
然后问你S = a1*a2*a3* .. *an,问你S最大是多少。

思路:这里有一个结论,对于一个数把他拆成从2开始的连续的数,最后得到的成绩是最大的,所以我们可以先预处理一下前缀和包括乘积后的结果。不过对于有些数无法用多个连续的数表示,比如对于11可以找到2+3+4,此时还余2,要构成5还差3,所以我们把3变成5即2 + 4 + 5.
但有种特殊情况,比如2+3+.. + l,此时还余个l,那么我们找到的是1,到哪很显然序列中是没有1的,所以把2变成l+2。因为要在取余后的结果除去一个数,再乘上一个数,所以这里要用到乘法逆元。

代码如下

#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
const ll Mod = 1e9+7;
const int MAX = 50000;
ll sum[MAX+10],f[MAX+10];
void init(){//预处理前缀和
    f[1] = 1;
    for(int i=2;i<=MAX;++i){
        sum[i] = sum[i-1] + i;
        f[i] = (f[i-1]*i) % Mod;
    }
}
ll Quick_pow(ll x,ll n){//快速幂,用来求逆元
    ll res = 1;
    while(n > 0){
        if(n&1)
            res = res%Mod*x%Mod;
        x = x%Mod*x%Mod;
        n >>= 1;
    }
    return res;
}
int main(void){
    int T;
    init();
    scanf("%d",&T);
    while(T--){
        ll x;
        scanf("%lld",&x);
        if(x <= 4){//对于小于5的数都是他本身
            printf("%lld\n",x);
            continue;
        }
        ll l = 0,r = MAX;//二分查找符合的l
        while(r - l > 1){
            ll mid = (l+r)/2;
            if(sum[mid] > x){
                r = mid;
            }
            else{
                l = mid;
            }
        }
     //   printf("l = %lld\n",l);
        ll left = x - sum[l];//余下的数
        ll res = f[l];
        if(left == l){//差的数为1时
            res = res*Quick_pow(2,Mod-2)%Mod*(l+2)%Mod;
        }
        else{
            res = res*Quick_pow(l+1-left,Mod-2)%Mod*(l+1)%Mod;
        }
        printf("%lld\n",res);
    }



    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值