HDU 1757 矩阵相乘,快速幂模板题

HDU 1757

题意:  If x < 10, f(x) = x;

     If x >= 10, f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + …… + a9 * f(x-10);

      给出k和mod,求f(k)。

总结 1、特别注意,矩阵相乘不满足交换律,即a*b != b*a。  2、感觉推方程有点困难。 3、矩阵初始化注意。

f(x-10)   0 0 0 0 0 0 0 0 0        ( first矩阵 )      
f(x-9)     0 0 0 0 0 0 0 0 0
f(x-8)     0 0 0 0 0 0 0 0 0
f(x-7)     0 0 0 0 0 0 0 0 0
f(x-6)     0 0 0 0 0 0 0 0 0
f(x-5)     0 0 0 0 0 0 0 0 0
f(x-4)     0 0 0 0 0 0 0 0 0
f(x-3)     0 0 0 0 0 0 0 0 0
f(x-2)     0 0 0 0 0 0 0 0 0
f(x-1)     0 0 0 0 0 0 0 0 0

*

0 1 0 0 0 0 0 0 0 0      ( temp矩阵 )
  0 0 1 0 0 0 0 0 0 0
  0 0 0 1 0 0 0 0 0 0
  0 0 0 0 1 0 0 0 0 0
  0 0 0 0 0 1 0 0 0 0
  0 0 0 0 0 0 1 0 0 0
  0 0 0 0 0 0 0 1 0 0
  0 0 0 0 0 0 0 0 1 0
  0 0 0 0 0 0 0 0 0 1

a9 8 7 6 5 4 3 2 1 0  (这一行是ai)

=
            
f(x-9)     0 0 0 0 0 0 0 0 0
f(x-8)     0 0 0 0 0 0 0 0 0
f(x-7)     0 0 0 0 0 0 0 0 0
f(x-6)     0 0 0 0 0 0 0 0 0
f(x-5)     0 0 0 0 0 0 0 0 0
f(x-4)     0 0 0 0 0 0 0 0 0
f(x-3)     0 0 0 0 0 0 0 0 0
f(x-2)     0 0 0 0 0 0 0 0 0
f(x-1)     0 0 0 0 0 0 0 0 0
f(x)        0 0 0 0 0 0 0 0 0

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<bitset>
#include<vector>
#include<set>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
#define F(i,a,b)  for (int i=a;i<b;i++)
#define FF(i,a,b) for (int i=a;i<=b;i++)
#define mes(a,b)  memset(a,b,sizeof(a))
#define INF 0x3f3f3f3f
typedef long long ll;
const int N = 1e5+10, Maxn = 15;

ll k,m=100;
struct Mat
{
    ll mat[Maxn][Maxn];
    Mat() {
        mes(mat,0);
        F(i,0,Maxn) mat[i][i]=1;
    }
} E;
Mat first, temp;

Mat operator * (Mat a, Mat b)
{
    Mat ans;
    memset(ans.mat, 0, sizeof(ans.mat));
    for (int i = 0; i < Maxn; i++)
        for (int j = 0; j < Maxn; j++)
            for (int k = 0; k < Maxn; k++)
                ans.mat[i][j] = (ans.mat[i][j] + a.mat[i][k] * b.mat[k][j]) % m;
    return ans;
}
Mat operator ^ (Mat a,ll x)
{
    Mat p = E, q = a;
    while (x) {
        if(x&1)  p = p*q;
        x>>=1;
        q = q*q;
    }
    return p;
}
int main()
{
    mes(first.mat, 0);
    F(i,0,10) first.mat[i][0]=i;
    while(~scanf("%lld%lld", &k, &m)) {
        mes(temp.mat, 0);
        FF(i,0,8) temp.mat[i][i+1]=1;
        F(i,0,10) {
            scanf("%lld", &temp.mat[9][9-i]);
        }
        if(k<10) {
            printf("%lld\n", k);
            continue;
        }
        Mat ans= (temp^(k-9))*first;   //注意,这里顺序不要反了
        printf("%lld\n", ans.mat[9][0]);
    }

    return 0;
}

矩阵模板

const int MOD , Maxn;  //MOD作余,Maxn为矩阵范围
struct Mat
{
    ll mat[Maxn][Maxn];     //开的long long
    Mat() {
        mes(mat,0);
        F(i,0,Maxn) mat[i][i]=1;    //对角线初始化为1,其它为0
    }
} E;   //单位矩阵
Mat first, temp;

Mat operator * (Mat a, Mat b)   //重载 * ,特别注意,矩阵相乘不满足交换律,即a*b != b*a
{
    Mat ans;
    memset(ans.mat, 0, sizeof(ans.mat));
    for (int i = 0; i < Maxn; i++)
        for (int j = 0; j < Maxn; j++)
            for (int k = 0; k < Maxn; k++)
                ans.mat[i][j] = (ans.mat[i][j] + a.mat[i][k] * b.mat[k][j]) % MOD;
    return ans;
}

Mat operator ^ (Mat a,ll x)    ////重载 ^
{
    Mat p = E, q = a;
    while (x) {
        if(x&1)  p = p*q;
        x>>=1;
        q = q*q;
    }
    return p;
}

