数论 UVALive-7728 Detachment 阶乘逆元

16 篇文章 0 订阅

https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=5750

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 109 + 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 ≤ 106 , 1 ≤ x ≤ 109

Output

Maximum s you can get modulo 109 + 7.

Note that we wants to be greatest product before modulo 109 + 7.

Sample Input

1

4

Sample Output

4

 

题目大意:

转换一下,即,将一个数字x,分成任意不同数字相加的和,求这些数字的最大乘积。

 

首先,一定条件下,能够分多一点的话乘积会大。通过手动打表找规律,发现 2 * ……(k个)……* (k+1)是分解成k个数相乘的第一个数。所以我们可以通过给定的x算出它应该分成k个数字使乘积最大。

然后我们可以算出多出的add,即x-分解成k个数乘积的第一个数。从最后一个数往前每个数+1可使乘积最大。注意特殊情况,add=k+1时,应使最后一个数+2。

注意特判1的情况。

然后写完t了,因为T太大,就先预处理出阶乘,以及阶乘的逆。有模数所以做除法的时候要转化为乘它的逆。

单独放一下预处理的代码:

fac[0] = 1;
for(int i = 1; i <= maxk; i++)
     fac[i] = (fac[i - 1] * i) % mod;
inv[maxk] = qpow(fac[maxk], mod - 2);
for(int i = maxk - 1; i >= 0; i--)
     inv[i] = (inv[i + 1] * (i + 1)) % mod;

如上真的非常神奇啊。

应用了费马小定理:

模为素数p的情况下,a^(p-1)=1(mod p)
那么 a^(p-2)=a^-1(mod p),也就是说a的逆元为 a^(p-2)

阶乘逆元就倒着来嘛(求逆是变相除法这样想的话就能感受到了)

 

最后,注意记得各种模!

 

#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;

const long long mod=1e9+7;
const int maxk=1e6;

long long x,k;
long long s;

long long fac[maxk+1],inv[maxk+1];

long long qpow(long long x,long long n)
{
     long long ans=1;
     while(n){
          if(n&1){
               ans*=x;
               ans%=mod;
          }
          n>>=1;
          x*=x;
          x%=mod;
     }
     return ans;
}

void pre()
{
    fac[0] = 1;
    for(int i = 1; i <= maxk; i++)
        fac[i] = (fac[i - 1] * i) % mod;
    inv[maxk] = qpow(fac[maxk], mod - 2);
    for(int i = maxk - 1; i >= 0; i--)
        inv[i] = (inv[i + 1] * (i + 1)) % mod;
}

int main()
{
     int T;

     scanf("%d",&T);
     /*
     t[0]=1;
     for(int i=1;i<=maxk;++i){
          t[i]=t[i-1]*i;
          t[i]%=mod;
     }
     inv[maxk]=qpow(t[maxk],mod-2);
     for(int i=maxk-1;i>=0;--i){
          inv[i]=inv[i+1]*(i+1);
          inv[i]%=mod;
     }
     */
     pre();
     //for(int i=0;i<25;++i){printf("%d fac:%lld inv:%lld\n",i,fac[i],inv[i]);}
     while(T--){
          s=1;
          scanf("%lld",&x);
          if(x==1){
               printf("1\n");
               continue;
          }
          double cal=sqrt(2*x+9*1.0/4)-1.5;
          k=(long long)cal;
          long long sum=k*(k+3)/2;
          long long add=x-sum;
          if(add<=k){
               s*=fac[k+1-add]%mod;
               s%=mod;
               s*=fac[k+2]%mod*inv[k+2-add]%mod;
               s%=mod;
          }
          else if(add==k+1){
               s*=fac[k+1]%mod*(k+3)%mod*inv[2]%mod;
               s%=mod;
          }
          printf("%lld\n",s);
     }

     return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值