【ZZULIOJ 2180】GJJ的日常之沉迷数学 【逆元 or 矩阵快速幂】

Description

GJJ每天都要膜拜一发数学大佬,因为GJJ的数学太差了。这不,GJJ又遇到难题了,他想求助WJJ,但是WJJ这几天忙于追妹子,哪有时间给他讲题, 于是GJJ求助于热爱ACM的你,Acmer们能帮帮他吗?问题是求: k^0 + k^1 +…+ k^(n) mod p (0 < k < 100, 0 <= n <= 10^9, p = 1000000007)
例如:6^0 + 6^1 +…+ 6^(10) mod 1000000007 (其中k = 6, n = 10, p = 1000000007)
Input

输入测试数据有多组,每组输入两个整数k, n
Output

每组测试数据输出:Case #: 计算结果
Sample Input

2 1
6 10
Sample Output

Case 1: 3
Case 2: 72559411

代码一
逆元
逆元可以用多种方法解决。例如 求a关于p的逆元 [inv(a,p)]
首先要明白只有当gcd(a,p)==1 才有逆元。
第一种 如果p为素数,我们可以用费马小定理来求解(a^(p-1)=1( mod p ) ) 公式为 inv(a,p) = pow ( a , p-2 , p ) [这里可以用快速幂取模来解决] 。
第二种 只要gcd(a,p)==1。就可以用。扩展欧几里得来求逆元。

void exgcd (LL a,LL b,LL &x,LL &y,LL &d) {
    if(b==0) {  d=a; x=1; y=0; }
    else {
        exgcd(b,a%b,y,x,d);
        y-=x*(a/b);
    }
}
LL inv(LL a,LL p){
    LL d,x,y;
    exgcd(a,p,x,y,d);
    return d==1?(x%p+p)%p:-1; 
}

ac代码 这里用的是 费马小定理求逆元

#include<bits/stdc++.h>
using namespace std ;
typedef long long LL ;

const int MAXN = 50000+10;
const int MAXM = 1e5 ;
const LL mod  = 1000000007;

LL qp(LL a,LL b){
    LL s=1LL,base=a%mod;
    while(b){
        if(b&1) s=s*base%mod;
        base=base*base%mod;
        b>>=1;
    }
    return s;
}

int main(){
    LL k,n;   int ncase=1;
    while(scanf("%lld%lld",&k,&n)!=EOF){
        printf("Case %d: ",ncase++);
        if(n==0) {
            puts("1");
            continue;
          }
        if(k==1) printf("%lld\n",(n+1)%mod); 
        else {
            //  a*b ^ (M-2) (mod M)  // 逆元公式
            LL a=(qp(k,n+1)-1LL+mod)%mod;
            LL b=(k-1)%mod;
            LL ans=a*qp(b,mod-2)%mod;
            printf("%lld\n",ans);
          }
      }
    return  0;
}

听大朋说他用的矩阵快速幂写的,想着我也写写(矩阵快速幂的板子都块忘了,赶紧熟悉熟悉 TAT )
初始矩阵 f[2]=k s[1]=1
过渡矩阵
k 1
0 1
结果矩阵 f[n+1] s[n]
代码

#include<bits/stdc++.h>
using namespace std ;
typedef long long LL ;

const int MAXN = 1e3 ;
const int MAXM = 1e5 ;
const int mod  = 1000000007 ;

struct Matrix{
    int w,h;
    LL a[5][5];
};
int n,m;
Matrix ori,it,res;
void init(){
    res.h=res.w=2;
    for(int i=1;i<=2;i++){
        for(int j=1;j<=2;j++) 
            if(i==j) res.a[i][j]=1;
            else res.a[i][j]=0;
    }
    ori.h=ori.w=2;
    ori.a[1][1]=m;ori.a[1][2]=1;
    ori.a[2][1]=0;ori.a[2][2]=1;
    it.h=1;it.w=2;
    it.a[1][1]=m;it.a[1][2]=1;    /*******/
} 

Matrix multi(Matrix x,Matrix y){
    Matrix z;
    z.h=x.h;z.w=y.w;memset(z.a,0,sizeof(z.a));
    for(int i=1;i<=x.h;i++){
        for(int k=1;k<=x.w;k++){
            if(x.a[i][k]==0) continue;
            for(int j=1;j<=y.w;j++){
                z.a[i][j]=(z.a[i][j]+x.a[i][k]*y.a[k][j]%mod)%mod;
            }
        }
    }
    return z;
}

void Matrix_mod(int n){
    while(n){
        if(n&1) res=multi(ori,res);
        ori=multi(ori,ori);
        n>>=1;
    } 
    res=multi(it,res);
    printf("%lld\n",res.a[1][2]);
} 

int main(){
    int ncase=1; 
    while(scanf("%d%d",&m,&n)!=EOF){
        init();
        printf("Case %d: ",ncase++);
        if(n==0) {
            puts("1");continue;
        }
        if(m==1) {
            printf("%d\n",n+1);continue;
        }
        Matrix_mod(n);
    }
    return  0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值