HDU 5976 Detachment 逆元

题意:

把一个数分解分解成若干个不相等的数,怎么分解使得这若干个数的积最大,输出这个积mod(1e+9)

分析:

考虑x=20的情况

如果不分解 那么 max ans===》20

如果分解成2个数    max ans===》9*11=99

如果分解成3个数    max ans===》5*7*8=280

如果分解成4个数    max ans===》3*4*6*7=504

如果分解成5个数    max ans===》2*3*4*5*6=720

不能分解成6个即以上的不相等的数

那么可以发现分解成的数越多ans越大

那么就一定存在一个数q满足   2+3+4+...+n-q=x

如果q=0的情况下 即2+3+4+...+n=x 那么答案就是 n!mod(1e9+7

如果q>1的情况下 即2+3+4+...+(q-1)+q+(q+1)+....+n-q=x那么答案就是(n!/q)mod(1e+9)

如果q=1的情况下 即1+2+3+....+n=x 因为求的是分解数的积那么就不能出现1,因为1会使得答案变小

那么 1+2+3+....+n可以转化为 3+4+..+(n-1)+(n+1) 如x=19 那么最大的ans=3*4*5*7 那么答案是 ((n+1)!/q*2)mod(1e9+7)

我们可以预处理 n!mod(1e9+7)和 2+3+...+n然后每次用lower_bound找到第一个大于x的数就可以了

ACcode:

#include <bits/stdc++.h>
#define ll long long
#define P acos(-1.0)
int mod=(1e9+7);
#define maxn 100005
using namespace std;
ll dp1[maxn];
ll dp2[maxn];
ll gcd(ll a,ll b){return b==0?a:gcd(b,a%b);}///gcd
ll extend_gcd(ll a,ll b,ll &x,ll &y){
    if(a==0&&b==0)return -1;
    if(b==0){x=1;y=0;return a;}
    ll d=extend_gcd(b,a%b,y,x);
    y-=a/b*x;
    return d;
}
        ///逆元
ll mod_reverse(ll a,ll n){
    ll x,y;
    ll d=extend_gcd(a,n,x,y);
    if(d==1)return (x%n+n)%n;
    else return -1;
}
int main(){
    int tot;
    memset(dp1,0,sizeof(dp1));
    memset(dp2,0,sizeof(dp2));
    dp2[1]=1;
    dp1[1]=0;
    for(int i=2;;++i){
        dp1[i]=dp1[i-1]+i;
        dp2[i]=dp2[i-1]*i%mod;
        if(dp1[i]>mod){
           tot=i;
            break;
        }
    }
    int n,loop;
    scanf("%d",&loop);
    while(loop--){
        scanf("%d",&n);
        if(n<=4)printf("%d\n",n);
        else {
            int pos=lower_bound(dp1,dp1+tot,n)-dp1;
            int s=dp1[pos]-n;
            if(s==0){
                printf("%d\n",dp2[pos]);
            }
            else if(s==1){
                s=2*pos;
                ll ans=dp2[pos+1]*mod_reverse(s,mod)%mod;
                ans%=mod;
                cout<<ans<<'\12';
            }
            else {
                ll ans=dp2[pos]*mod_reverse(s,mod)%mod;
                ans%=mod;
                cout<<ans<<'\12';
            }
        }
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值