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;
}
时间: 2024-10-12 04:01:31

UVA 10689 Yet another Number Sequence 矩阵快速幂 水呀水的相关文章

UVA - 10689 Yet another Number Sequence 矩阵快速幂

                  Yet another Number Sequence Let’s de?ne another number sequence, given by the following function:f(0) = af(1) = bf(n) = f(n − 1) + f(n − 2), n > 1When a = 0 and b = 1, this sequence gives the Fibonacci Sequence. Changing the values

Uva10689 Yet another Number Sequence ( 矩阵快速幂 )

Uva 10689Yet another Number Sequence(  矩阵快速幂  ) 题意: 就是矩阵快速幂,没什么好说的. 分析: 其实还是斐波那契数列.只是最后对应的矩阵不是(1,1)是(a,b)了 MOD = 1; for( int i = 0; i < m; ++i ) MOD *= 10; 代码 #include <cstdio> #include <cstring> #include <algorithm> using namespace s

HDU 1005 Number Sequence 矩阵快速幂

Number Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 236241    Accepted Submission(s): 60070 Problem Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A

SDUT1607:Number Sequence(矩阵快速幂)

题目:http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1607 题目描述 A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7. Given A, B, and n, you are to calculate the value of f(n). 输入

uva 11651 - Krypton Number System(矩阵快速幂)

题目链接:uva 11651 - Krypton Number System 题目大意:给定进制base,和分数score,求在base进制下,有多少个数的值为score,要求不能有连续相同的数字以及前导0.计算一个数的值即为相邻两位数的平方差和. 解题思路:因为score很大,所以直接dp肯定超时,但是即使对于base=6的情况,每次新添一个数score最大增加25(0-5),所以用dp[i][j]预处理出base平方以内的总数,然后用矩阵快速幂计算. #include <cstdio> #

UVA 10689 Yet another Number Sequence

简单矩阵快速幂. if(m==1) MOD=10; if(m==2) MOD=100; if(m==3) MOD=1000; if(m==4) MOD=10000; 剩下的就是矩阵快速幂求斐波那契数列第n项取模 #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<algorithm> using namespace std; long long

hdu-5667 Sequence(矩阵快速幂+费马小定理+快速幂)

题目链接: Sequence Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 65536/65536 K (Java/Others) Problem Description Holion August will eat every thing he has found. Now there are many foods,but he does not want to eat all of them at once,so he fi

HDU5950-Recursive sequence(矩阵快速幂)

题目链接:Recursive sequence 题意:给出n头母牛,第一头报a,第二头报b,第i头报f[i-2]*2+f[i-1]+i^4,问第n头母牛报数多少 分析:N,a,b<2^31,果断矩阵快速幂,关键是要推出公式,公式如下,仅作参考 1 0 0 0 0 0 0        1               1 1 1 0 0 0 0 0        i                i+1 1 2 1 0 0 0 0       i2              (i+1)2 1 3

zoj3690--Choosing number(dp,矩阵快速幂)

Choosing number Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Submit Status Description There are n people standing in a row. And There are m numbers, 1.2...m. Every one should choose a number. But if two persons standing