HDU 5976 Detachment (16 大连[铜],预处理 + 前缀积 + 费马小定理 + 贪心 + 二分)

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

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亚洲区大连站-重现赛(感谢大连海事大学)

题意: 给一个数 n n n,要求将 n n n 分成若干个数的和,且这若干个数不重复。

思路: 不是很明白题解的优化,所以自己写一个不那么优的题解,时间1500ms。

首先容易想到,分 1 1 1 出来是完全没有贡献的。那么分的数从 2 2 2 开始。

  • n n n 2 2 2 x x x 的和,那么分成 2 , 3 , 4 … x 就是最优的。
  • 若不是,那么可以先二分出一个最大 x,使得 ∑ i = 2 x &lt; n \sum_{i = 2}^x &lt; n i=2x<n,然后将 n − ∑ i = 2 x n - \sum_{i = 2}^x ni=2x 从后面前依次分到每个数,可得到每个数是什么。
  • 如 n = 8, 有 x = 3,那么初始时为序列 2 , 3 ,剩余lef = 8 - (2 + 3) = 3,将 3 依次分到2 , 3 上。有 3, 4 此时 lef = 1,然后有 3 , 5 ,lef = 0,故最终序列就是 3 , 5最优
  • 利用前缀积与逆元 求解答案。

Code:

#include<bits/stdc++.h>
#define debug(x) cout << "[" << #x <<": " << (x) <<"]"<< endl
#define pii pair<int,int>
#define clr(a,b) memset((a),b,sizeof(a))
#define rep(i,a,b) for(int i = a;i < b;i ++)
#define pb push_back
#define MP make_pair
#define LL long long
#define ull unsigned LL
#define ls i << 1
#define rs (i << 1) + 1
#define fi first
#define se second
#define ptch putchar
#define CLR(a) while(!(a).empty()) a.pop()

using namespace std;
inline LL read() {
    LL s = 0,w = 1;
    char ch = getchar();
    while(!isdigit(ch)) {
        if(ch == '-') w = -1;
        ch = getchar();
    }
    while(isdigit(ch))
        s = s * 10 + ch - '0',ch = getchar();
    return s * w;
}
inline void write(LL x) {
    if(x < 0)
        putchar('-'), x = -x;
    if(x > 9)
        write(x / 10);
    putchar(x % 10 + '0');
}
#ifndef ONLINE_JUDGE
    clock_t prostart = clock();
#endif

const LL mod = 1e9 + 7;
const int maxn = 1e5 + 10;
LL prex[maxn];

LL solve(LL n){
    LL l = 1,r = maxn;
    LL ans = 0;
    while(l <= r){
        LL mid = (l + r) >> 1;
        LL tmp = (mid + 2) * (mid - 1) / 2;
        if(tmp <= n){
            ans = mid - 1;
            l = mid + 1;
        }
        else r = mid - 1;
    }
    return ans;
}

LL quick(LL a,LL b,LL p){
    LL ans = 1;
    while(b){
        if(b & 1) ans = ans * a % p;
        b >>= 1;
        a = a * a % p;
    }
    return ans;
}

LL niYuan(LL b,LL p){
    return quick(b,p - 2,p);
}

LL nY[maxn];

int main() {
#ifndef ONLINE_JUDGE
//    freopen("in.txt", "r", stdin);
//    freopen("out.txt", "w", stdout);
#endif

    prex[0] = prex[1] = 1LL;
    for(int i = 2;i < maxn;++ i)
        prex[i] = prex[i - 1] * i % mod;
    for(int i = 0;i < maxn;++ i) nY[i] = niYuan(prex[i],mod);
    int t = read();
    while(t --){
        LL n = read();
        if(n == 1){ printf("1\n"); continue; }
        LL num = solve(n);
        LL all = (2 + num + 1) * num / 2;
        LL lef = n - all;
        LL k = lef / num;
        LL m = lef % num;
//        debug(all); debug(lef);
//        debug(k); debug(m); debug(num);
//        debug(prex[2 + num - 1 + k - m]);
//        debug(niYuan(prex[2 + k - 1],mod));
//        debug(prex[num + 1 + k + 1]);
//        debug(niYuan(prex[num + 1 + k - m + 1],mod));
        write(prex[2 + num - 1 + k - m] * nY[2 + k - 1] % mod
              * prex[num + 1 + k + 1] % mod * nY[num + 1 + k - m + 1] % mod);
        ptch('\n');
    }

#ifndef ONLINE_JUDGE
    cout << "time: " << 1.0 * (clock() - prostart) / CLOCKS_PER_SEC << " s" << endl;
#endif
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值