比赛 |
201712练习 |
评测结果 |
AAAAAAAAAA |
题目名称 |
外星语言 |
最终得分 |
100 |
用户昵称 |
胡嘉兴 |
运行时间 |
0.003 s |
代码语言 |
C++ |
内存使用 |
0.31 MiB |
提交时间 |
2017-12-24 22:14:36 |
显示代码纯文本
#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll p, q, n, m;
struct matrix
{
ll a[4][4];
};
matrix A, ans;
void init(int k)
{
memset(A.a, 0, sizeof(A.a));
memset(ans.a, 0, sizeof(ans.a));
A.a[1][1] = A.a[1][2] = A.a[2][2] = k%m;
A.a[1][3] = A.a[2][3] = A.a[3][3] = 1;
ans.a[1][1] = ans.a[2][1] = ans.a[3][1] = 1;
return;
}
matrix mul(matrix c, matrix b)
{
matrix rel;
memset(rel.a, 0, sizeof(rel.a));
for(int i = 1; i < 4; i++)
{
for(int j = 1; j < 4; j++)
{
for(int k = 1; k < 4; k++)
{
rel.a[i][j] += c.a[i][k]*b.a[k][j];
}
rel.a[i][j] %= m;
}
}
return rel;
}
ll qpow(ll k)
{
while(k)
{
if(k&1)
{
ans = mul(A, ans);
}
A = mul(A, A);
k >>= 1;
}
return ans.a[1][1];
}
int main()
{
ll f, g;
freopen("alienlanguage.in","r",stdin);
freopen("alienlanguage.out","w",stdout);
scanf("%lld%lld%lld%lld", &p, &q, &n, &m);
init(p);
f = qpow(n);
init(q);
g = qpow(n);
printf("%lld\n", (f*g-1+m)%m);
return 0;
}