uva 10870 Recurrences

点击打开uva 10870

思路:构造矩阵+矩阵快速幂

分析:

1 题目给定f(n)的表达式 f(n) = a1 f(n - 1) + a2 f(n - 2) + a3 f(n -3) + ... + ad f(n - d)对于n > d的时候

2 那么我们可以构造出矩阵

   a1 a2 ... an        f(n-1)             f(n)

   1 0 ......... 0        f(n-2)           f(n-1)

   0 1 ..........0  *     ........  =>       ......

   ..................         .......              ......

   0 0 ...... 1 0         .......              f(n-d)

   0 0 0 0 ... 0        f(n-d)            f(n-d+1)

3 题目有个地方错了,两个case之间根本不需要空行

代码:

/************************************************
 * By: chenguolin                               *
 * Date: 2013-08-29                             *
 * Address: http://blog.csdn.net/chenguolinblog *
 ************************************************/
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

typedef long long int64;
const int N = 16;

int d , n , MOD;
int a[N] , f[N];
struct Matrix{
    int64 mat[N][N];
    Matrix operator*(const Matrix &m)const{
        Matrix tmp;
        for(int i = 0 ; i < d ; i++){
            for(int j = 0 ; j < d ; j++){
                tmp.mat[i][j] = 0;
                for(int k = 0 ; k < d ; k++)
                    tmp.mat[i][j] += mat[i][k]*m.mat[k][j]%MOD;
                tmp.mat[i][j] %= MOD;
            }
        }
        return tmp;
    }
};

void init(Matrix &m){
    memset(m.mat , 0 , sizeof(m.mat));
    for(int i = 0 ; i < d ; i++)
        m.mat[0][i] = a[i+1];
    for(int i = 1 ; i < d ; i++)
        m.mat[i][i-1] = 1;
}

int Pow(Matrix &m){
    if(n <= d)
        return f[n];
    n -= d;
    Matrix ans;
    memset(ans.mat , 0 , sizeof(ans.mat));
    for(int i = 0 ; i < d ; i++)
        ans.mat[i][i] = 1;
    while(n){
        if(n&1)
            ans = ans*m;
        n >>= 1;
        m = m*m;
    }
    int64 sum = 0;
    for(int i = 0 ; i < d ; i++)
        sum += ans.mat[0][i]*f[d-i]%MOD;
    return sum%MOD;
}

int main(){
    Matrix m;
    bool isFirst = true;
    while(scanf("%d%d%d" ,&d , &n , &MOD) && d+n+MOD){
        if(isFirst)
            isFirst = false;
        else
            puts("");
        for(int i = 1 ; i <= d ; i++){
            scanf("%d" , &a[i]);
            a[i] %= MOD;
        }
        for(int i = 1 ; i <= d ; i++){
            scanf("%d" , &f[i]);
            f[i] %= MOD;
        }
        init(m);
        printf("%d\n" , Pow(m));
    }
    return 0;
}
时间: 2024-07-29 12:10:24

uva 10870 Recurrences的相关文章

矩阵快速幂专题【完结】

第一题 hdu 1757 A Simple Math Problem 点击打开链接 思路:矩阵快速幂 分析: 1 最简单的矩阵快速幂的题目,直接利用矩阵求解即可 点击打开查看代码 第二题 hdu 1575 Tr A 点击打开hdu 1575 思路: 矩阵快速幂 分析: 1 题目给定一个n*n的矩阵要求矩阵的k次幂之后的矩阵的对角线的和 2 矩阵快速幂的裸题 点击打开查看代码 第三题 hdu 2604 Queuing 点击打开hdu 2604 思路: 递推+矩阵快速幂 分析; 1 根据题目的意思,

UVa 10602

链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1543 类型:贪心 原题: Company Macrohard has released it's new version of editor Nottoobad, which can understand a few voice commands.

UVa 10392 Factoring Large Numbers:素因子分解

10392 - Factoring Large Numbers Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=100&page=show_problem&problem=1333 One of the central ideas behind much cryptography is that factoring

UVa 10182 Bee Maja:规律&amp;amp;O(1)算法

10182 - Bee Maja Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1123 Maja is a bee. She lives in a bee hive with thousands of other bees. This bee hive c

算法题之UVA 763

Fibinary Numbers The standard interpretation of the binary number 1010 is 8 + 2 = 10. An alternate way to view the sequence ``1010'' is to use Fibonacci numbers as bases instead of powers of two. For this problem, the terms of the Fibonacci sequence

算法题:UVa 11461 Square Numbers (简单数学)

11461 - Square Numbers Time limit: 1.000 seconds http://uva.onlinejudge.org/index.php? option=com_onlinejudge&Itemid=8&category=467&page=show_problem&problem=24 56 A square number is an integer number whose square root is also an integer.

UVa 10183 How Many Fibs? (统计斐波那契数个数&amp;amp;高精度)

10183 - How Many Fibs? Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=115&page=show_problem&problem=1124 Recall the definition of the Fibonacci numbers:    f1 := 1    f2 := 2    fn :

UVa 701 The Archeologists&#039; Dilemma: 数学及枚举

701 - The Archeologists' Dilemma Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=115&page=show_problem&problem=642 An archeologist seeking proof of the presence of extraterrestrials i

UVa 550 Multiplying by Rotation:模拟乘法

550 - Multiplying by Rotation Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=100&page=show_problem&problem=491 Warning: Not all numbers in this problem are decimal numbers! Multiplic