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 0 1 0 |
    |1 0 0 1 |
    |1 0 0 0 |
                       |1 1 0 0 |^n
                       |0 0 1 0 |
|a[4],a[3],a[2],a[1]|* |1 0 0 1 |   = |a[5],a[4],a[3],a[2]|
                       |1 0 0 0 |
    A*B^n=C;
    直接套模板即可。
*/
#include<stdio.h>
#include<string.h>
#define N  11
int a[N];
struct matrix{
 __int64 mat[5][5];
};
matrix matmul(matrix b,matrix c,int mm) {
int i,j,k;
  matrix d;
  memset(d.mat,0,sizeof(d.mat));
  for(i=0;i<4;i++)
    for(j=0;j<4;j++)
    for(k=0;k<4;k++) {
    d.mat[i][j]+=b.mat[i][k]*c.mat[k][j];
    d.mat[i][j]%=mm;
    }
    return d;
}
matrix matpow(matrix f,matrix  ff,int k,int m) {
  while(k) {
    if(k&1)
        ff=matmul(ff,f,m);
    f=matmul(f,f,m);
    k/=2;
  }
  return ff;
}
int main(){
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF) {
        a[1]=2;a[2]=4;a[3]=6;a[4]=9;
        if(n<=4) {
        printf("%d\n",a[n]%m);
        continue;
        }
      matrix  aa,bb,cc;
      memset(aa.mat,0,sizeof(aa.mat));
       memset(bb.mat,0,sizeof(bb.mat));
      aa.mat[0][0]=1;//构建矩阵
      aa.mat[2][0]=1;
      aa.mat[3][0]=1;
      aa.mat[0][1]=1;
      aa.mat[1][2]=1;
      aa.mat[2][3]=1;
      bb.mat[0][0]=a[4];
      bb.mat[0][1]=a[3];
      bb.mat[0][2]=a[2];
      bb.mat[0][3]=a[1];
      cc=matpow(aa,bb,n-4,m);
        printf("%d\n",cc.mat[0][0]);
    }
return 0;}

时间: 2024-08-12 02:58:55

hdu 2604 矩阵快速幂模板题的相关文章

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)

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 //

HDU 2604 矩阵快速幂

题目大意 给定长度为l的只有f,m两种字母 的序列,问不出现fff,fmf的序列个数有多少个 每次的下一个状态都与前一次状态的后两个字母有关 比如我令mm : 0 , mf : 1 , fm : 2 , ff : 3: 那么dp[i][j] 表示长度为i的序列最后由j状态结尾的总个数,当然 j 要大于2 dp[i][0] = dp[i-1][0] + dp[i-1][2] dp[i][1] = dp[i-1][0] dp[i][2] = dp[i-1][1] + dp[i-1][3] dp[i]

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

hdu 1575 矩阵快速幂模板

1 #include "iostream" 2 #include "vector" 3 #include "cstring" 4 using namespace std; 5 6 typedef unsigned long int ULL; 7 typedef vector<ULL> vec; 8 typedef vector<vec> mat; 9 const ULL P=9973; 10 int n,m; 11 12

矩阵快速幂模板题

题目描述 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

51nod1113(矩阵快速幂模板)

题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1113 题意:中文题诶- 思路:矩阵快速幂模板 代码: 1 #include <iostream> 2 #define ll long long 3 using namespace std; 4 5 const int mod = 1e9+7; 6 const int MAXN = 1e2+10; 7 int n, m; 8 9 typedef struct