UVA 10689 Yet another Number Sequence 矩阵快速幂 水呀水

Posted Pacify

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UVA 10689 Yet another Number Sequence 矩阵快速幂 水呀水相关的知识,希望对你有一定的参考价值。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

typedef long long ll;
const int N = 4;
int Mod;
int msize;

struct Mat
{
    int mat[N][N];
};

Mat operator *(Mat a, Mat b)
{
    Mat c;
    memset(c.mat, 0, sizeof(c.mat));
    for(int k = 0; k < msize; ++k)
        for(int i = 0; i < msize; ++i)
            if(a.mat[i][k])
                for(int j = 0; j < msize; ++j)
                    if(b.mat[k][j])
                        c.mat[i][j] = ((ll)a.mat[i][k] * b.mat[k][j] + c.mat[i][j])%Mod;
    return c;
}

Mat operator ^(Mat a, int k)
{
    Mat c;
    memset(c.mat,0,sizeof(c.mat));
    for(int i = 0; i < msize; ++i)
        c.mat[i][i]=1;
    for(; k; k >>= 1)
    {
        if(k&1) c = c*a;
        a = a*a;
    }
    return c;
}

int main()
{
//    freopen("in.txt", "r", stdin);
    int t, a, b, n, m;
    msize = 2;
    scanf("%d", &t);
    while(t--)
    {
        scanf("%d%d%d%d", &a, &b, &n, &m);
        Mod = 1;
        while(m--) Mod *= 10;
        if(n == 0)
        {
            printf("%d\\n", a%Mod);
            continue;
        }
        Mat A;
        A.mat[0][0] = 1, A.mat[0][1] = 1;
        A.mat[1][0] = 1, A.mat[1][1] = 0;
        A = A^(n-1);
        printf("%d\\n", (A.mat[0][0]*b + A.mat[0][1]*a)%Mod);
    }
    return 0;
}

 

以上是关于UVA 10689 Yet another Number Sequence 矩阵快速幂 水呀水的主要内容,如果未能解决你的问题,请参考以下文章

uva10689矩阵快速幂

D. Yet Another Yet Another Task (ST表模版 + 单调队列)

CF-1359 D. Yet Another Yet Another Task ST表+单调队列

CF-1359 D. Yet Another Yet Another Task ST表+单调队列

Educational Codeforces Round 88 (Rated for Div. 2) D. Yet Another Yet Another Task

Educational Codeforces Round 88 (Rated for Div. 2) D. Yet Another Yet Another Task