UVA 10689 Yet another Number Sequence

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 MOD;
long long x, y;
int n,m;

long long mod(long long a, long long b)
{
    if (a >= 0) return a%b;
    if (abs(a) % b == 0) return 0;
    return (a + b*(abs(a) / b + 1));
}

struct Matrix
{
    long long A[5][5];
    int R, C;
    Matrix operator*(Matrix b);
};

Matrix X, Y, Z;

Matrix Matrix::operator*(Matrix b)
{
    Matrix c;
    memset(c.A, 0, sizeof(c.A));
    int i, j, k;
    for (i = 1; i <= R; i++)
        for (j = 1; j <= C; j++)
            for (k = 1; k <= C; k++)
                c.A[i][j] = mod((c.A[i][j] + mod(A[i][k] * b.A[k][j], MOD)), MOD);
    c.R=R; c.C=b.C;
    return c;
}

void read()
{
    scanf("%lld%lld%d%d", &x, &y, &n,&m);
    if(m==1) MOD=10;
    if(m==2) MOD=100;
    if(m==3) MOD=1000;
    if(m==4) MOD=10000;
}

void init()
{
   // n = n - 1;
    Z.A[1][1] = x, Z.A[1][2] = y; Z.R = 1; Z.C = 2;
    Y.A[1][1] = 1, Y.A[1][2] = 0, Y.A[2][1] = 0, Y.A[2][2] = 1; Y.R = 2; Y.C = 2;
    X.A[1][1] = 0, X.A[1][2] = 1, X.A[2][1] = 1, X.A[2][2] = 1; X.R = 2; X.C = 2;
}

void work()
{
    while (n)
    {
        if (n % 2 == 1) Y = Y*X;
        n = n >> 1;
        X = X*X;
    }
    Z = Z*Y;

    printf("%lld\n", mod(Z.A[1][1], MOD));
}

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        read();
        init();
        work();
    }
    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