(3070)POJ

本文介绍了矩阵快速幂运算的基本概念,并通过实例展示了如何使用C++实现矩阵快速幂运算,解决特定数学问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include<iostream>
#include<cstdio>
#include<string.h>
#include<string>
#include<set>
#include<algorithm>
#include<cmath>


#define ll __int64
#define MAX 1000009
using namespace std;


int n;
const int mod = 10000;
struct Matrix
{
    int  m[12][12];
};


Matrix mul(Matrix a,Matrix b) //¾ØÕó³Ë·¨
{
    int i,j,k;
    Matrix c;
    for (i = 0 ; i < n; i++)
        for (j = 0; j < n; j++)
        {
            c.m[i][j] = 0;
            for (k = 0; k < n; k++)
                c.m[i][j] += (a.m[i][k] * b.m[k][j])%mod;
            c.m[i][j] %= mod;
        }
    return c;
}


Matrix pow(Matrix a,Matrix b,int x)//µÝ¹éÇó¿ìËÙÃÝ
{
    while (x)
    {
        if (x & 1)
            b = mul(b,a);
        x = x >> 1;
        a = mul(a,a);
    }
    return b;
}


int main()
{
    int k;
    int t;
    Matrix A,B;
    while(~scanf("%d",&k))
    {
        if(k==-1)break;
        n = 2;
        A.m[0][0] = 1;
        A.m[1][0] = 1;
        A.m[0][1] = 1;
        A.m[1][1] = 0;
        Matrix C = pow(A,A,k);
        int ans = 0;
        printf("%d\n",C.m[1][1]);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值