ahu 395 快速矩阵幂初学 + 矩阵快速幂模板

15 篇文章 0 订阅
#include <cstdio>
#include <string>
#include <cmath>
#include <iostream>
using namespace std;
const long long M = 10003;
const long long N = 3;
long long t,b,c=0,f1,f2;
struct Node  //矩阵
{
    long long line,cal;
    long long a[N+1][N+1];
    Node(){
        line=3,cal=3;
        a[0][0] = b; a[0][1] = 1; a[0][2] = 0;
        a[1][0] = t; a[1][1] = 0; a[1][2] = 0;
        a[2][0] = c; a[2][1] = 0; a[2][2] = 1;
    }
};

Node isit(Node x,long long c)  //矩阵初始化
{
    for(long long i=0;i<N;i++)
        for(long long j=0;j<N;j++)
            x.a[i][j]=c;
    return x;
}

Node Matlab(Node x,Node s)  //矩阵乘法
{
    Node ans;
    ans.line = x.line,ans.cal = s.cal;
    ans=isit(ans,0);
    for(long long i=0;i<x.line;i++)
    {
        for(long long j=0;j<x.cal;j++)
        {
            for(long long k=0;k<s.cal;k++)
            {
                ans.a[i][j] += x.a[i][k]*s.a[k][j];
                ans.a[i][j]=(ans.a[i][j]+M)%M;
            }
        }
    }
    return ans;
}
long long Fast_Matrax(long long n)  //矩阵快速幂
{
    if(n==1)
        return f1;
    n-=2;
    long long x=1,f=n,ok=1;
    Node ans,tmp,ch;
    ans.line = 1,ans.cal = 3;
    ans.a[0][0] = f2, ans.a[0][1] = f1 ,ans.a[0][2] = 1;
    while(n>0)
    {
        if(n%2)
        {
            ans=Matlab(ans,tmp);
        }
        tmp=Matlab(tmp,tmp);
        n/=2;
    }
    return ans.a[0][0];
}
int main()
{
    long long n,T;
    while(1)
    {
        scanf("%lld%lld%lld%lld%lld",&f1,&f2,&b,&t,&n);
        if(f1==0&&f2==0&&b==0&&t==0&&n==0)break;
        printf("%lld\n",Fast_Matrax(n));
    }
    return 0;
}
//模板
int n,k,b,mod = 2;
char c[500];
//快速幂
struct ma{
     int m[115][115],row,col;
     ma()
     {
         memset(m,0,sizeof(m));
         row = col = 0;
     }
     ma operator * (ma ma1) //乘
     {
        ma ans;
        ans.row = row;
        ans.col = ma1.col;
        for(int i = 1;i <= ans.row;i++)
            for(int j = 1;j <= ans.col;j++)
            {
                for(int k = 1;k <= col;k++)
                {
                    ans.m[i][j] += (m[i][k] * ma1.m[k][j]);
                }
                 ans.m[i][j] %= mod;
            }
        return ans;
     }
     ma operator + (ma ma1) //加
     {
         ma ans;
         ans.row = row;
         ans.col = col;
         for(int i = 1;i <= row;i++)
             for(int j = 1;j <= col;j++)
                 {
                    ans.m[i][j] =  ma1.m[i][j] + m[i][j];
                    ans.m[i][j] %= mod;
                 }
          return  ans;
     }
     ma operator^(int n)  // 快速幂
     {
         ma ans,ma1;

         ans.row = row; ans.col = col;
         for(int i = 1;i <= row;i++)
             ans.m[i][i] = 1;

         ma1.row = row;  ma1.col = col;
         for(int i = 1;i <= row;i++)
             for(int j = 1;j <= col;j++)
                  ma1.m[i][j] = m[i][j];
         while(n)
         {
            if(n&1)ans = ans * ma1;
            ma1 = ma1 * ma1;
            n >>= 1;
         }
         return ans;
     }
     void debug()
     {
         for(int i = 1;i <= row;i++)
         {
             for(int j = 1;j <= col;j++)
                 printf("%d ",m[i][j]);
             printf("\n");
         }
     }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值