记录编号 |
584894 |
评测结果 |
AAAAAAAAAA |
题目名称 |
[HZOI 2016]简单的AVL树 |
最终得分 |
100 |
用户昵称 |
┭┮﹏┭┮ |
是否通过 |
通过 |
代码语言 |
C++ |
运行时间 |
0.000 s |
提交时间 |
2023-11-16 20:05:36 |
内存使用 |
0.00 MiB |
显示代码纯文本
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
//矩阵快速幂
ll n,p;
struct Matrix{
ll a[3][3],n,m;
void clear(){n = m = 0;memset(a,0,sizeof(a));}
Matrix operator * (const Matrix &x)const{
Matrix y;y.clear();
y.n = n,y.m = m;
for(int i = 0;i < n;i++)
for(int j = 0;j < m;j++)
for(int k = 0;k < m;k++)
y.a[i][j] = (y.a[i][j] + (a[i][k] * x.a[k][j]) % p) % p;
return y;
}
}c,f;
int main(){
freopen("AVL.in","r",stdin);
freopen("AVL.out","w",stdout);
scanf("%lld%lld",&n,&p);
if(n == 1){printf("1\n");return 0;}
if(n == 2){printf("2\n");return 0;}
n -= 2;
f.n = 1,f.m = 3;
f.a[0][0] = 2,f.a[0][1] = 1,f.a[0][2] = 1;
c.n = 3,c.m = 3;
c.a[0][0] = 1,c.a[1][0] = 1,c.a[2][0] = 1,c.a[0][1] = 1,c.a[2][2] = 1;
while(n){
if(n & 1)f = f * c;
c = c * c;
n >>= 1;
}
printf("%lld\n",f.a[0][0]);
return 0;
}