码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-07-31 15:44:04      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:stream   ber   scan   技术   ==   span   ++   png   const   

技术分享

#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 矩阵快速幂 水呀水

标签:stream   ber   scan   技术   ==   span   ++   png   const   

原文地址:http://www.cnblogs.com/pach/p/7263026.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!