Mat mul(Mat a,Mat b)    //函数 *
{
    Mat ans;
    int i,j,k;
    for(i=0;i<Maxn;i++){
        for(j=0;j<Maxn;j++)
        {
            ans.mat[i][j]=0;
            for(k=0;k<Maxn;k++){
                ans.mat[i][j]+=a.mat[i][k]*b.mat[k][j]%m;
                ans.mat[i][j]%=m;
            }
        }
    }
    return ans;
}

Mat Pow_Mod(Mat s, int b){   //函数开方
    Mat ans;
    for(int i=0; i<Maxn; ++i) ans.mat[i][i]=1;
    while(b){
        if(b&1) ans=mul(ans, s);
        s= mul(s, s);
        b>>=1;
    }
    return ans;
}

void prMat(Mat a)   //打印矩阵
{
    int i,j;
    for(i=0;i<Maxn;i++)
    {
        for(j=0;j<Maxn;j++)
            printf("%d ",a.mat[i][j]);
        puts("");
    }
    puts("");
    return ;
}
时间: 2024-10-15 07:19:41

HDU 1757 矩阵相乘,快速幂模板题的相关文章

hdu-1757 A Simple Math Problem---矩阵快速幂模板题

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1757 题目大意: 求递推式第k项模m If x < 10 f(x) = x.If x >= 10 f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + -- + a9 * f(x-10);And ai(0<=i<=9) can only be 0 or 1 . 解题思路: 构建矩阵 直接用矩阵快速幂模板求解 注意,小于10的时候不能直接输出

hdu 2604 矩阵快速幂模板题

/* 矩阵快速幂: 第n个人如果是m,有f(n-1)种合法结果 第n个人如果是f,对于第n-1和n-2个人有四种ff,fm,mf,mm其中合法的只有fm和mm 对于ffm第n-3个人只能是m那么有f(n-4)种 对于fmm那么对于第n-3个人没有限制有f(n-3)种 顾f(n)=f(n-1)+f(n-3)+f(n-4); 求出前四个结果分别是 a[1]=2;a[2]=4;a[3]=6;a[4]=9; A=|a[4],a[3],a[2],a[1]| 可以构造矩阵 |1 1 0 0 | B= |0

hdu 1575 求一个矩阵的k次幂 再求迹 (矩阵快速幂模板题)

Problem DescriptionA为一个方阵,则Tr A表示A的迹(就是主对角线上各项的和),现要求Tr(A^k)%9973. Input数据的第一行是一个T,表示有T组数据.每组数据的第一行有n(2 <= n <= 10)和k(2 <= k < 10^9)两个数据.接下来有n行,每行有n个数据,每个数据的范围是[0,9],表示方阵A的内容. Output对应每组数据,输出Tr(A^k)%9973. Sample Input22 21 00 13 999999991 2 34

CodeForces 450B (矩阵快速幂模板题+负数取模)

题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=51919 题目大意:斐波那契数列推导.给定前f1,f2,推出指定第N项.注意负数取模的方式:-1%(10^9+7)=10^9+6. 解题思路: 首先解出快速幂矩阵.以f3为例. [f2]  * [1 -1] = [f2-f1]=[f3]  (幂1次) [f1]  * [1  0]     [f2]      [f2] 于是fn=[f2] *[1 -1]^(n-2)

求幂大法,矩阵快速幂,快速幂模板题--hdu4549

hdu-4549 求幂大法.矩阵快速幂.快速幂 题目 M斐波那契数列 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 6217 Accepted Submission(s): 1902 Problem Description M斐波那契数列F[n]是一种整数数列,它的定义如下: F[0] = a F[1] = b F[n] = F[n-1] *

HDU1757又是一道矩阵快速幂模板题

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1757 按照题目的要求构造矩阵 //Author: xiaowuga //矩阵: //a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 9 // 1 0 0 0 0 0 0 0 0 0 8 // 0 1 0 0 0 0 0 0 0 0 7 // 0 0 1 0 0 0 0 0 0 0 6 // 0 0 0 1 0 0 0 0 0 0 5 // 0 0 0 0 1 0 0 0 0 0 4 //

HDU1575:Tr A(矩阵快速幂模板题)

http://acm.hdu.edu.cn/showproblem.php?pid=1575 #include <iostream> #include <string.h> #include <stdlib.h> #include <cstdio> #include <algorithm> #define mod 9973 using namespace std; struct matrix { int a[11][11]; } init,res

POJ3070:Fibonacci(矩阵快速幂模板题)

http://poj.org/problem?id=3070 #include <iostream> #include <string.h> #include <stdlib.h> #include <cstdio> #include <algorithm> #define mod 10000 using namespace std; struct m { int a[3][3]; } init,res; int n; m Mult(m x,m

矩阵快速幂模板题

题目描述 God Water likes to eat meat, fish and chocolate very much, but unfortunately, the doctor tells him that some sequence of eating will make them poisonous. Every hour, God Water will eat one kind of food among meat, fish and chocolate. If there ar