• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
村雨sup
自己选的路,跪着也要走完 XD
博客园    首页    新随笔    联系   管理    订阅  订阅
矩阵快速幂——POJ3070

矩阵快速幂和普通的快速幂差不多,只不过写起来比较麻烦一点,需要重载*运算符。

模板:

struct mat
{
    int m[maxn][maxn];
}unit;

mat operator * (mat a,mat b)
{
    mat ret;
    ll x;
    for(int i=0;i < n;i++)
        for(int j=0;j < n;j++)
        {
            x = 0;
            for(int k=0;k < n;k++)
                x += mod((ll)a.m[i][k]*b.m[k][j]);
            ret.m[i][j] = mod(x);
        }
    return ret;
}

void init_unit()             //初始化单位矩阵
{
    for(int i=0;i < maxn;i++)
        unit.m[i][i] = 1;
    return;
}

mat pow_mat(mat a,ll n)
{
    mat ret = unit;
    while(n)
    {
        if(n&1) ret = ret*a;
        a = a*a;
        n >>= 1;
    }
    return ret;
}

例题:POJ3070

用矩阵快速幂求fib并取模10000

有这个定理就很好求了:

#include <bits/stdc++.h>
using namespace std;
typedef long long  ll;
const int INF = 0x3f3f3f3f;
const int moder = 10000;
const int maxn = 110;
#define mod(x)  ((x)%moder)
int n = 2;

struct mat
{
    int m[maxn][maxn];
}unit;

mat operator * (mat a,mat b)
{
    mat ret;
    ll x;
    for(int i=0;i < n;i++)
        for(int j=0;j < n;j++)
        {
            x = 0;
            for(int k=0;k < n;k++)
                x += mod((ll)a.m[i][k]*b.m[k][j]);
            ret.m[i][j] = mod(x);
        }
    return ret;
}

void init_unit()
{
    for(int i=0;i < maxn;i++)
        unit.m[i][i] = 1;
    return;
}

mat pow_mat(mat a,ll n)
{
    mat ret = unit;
    while(n)
    {
        if(n&1) ret = ret*a;
        a = a*a;
        n >>= 1;
    }
    return ret;
}




int main()
{
    ll p;
    init_unit();
    while(cin >> p)
    {
        if(p == -1) break;
        mat a;
        a.m[0][0] = 1;
        a.m[0][1] = 1;
        a.m[1][0] = 1;
        a.m[1][1] = 0;
        a = pow_mat(a,p);
        cout << a.m[0][1] << endl;
    }
    return 0;
}

 

要注意的是maxn开小一点,不然本地会炸。

posted on 2018-02-18 12:28  村雨sup  阅读(139)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3