HDU5976 LA7728 Detachment逆元+前缀和+二分

Posted 海岛Blog

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU5976 LA7728 Detachment逆元+前缀和+二分相关的知识,希望对你有一定的参考价值。

Detachment
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 6903 Accepted Submission(s): 1918

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 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

Source
2016ACM/ICPC亚洲区大连站-重现赛(感谢大连海事大学)

问题链接HDU5976 LA7728 Detachment
问题简述:给定一个数N(N<=109),将其拆成若干各不相同的数Ai,使得ΣAi=N,并且其乘积ΠAi为最大。
问题分析:一种是利用“前缀和”、“前缀积”和二分来解决;另外一种是用逆元来解决。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* HDU5976 LA7728 Detachment */

#include <bits/stdc++.h>

using namespace std;

typedef long long LL;
const int MOD = 1e9 + 7;
const int N = 1e6;
LL inv[N + 1], psum[N + 1];

void inverse()
{
    inv[1] = 1;
    psum[1] = 1;
    for (int i = 2; i < N; i++) {
        inv[i] = (MOD - MOD / i) * inv[MOD % i] % MOD;
        psum[i] = psum[i - 1] * i % MOD;
    }
}

int main()
{
    inverse();

    int t, x;
    scanf("%d", &t);
    while (t--) {
        LL ans;

        scanf("%d", &x);

        if (x <= 4) ans = x;
        else {
            int max = sqrt(x * 2 + 2.25) - 0.5;
            int res = x - (max + 2) * (max - 1) / 2;
            int base = res / (max - 1);
            res %= max - 1;
            ans = 1;
            if (res) {
                ans *= psum[max + base + 1] % MOD;
                ans *= inv[max + base + 1 - res];
            } else
                ans *= psum[max + base];
            ans %= MOD;
            while (base--) {
                ans *= inv[2 + base];
                ans %= MOD;
            }
        }

        printf("%lld\\n", ans);
    }

    return 0;
}

AC的C++语言程序如下:

/* HDU5976 LA7728 Detachment */

#include <bits/stdc++.h>

using namespace std;

typedef long long LL;
const int MOD = 1e9 + 7;
const int N = 1e5;
LL sum[N + 1], mul[N + 1];

void init()
{
    sum[1] = 0;
    mul[1] = 1;
    for (int i = 2; i <= N; i++) {
        sum[i] = sum[i - 1] + i;
        mul[i] = mul[i - 1] * i % MOD;
    }
}

/* 快速模幂 */
LL qpow(LL a, LL n, LL mod)
{
    LL res = 1;
    while (n) {
        if (n & 1) res = res * a % mod;
        a = a * a % mod;
        n >>= 1;
    }
    return res;
}

int main()
{
    init();

    int t, x;
    scanf("%d", &t);
    while (t--) {
        LL ans;

        scanf("%d", &x);

        if (x <= 4) ans = x;
        else {
            int k = upper_bound(sum + 1, sum + 1 + N, x) - sum - 1;
            int m = x - sum[k];
            if (k == m)
                ans =  mul[k] * qpow(2, MOD - 2, MOD) % MOD * (k + 2) % MOD;
            else
                ans = mul[k+1] * qpow(k - m + 1, MOD - 2, MOD) % MOD;
        }

        printf("%lld\\n", ans);
    }

    return 0;
}

以上是关于HDU5976 LA7728 Detachment逆元+前缀和+二分的主要内容,如果未能解决你的问题,请参考以下文章

HDU5976 Detachment

HDU 5976 Detachment

hdu 5976 Detachment

HDU - 5976 Detachment(逆元)

HDU 5976 Detachment 贪心 (2016ACM/ICPC亚洲区大连站)

HDU 5976 数学,逆元