注意:函数传数组传的是指针,并非copy一份,所以必须用一个tmp
数组
#include <bits/stdc++.h>
#define fastio() ios::sync_with_stdio(0),\
cin.tie(0),cout.tie(0);
#define lowbit(x) (x & -x)
#define endl '\n'
using namespace std;
typedef long long LL;
LL g[3][3];int n,m;
void mul(LL ans[3],LL vec[3],LL mat[3][3])
{
LL tmp[3] = {0};
for(int i = 0;i < 3;i ++ )
{
for(int j = 0;j < 3;j ++ )
tmp[i] = (tmp[i] + vec[j] * mat[j][i]) % m;
}
memcpy(ans,tmp,24);
}
void mul(LL ans[3][3],LL mat1[3][3],LL mat2[3][3])
{
LL tmp[3][3] = {0};
for(int i = 0;i < 3;i ++ )
for(int j = 0;j < 3;j ++ )
for(int k = 0;k < 3;k ++ )
tmp[i][j] = (tmp[i][j] + mat1[i][k] * mat2[k][j]) % m;
memcpy(ans,tmp,72);
}
void qmi(LL ans[3],LL mat[3][3],LL b)
{
while(b)
{
if(b & 1) mul(ans,ans,mat);
mul(mat,mat,mat);
b >>= 1;
}
}
int main()
{
fastio();
cin >> n >> m;
LL ans[3] = {1,1,1};
LL g[3][3] = {
{0,1,0},
{1,1,1},
{0,0,1},
};
qmi(ans,g,n - 1);
cout << ans[2] << endl;
return 0;
}