HDU 5976 Detachment(逆元)+逆元总结

28 篇文章 0 订阅
8 篇文章 0 订阅

Description

In a highly developed alien society, the habitats are almost infinitedimensional 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,a2a1,a2, … (x= a1+a2a1+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≠ajai≠aj when i≠j). 2.Make this multidimensional space size s as large as possible (s= a1∗a2a1∗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

题目大意

将一个数分成若干个数的和,使得它们的积最大。
需满足这若干个数各不相等。输出最大的乘积%(1e9+7)。

解题思路

类似于在周长相等的情况下,正方形面积大于长方形面积,也就是说两个数值越接近,积越大,则该题转化为将一个数分解为尽量多个尽量靠近的整数,由于在计算乘积时,1 对于乘积结果没有贡献,所以分解的第一个数从 2 开始。分解的情况经过总结有三种:

1. ni=2i=x ∑ i = 2 n i = x ,比如 20=2+3+4+5+6,此时乘积为 n!;
2. ni=2i1=x ∑ i = 2 n i − 1 = x ,比如 19=2+3+4+5+(6-1),可转化为 19=3+4+5+7,即乘积为 (n+1)!2n ( n + 1 ) ! 2 ∗ n
3. ni=2ik=x ∑ i = 2 n i − k = x ,比如 15=2+3+4+5+6(-5),即加到6时序列中增加了一个5,于是我们想办法从序列的前面减去一个5,此时乘积为 n!(ni=2i)x n ! ( ∑ i = 2 n i ) − x

于是对于以上三种情况,我们可以先进行打表处理出前 n 项和与前 n 项和的阶乘,由于 x 的范围是 109 10 9 ,数据规模太大,所以在计算阶乘的时候直接对 1e9+7 取模,由于计算乘积结果的时候又涉及到除法,所以需要用到乘法逆元。

有关乘法逆元:
1.理解:如果b*b1%p==1,则b1是b%p的乘法逆元,且具有 ab a b %p==a*b1%p;
2.对于上式的证明(反证法):
       假设 ab a b %p!=a*b1%p;
       ab a b =k1*c+y1①,a*b1=k2*c+y2②;根据假设y1!=y2;
       ①-②: abab1=(k1k2)c+(y1y2) a b − a ∗ b 1 = ( k 1 − k 2 ) ∗ c + ( y 1 − y 2 ) ,令k1-k2=k,y1-y2=y,根据假设y!=0;
       等式两边同时b可得:a(1-b*b1)%c=b(k*c+y);
       等式两边同时%c,左式=((a%c)(1-b*b1)%c)%c=(a%c)((1%c-b*b1%c)%c)%c,因为b*b1%c=0,所以左式=0;
       则右式=b*(k*c+y)%c=((b%c)(k*c+y)%c)%c=((b%c)(k*c%c+y%c)%c)%c=0,因为a/b,所以b!=0,所以b%c!=0,可推知y%c=0,则y=0,这与假设条件相矛盾,所以假设不成立,命题成立。即 ab a b %p==a*b1%p。
3.使用乘法逆元的原因:对于取模运算规则

(a + b) % p = (a % p + b % p) % p (1)
(a - b) % p = (a % p - b % p) %p (2)
(a * b) % p = (a % p * b % p) % p (3) from-百度百科

唯独除法没有这个规则,但在数据规模太大,只能通过先取模以减小数据规模时,就使用到了乘法逆元,比如该题。
4.对于取模是质数的情况可以借助费马小定理求解逆元(a与p互质):
费马小定理 ap a p %p=a=> ap1 a p − 1 %p=1=> aap2 a ∗ a p − 2 %p=1。则 ap2 a p − 2 即为 a a <script type="math/tex" id="MathJax-Element-156">a</script>的逆元。比如该题。

实现代码

#include <iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
#define ll long long
const int mod=1e9+7;
#define maxn 50007
ll sum[maxn],fac[maxn];
void init()
{
    sum[0]=0,sum[1]=0,fac[1]=1;
    for(int i=2;i<maxn;i++)
    {
        sum[i]=sum[i-1]+i;
        fac[i]=fac[i-1]*i%mod;
    }
    sum[1]=1;
}
ll multi(ll a,ll p)
{
    ll res=1;
    while(p>0)
    {
        if(p%2==1) res=(res*a)%mod;
        a=(a*a)%mod;
        p/=2;
    }
    return res;
}
int main()
{
    int T;
    ll x,ans;
    scanf("%d",&T);
    init();
    while(T--)
    {
        scanf("%lld",&x);
        int n=lower_bound(sum,sum+maxn,x)-sum;
        if(sum[n]==x)
            ans=fac[n];
        else
        {
            if(sum[n]-1==x)
            {
                ans=fac[n+1]*multi(2*n,mod-2)%mod;
            }
            else
            {
                ans=fac[n]*multi(sum[n]-x,mod-2)%mod;
            }
        }
        printf("%lld\n",ans);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值