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;
}
时间: 2024-12-26 08:45:00

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

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; mems

uva 10706 Number Sequence(找规律)

uva 10706 Number Sequence A single positive integer iis given. Write a program to find the digit located in the position iin the sequence of number groups S1S2-Sk. Each group Skconsists of a sequence of positive integer numbers ranging from 1 to k, w

UVA - 10706 Number Sequence

先找到是在哪个集合内,再找到是集合内的哪个元素,最后找到元素的第几位数 #include<iostream> #include<algorithm> #include<cstdio> #include<cstring> using namespace std; long long table[100010]; long long psum[100010]; int n=100000; void maketable() { int i,flag=1,x=0;

【矩阵快速幂】UVA 10698 G - Yet another Number Sequence

[题目链接]click here~~ [题目大意] Let's define another number sequence, given by the following function: f(0) = a f(1) = b f(n) = f(n-1) + f(n-2), n > 1 When a = 0 and b = 1, this sequence gives the Fibonacci Sequence. Changing the values of a and b , you ca

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

1005 Number Sequence

Problem Description 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). Input The input consists of multiple test cases. Each test case co

hdu5014 Number Sequence(异或运算)

题目链接: huangjing 题意: 这个题目的意思是给出0~n的排列,然后找出与这个序列的配对使(a0 ⊕ b0) + (a1 ⊕ b1) +·+ (an ⊕ bn)最大.. 思路: 从大到小遍历每个数,然后找到与这个数二进制位数互补的数,那么他们的抑或值必定是pow(2,n)-1,,肯定是最大的.... 题目: Number Sequence Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Jav

HDU 5014 Number Sequence(2014 ACM/ICPC Asia Regional Xi&#39;an Online) 题解

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5014 Number Sequence Problem Description There is a special number sequence which has n+1 integers. For each number in sequence, we have two rules: ● ai ∈ [0,n] ● ai ≠ aj( i ≠ j ) For sequence a and sequ