hdu 1757 矩陣連乘
http://acm.hdu.edu.cn/showproblem.php?pid=1757
Problem Description
Lele now is thinking about a simple function f(x).
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 .
Now, I will give a0 ~ a9 and two positive integers k and m ,and could you help Lele to caculate f(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 .
Now, I will give a0 ~ a9 and two positive integers k and m ,and could you help Lele to caculate f(k)%m.
Input
The problem contains mutiple test cases.Please process to the end of file.
In each case, there will be two lines.
In the first line , there are two positive integers k and m. ( k<2*10^9 , m < 10^5 )
In the second line , there are ten integers represent a0 ~ a9.
In each case, there will be two lines.
In the first line , there are two positive integers k and m. ( k<2*10^9 , m < 10^5 )
In the second line , there are ten integers represent a0 ~ a9.
Output
For each case, output f(k) % m in one line.
Sample Input
10 9999
1 1 1 1 1 1 1 1 1 1
20 500
1 0 1 0 1 0 1 0 1 0
Sample Output
45
104
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <math.h>
using namespace std;
typedef long long LL;
const int N=10;
LL MOD,n;
int a[10];
struct Matrix
{
LL m[N][N];
};
Matrix I=
{
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,0,0,
0,0,0,0,0,0,0,0,1,0,
0,0,0,0,0,0,0,0,0,1
};
Matrix multi(Matrix a,Matrix b)
{
Matrix c;
for(int i=0; i<N; i++)
for(int j=0; j<N; j++)
{
c.m[i][j]=0;
for(int k=0; k<N; k++)
{
c.m[i][j]+=a.m[i][k]*b.m[k][j]%MOD;
}
c.m[i][j]=c.m[i][j]%MOD;
}
return c;
}
Matrix quick_mod(Matrix a,LL k)
{
Matrix ans=I;
while(k!=0)
{
if(k&1)
{
ans=multi(ans,a);
}
k>>=1;
a=multi(a,a);
}
return ans;
}
int main()
{
while(~scanf("%I64d%I64d",&n,&MOD))
{
for(int i=0;i<10;i++)
scanf("%d",&a[i]);
if(n<=9)
{
printf("%I64d\n",n);
continue;
}
Matrix A={a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
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,0,0,
0,0,0,0,0,0,0,0,1,0
};
Matrix P=quick_mod(A,n-9);
LL x=(P.m[0][0]*9%MOD+P.m[0][1]*8%MOD+P.m[0][2]*7%MOD+P.m[0][3]*6%MOD+P.m[0][4]*5%MOD+P.m[0][5]*4%MOD+P.m[0][6]*3%MOD+P.m[0][7]*2%MOD+P.m[0][8])%MOD;
if(x<0)
x+=MOD;
printf("%I64d\n",x);
}
return 0;
}
相關文章
- NYOJ 1409 快速計算【矩陣連乘】矩陣
- 矩陣連乘(動態規劃演算法)矩陣動態規劃演算法
- 矩陣連乘問題 Python 動態規劃矩陣Python動態規劃
- HDU 1005 Number Sequence(矩陣快速冪)矩陣
- HDU 2256Problem of Precision(矩陣快速冪)矩陣
- HDU 2157 How many ways?? (矩陣快速冪)矩陣
- HDU 2276 - Kiki & Little Kiki 2 (矩陣快速冪)矩陣
- 三維旋轉矩陣 左乘和右乘分析矩陣
- 【矩陣求導】關於點乘 (哈達瑪積)的矩陣求導矩陣求導點乘
- matlab矩陣連線圖解Matlab矩陣圖解
- bzoj4547: Hdu5171 小奇的集合(矩陣乘法)矩陣
- 大規模 Transformer 模型 8 位元矩陣乘簡介ORM模型矩陣
- 巨大的矩陣(矩陣加速)矩陣
- 鄰接矩陣、度矩陣矩陣
- 奇異矩陣,非奇異矩陣,偽逆矩陣矩陣
- 三維空間變換中旋轉矩陣左乘與右乘的區別矩陣
- 資料結構:陣列,稀疏矩陣,矩陣的壓縮。應用:矩陣的轉置,矩陣相乘資料結構陣列矩陣
- 矩陣矩陣
- 動態規劃_備忘錄法_矩陣鏈乘問題動態規劃矩陣
- 求任意矩陣的伴隨矩陣矩陣
- HDU 4549 M斐波那契數列(矩陣快速冪+費馬小定理)矩陣
- 矩陣類及其常規運算(加、減、乘、轉置、求逆、行列式、代數餘子式、伴隨矩陣)矩陣
- 矩陣和陣列矩陣陣列
- 理解矩陣矩陣
- 海浪矩陣矩陣
- 矩陣相乘矩陣
- 稀疏矩陣矩陣
- 螺旋矩陣矩陣
- 矩陣乘法矩陣
- 8.6 矩陣?矩陣
- 找矩陣矩陣
- 矩陣分解矩陣
- 快手矩陣管理平臺,矩陣管理有方法矩陣
- 機器學習中的矩陣向量求導(五) 矩陣對矩陣的求導機器學習矩陣求導
- 矩陣:如何使用矩陣操作進行 PageRank 計算?矩陣
- 演算法學習:矩陣快速冪/矩陣加速演算法矩陣
- hdu 1175 連連看 搜尋
- 高斯消除矩陣矩陣
- 矩陣求導矩陣求導