HDU 5976 Detachment

Detachment

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

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
const int MAXN=1e5+5;
const LL MOD=1e9+7;
int has[MAXN],sum[MAXN],inv[MAXN],tot;
LL pro[MAXN];
int get_inv(int x)
{
    if(x==1)return 1;
    return (MOD-MOD/x)*get_inv(MOD%x)%MOD;
}
void table()
{
    has[2]=5;
    tot=2;
    while(has[tot]<=1e9)
    {
        tot++;
        has[tot]=has[tot-1]+tot+1;
    }
    sum[1]=2;
    for(int i=2;sum[i-1]<=1e9;i++)
        sum[i]=sum[i-1]+i+1;
    inv[1]=pro[1]=1;
    for(int i=2;i<MAXN;i++)
        (pro[i]=pro[i-1]*i)%=MOD,inv[i]=get_inv(pro[i]);
    for(int i=2;i<MAXN;i++)
        inv[i]=(MOD-MOD/i)*inv[MOD%i]%MOD;
}
void solve(int x)
{
    int num=upper_bound(has+2,has+tot,x)-has-1;
    int more=x-sum[num];
    int l,r;
    l=2+more/num;
    r=l+num-1;
    LL res;
    if(more%num)
        res=pro[r+1]*inv[l-1]%MOD*inv[r+1-more%num]%MOD;
    else
        res=pro[r]*inv[l-1]%MOD;
    printf("%lld\n",res);
}
int main()
{
    table();
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int x;
        scanf("%d",&x);
        if(x==1||x==2||x==3||x==4)
        {
            printf("%d\n",x);
            continue;
        }
        solve(x);
    }
    return 0;
}
时间: 2024-10-10 14:37:41

HDU 5976 Detachment的相关文章

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

Detachment Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 570    Accepted Submission(s): 192 Problem Description In a highly developed alien society, the habitats are almost infinite dimensiona

HDU - 5976 Detachment(逆元)

题意:将一个数x拆成a1+a2+a3+……,ai不等于aj,求最大的a1*a2*a3*……. 分析: 1.预处理前缀和前缀积,因为拆成1对乘积没有贡献,所以从2开始拆起. 2.找到一个id,使得2+3+4+……+id - 1(sum[id-1]) < x < 2+3+4+……+id(sum[id).(二分找即可) 则rest = x - sum[id - 1].将rest分配给2+3+4+id-1中的某个数. 3.在保证数字不重复的前提下,分配给的那个数越小越好,证明见4. 因此,应该分配给的

HDU 5976 数学,逆元

1.HDU 5976 Detachment 2.题意:给一个正整数x,把x拆分成多个正整数的和,这些数不能有重复,要使这些数的积尽可能的大,输出积. 3.总结:首先我们要把数拆得尽可能小,这样积才会更大(当然不能拆1).所以容易想到是拆成2+3+...+n+s=x,先求出n即2+3+...+n<x<2+3+...+n+(n+1),然后将某个数向右平移s个单位变为n+1即可.注意:(1)预处理出前缀和,前缀积.(2)将某个数移到n+1,要除这个数再乘n+1,这里要用逆元,也要预处理出来. #in

hdoj 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

HDU 1576 -- A/B (总结乘法逆元的几种求法)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1576 A/B Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 7264    Accepted Submission(s): 5774 Problem Description 要求(A/B)%9973,但由于A很大,我们只给出n(n=A%99

HDU5976 Detachment

1 /* 2 HDU5976 Detachment 3 http://acm.hdu.edu.cn/showproblem.php?pid=5976 4 数论 等差数列 5 * 6 * 7 */ 8 #include <cstdio> 9 #include <algorithm> 10 #include <vector> 11 using namespace std; 12 const int mod=1000000007; 13 const int Nmax=5000

HDU 6203 ping ping ping [LCA,贪心,DFS序,BIT(树状数组)]

题目链接:[http://acm.hdu.edu.cn/showproblem.php?pid=6203] 题意 :给出一棵树,如果(a,b)路径上有坏点,那么(a,b)之间不联通,给出一些不联通的点对,然后判断最少有多少个坏点. 题解 :求每个点对的LCA,然后根据LCA的深度排序.从LCA最深的点对开始,如果a或者b点已经有点被标记了,那么continue,否者标记(a,b)LCA的子树每个顶点加1. #include<Bits/stdc++.h> using namespace std;

HDU 5542 The Battle of Chibi dp+树状数组

题目:http://acm.hdu.edu.cn/showproblem.php?pid=5542 题意:给你n个数,求其中上升子序列长度为m的个数 可以考虑用dp[i][j]表示以a[i]结尾的长度为j的上升子序列有多少 裸的dp是o(n2m) 所以需要优化 我们可以发现dp的第3维是找比它小的数,那么就可以用树状数组来找 这样就可以降低复杂度 #include<iostream> #include<cstdio> #include<cstring> #include

hdu 1207 汉诺塔II (DP+递推)

汉诺塔II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4529    Accepted Submission(s): 2231 Problem Description 经典的汉诺塔问题经常作为一个递归的经典例题存在.可能有人并不知道汉诺塔问题的典故.汉诺塔来源于印度传说的一个故事,上帝创造世界时作了三根金刚石柱子,在一根柱子上从下往