hdu 4291 A Short problem 打表找规律&矩阵快速幂

本文介绍了一种利用矩阵快速幂解决特定数学问题的方法,并通过具体的C++代码实现展示了该算法的工作原理。文中详细解释了矩阵乘法及快速幂运算的过程,适用于对算法效率有较高要求的场景。

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

http://www.cnblogs.com/kuangbin/archive/2012/09/17/2688852.html


//author: CHC
//First Edit Time:	2014-10-18 16:11
//Last Edit Time:	2014-10-18 23:27
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <set>
#include <vector>
#include <map>
#include <queue>
#include <set>
#include <algorithm>
#include <limits>
using namespace std;
typedef long long LL;
const int MAXN=1e+4;
const int MAXM=1e+5;
const int INF= numeric_limits<int>::max();
const LL LL_INF= numeric_limits<LL>::max();
#define N 2
LL MOD=1e+9 +7;
// a = a * b
void matric_mul(LL a[][N],LL b[][N])
{
	int i,j,k;
    LL tmp[N][N];
    memset(tmp,0,sizeof(tmp));
	for(i=0;i<N;i++)
	{
		for(j=0;j<N;j++)
		{
			for(k=0;k<N;k++)
			{
				tmp[i][j] = (tmp[i][j]+a[i][k]*b[k][j]) % MOD;
                if(tmp[i][j]<0)tmp[i][j]+=MOD;
			}
		}
	}
	for(i=0;i<N;i++)
	{
		for(j=0;j<N;j++)
		{
			a[i][j] = tmp[i][j];
		}
	}
}
LL quickpow(LL n){
    if(n==0)return 0LL;
    //int ans=1,tmp=base;
    LL ans[2][2]={{1,0},{0,1}};
    LL tmp[2][2]={{0,1},{1,3}};
    while(n){
        if(n&1)matric_mul(ans,tmp);
            //ans=(ans*tmp)%MOD;
        //tmp=(tmp*tmp)%MOD;
        matric_mul(tmp,tmp);
        n>>=1;
    }
    return ans[0][1];
}
int main()
{
    LL n;
    while(~scanf("%I64d",&n)){
        MOD=183120;
        LL n1=quickpow(n);
        MOD=222222224;
        LL n2=quickpow(n1);
        MOD=1000000007;
        LL n3=quickpow(n2);
        printf("%I64d\n",n3);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值