hdu5976(前缀+逆元)

来源:互联网 发布:新能源就业前景 知乎 编辑:程序博客网 时间:2024/06/05 12:43

Detachment

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1313    Accepted Submission(s): 376


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 ( aiaj when i≠j).
2.Make this multidimensional space size s as large as possible (s= a1a2*...).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
14
 

Sample Output
4
 
题意:
给你一个数n,要求将n分成任意不同的数,并使其乘积s最大,结果s对1000000007取模
解题思路:
易知n<=4时,s=n
当n>4时,先写几个数可知道规律
5=2*3
6=2*4→2*3*1(将多出的1加到3上)
7=3*4→2*3*2(多余的2分别加到2和3上)
8=3*5→2*3*3(多余的3先分别加到2和3上但还是多1,再加到3上,于是2变成3,3变成5)
9=2*3*4
10=2*3*5→2*3*4*1(同理1加到4上)
发现规律后分析一下:
对于大于4的n能将其拆分成
n=2+3+4+…+k+△x (△x<=k)
即2+3+4+…+k<=n<2+3+4+…+k+1
当将△x分配完之后我们都会得到以下两个式子中的一个
①2*3*...*(i-1)*(i+1)*...*k*(k+1)
②3*4*...*i*(i+1)*...*k*(k+2)
式子由两个连续的积组成,所以我们可以用两个阶乘除来求解,又涉及到取模所以要用逆元
用前缀和和前缀积二分查找来优化,求逆元用扩展欧几里得
#include<iostream>#include<cstdio>#include<cstdlib>#include<cmath>#include <algorithm>using namespace std;typedef long long LL;const LL mod=1e9+7;LL sum[50000];LL mul[50000];void inti(){    sum[0]=sum[1]=0;    mul[0]=mul[1]=1;    for(int i=2;i<50000;i++)    {        sum[i]=sum[i-1]+i;        mul[i]=((mul[i-1]%mod)*(i%mod))%mod;    }}void exgcd(LL a, LL b, LL& d, LL& x, LL& y){     if(!b){ d = a; x = 1; y = 0;}    else{        exgcd(b, a%b, d, y, x);        y -= x*(a/b);    }}LL inv(LL a, LL n){      LL d, x, y;    exgcd(a, n, d, x, y);    return (x+n)%n;}int main(){    inti();    int t;    scanf("%d",&t);    while(t--)    {        int n;        scanf("%d",&n);        if(n<=4)        {            printf("%d\n",n);        continue;        }        int  pos=upper_bound(sum,sum+50000,n)-sum-1;        int num=n-sum[pos];        long long x,y,ans;        if(num==pos)            {                ans=(mul[pos]%mod*inv(2,mod)%mod*(pos+2)%mod)%mod;            }            else if(num==0)            ans=mul[pos];            else            {                ans=(mul[pos+1]%mod*inv(mul[pos-num+1],mod)%mod*mul[pos-num]%mod)%mod;            }            printf("%lld\n",ans);    }    return 0;}


原创粉丝点击