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;
}
相關文章
- 矩陣連乘矩陣
- hdu2243 ac自動機+矩陣連乘矩陣
- 矩陣連乘問題矩陣
- 動態規劃&矩陣連乘動態規劃矩陣
- 矩陣連乘演算法精講矩陣演算法
- HDU 1588 斐波那契數列數列變形和矩陣連乘矩陣
- CUDA 版本矩陣乘矩陣
- NYOJ 1409 快速計算【矩陣連乘】矩陣
- 矩陣連乘(動態規劃演算法)矩陣動態規劃演算法
- 矩陣連乘問題 Python 動態規劃矩陣Python動態規劃
- poj 2778 AC自動機與矩陣連乘矩陣
- CUDA版本稀疏矩陣向量乘矩陣
- OpenMP 版本稀疏矩陣向量乘矩陣
- HDU4565 So Easy! (矩陣)矩陣
- HDU 1005 Number Sequence(矩陣)矩陣
- HDU 1575 Tr A(矩陣快速冪)矩陣
- HDU 4565 So Easy!(矩陣快速冪)矩陣
- HDU 2254 奧運(數論+矩陣)矩陣
- HDU 4686 (推公式+矩陣快速冪)公式矩陣
- HDU 4920 Matrix multiplication(矩陣相乘)矩陣
- 三維旋轉矩陣 左乘和右乘分析矩陣
- HDU 4686 Arc of Dream(矩陣加速遞推)矩陣
- HDU 4965 Fast Matrix Calculation(矩陣快速冪)AST矩陣
- 【矩陣求導】關於點乘 (哈達瑪積)的矩陣求導矩陣求導點乘
- HDU 3059 Fibonacci數列與矩陣求和 矩陣大小不固定矩陣
- 演算法題:矩陣鏈乘問題演算法矩陣
- HDU 2157 How many ways?? (矩陣快速冪)矩陣
- HDU 1005 Number Sequence(矩陣快速冪)矩陣
- HDU 2256Problem of Precision(矩陣快速冪)矩陣
- HDU 1575 Tr A【矩陣快速冪取模】矩陣
- HDU 1005 Number Sequence:矩陣快速冪矩陣
- HDU 4565 So Easy!(公式化簡+矩陣)公式矩陣
- HDU5411CRB and Puzzle(矩陣快速冪)矩陣
- HDU1588Gauss Fibonacci(矩陣)矩陣
- 演算法題系列:矩陣鏈乘問題演算法矩陣
- HDU 2276 - Kiki & Little Kiki 2 (矩陣快速冪)矩陣
- HDU 4291 A Short problem(矩陣快速冪+迴圈節)矩陣
- HDU 5318 The Goddess Of The Moon(遞推+矩陣優化)Go矩陣優化