矩阵快速幂 HDU3483

  1 #include <iostream>
  2 #include <cstring>
  3
  4 using namespace std;
  5
  6 //矩阵大小上限
  7 const int SIZ=100;
  8 int MOD;
  9
 10 //矩阵大小为n*m,初始化全部为0
 11 struct mat
 12 {
 13     int n,m;
 14     long long  ar[SIZ][SIZ];
 15     mat()
 16     {
 17         memset(ar,0,sizeof(ar));
 18         n=m=SIZ;
 19     };
 20 };
 21
 22 //矩阵乘法
 23 mat operator *(mat a,mat b)
 24 {
 25     mat c;
 26     c=mat();
 27     c.n=a.n;
 28     c.m=b.m;
 29     for(int i=1;i<=a.n;i++)
 30         for(int j=1;j<=b.m;j++)
 31             for(int k=1;k<=a.m;k++)
 32             {
 33                 c.ar[i][j]+=(a.ar[i][k]*b.ar[k][j])%MOD;
 34                 c.ar[i][j]%=MOD;
 35             }
 36     return c;
 37 }
 38
 39 //矩阵加法
 40 mat operator +(mat a,mat b)
 41 {
 42     mat c;
 43     c=mat();
 44     c.n=a.n;
 45     c.m=a.m;
 46     for(int i=1;i<=a.n;i++)
 47         for(int j=1;j<a.m;j++)
 48             c.ar[i][j]=a.ar[i][j]+b.ar[i][j];
 49     return c;
 50 }
 51
 52 //矩阵快速幂
 53 mat operator ^(mat a,int k)
 54 {
 55     mat c;
 56     c=mat();
 57     c.n=a.n;
 58     c.m=a.m;
 59     for(int i=1;i<=a.n;i++)
 60         c.ar[i][i]=1;
 61     while(k)
 62     {
 63         if(k&1)
 64             c=c*a;
 65         a=a*a;
 66         k/=2;
 67     }
 68     return c;
 69 }
 70
 71 long long  tarr[100][100];
 72 long long  C(long long  a,long long  b)
 73 {
 74     memset(tarr,0,sizeof(tarr));
 75     for(int i=0;i<=a;i++)
 76         tarr[i][0]=1;
 77     for(int i=1;i<=a;i++)
 78         for(int t=1;t<=i;t++)
 79         {
 80             tarr[i][t]=(tarr[i-1][t-1]+tarr[i-1][t])%MOD;
 81         }
 82     return tarr[a][b];
 83 }
 84
 85 int main()
 86 {
 87     int n,x;
 88     while(cin>>n>>x>>MOD&&(n!=-1||x!=-1||MOD!=-1))
 89     {
 90         mat tt;
 91         tt=mat();
 92         tt.m=tt.n=x+2;
 93         for(int i=1;i<tt.m;i++)
 94         {
 95             for(int t=1;t<=i;t++)
 96             {
 97                 tt.ar[i][t]=C(i-1,t-1)*x%MOD;
 98             }
 99         }
100         tt.ar[tt.m][tt.m-1]=tt.ar[tt.m][tt.m]=1;
101         mat ans;
102         ans=mat();
103         ans.n=x+2;
104         ans.m=1;
105         for(int i=1;i<x+2;i++)
106         {
107             ans.ar[i][1]=x;
108         }
109         ans.ar[x+2][1]=0;
110         cout<<((tt^(n))*ans).ar[x+2][1]%MOD<<endl;
111     }
112     return 0;
113 }

时间: 2024-07-29 01:49:17

矩阵快速幂 HDU3483的相关文章

矩阵快速幂刷题系列

来源自http://blog.csdn.net/chenguolinblog/article/details/10309423 hdu 1575 Tr A Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5587    Accepted Submission(s): 4200 Problem Description A为一个方阵,则Tr

HDU 1757 A Simple Math Problem (矩阵快速幂)

[题目链接]:click here~~ [题目大意]: 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); 问f(k)%m的值. [思路]:矩阵快速幂,具体思路看代码吧,注意一些细节. 代码: #include<bits/stdc++.h> using namespace std; typedef long long LL; const

Codeforces Round #291 (Div. 2) E - Darth Vader and Tree (DP+矩阵快速幂)

这题想了好长时间,果断没思路..于是搜了一下题解.一看题解上的"快速幂"这俩字,不对..这仨字..犹如醍醐灌顶啊...因为x的范围是10^9,所以当时想的时候果断把dp递推这一方法抛弃了.我怎么就没想到矩阵快速幂呢.......还是太弱了..sad..100*100*100*log(10^9)的复杂度刚刚好. 于是,想到了矩阵快速幂后,一切就变得简单了.就可以把距离<=x的所有距离的点数都通过DP推出来,然后一个快速幂就解决了. 首先DP递推式很容易想到.递推代码如下: for(

POJ 3233 - Matrix Power Series ( 矩阵快速幂 + 二分)

POJ 3233 - Matrix Power Series ( 矩阵快速幂 + 二分) #include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef long long LL; #define MAX_SIZE 30 #define CLR( a, b ) memset( a, b, sizeof(a) ) int MOD = 0; int n, k; st

HDU 4990 Reading comprehension(找规律+矩阵快速幂)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4990 Problem Description Read the program below carefully then answer the question. #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include<iostream> #include

hdu 6198(矩阵快速幂)

number number number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 175    Accepted Submission(s): 119 暴力发现当4 12 33 88 232 和斐波那契数列对比  答案为 第2*k+3个数减1 直接用矩阵快速幂求的F[2*k+3]  然后减1 A=1,B=0; 然后矩阵快速幂2*k

矩阵快速幂 模板与简单讲解

模板 快速幂模板 1 void solve(matrix t,long long o) 2 { 3 matrix e; 4 5 memset(e.a,0,sizeof(e.a)); 6 7 for (int i = 0;i < d;i++) 8 e.a[i][i] = 1; 9 10 while (o) 11 { 12 if (o & 1) 13 { 14 e = mul(e,t); 15 } 16 17 o >>= 1; 18 19 t = mul(t,t); 20 } 21

233 Matrix 矩阵快速幂

In our daily life we often use 233 to express our feelings. Actually, we may say 2333, 23333, or 233333 ... in the same meaning. And here is the question: Suppose we have a matrix called 233 matrix. In the first line, it would be 233, 2333, 23333...

2017省夏令营Day7 【快速幂,筛法,矩阵快速幂,线段树】

题解:首先,我们可以得到一个规律:经过2次变换后,a和b的值都分别乘2了,所以只要用快速幂就能过啦,但是,要特判n为0的情况. 代码如下: 1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 #define Mod 1000000007 5 using namespace std; 6 long long a,b,n,ans1,ans2; 7 long long power(long long x)