# hdu 4565 So Easy!

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

typedef long long LL;

struct Matrix
{
LL iMatrix[2][2];
};

Matrix iPer, iCell;
LL a, b, n, iMod;

void Inite_Matrix()
{
iPer.iMatrix[0][0] = iPer.iMatrix[1][1] = 1, iPer.iMatrix[0][1] = iPer.iMatrix[1][0] = 0;
iCell.iMatrix[0][0] = iCell.iMatrix[1][1] = a, iCell.iMatrix[0][1] = b, iCell.iMatrix[1][0] = 1;
}

Matrix Multi_Matrix(Matrix a, Matrix b)
{
Matrix c;
int i, j, k;
for(i = 0; i < 2; ++i)
{
for(j = 0; j < 2; ++j)
{
c.iMatrix[i][j] = 0;
for(k = 0; k < 2; ++k)
{
c.iMatrix[i][j] = (c.iMatrix[i][j]%iMod + ((a.iMatrix[i][k]%iMod) * (b.iMatrix[k][j]%iMod))%iMod)%iMod;
}
}
}
return c;
}

Matrix Quick_Mod_Matrix(LL n)
{
if(n == 1)
return iCell;
Matrix c, tmp;
tmp = iCell;
c = iPer;
while(n)
{
if(n&1)
{
c = Multi_Matrix(c, tmp);
n--;
}
n >>= 1;
tmp = Multi_Matrix(tmp, tmp);
}
return c;
}

int main()
{
Matrix c;
while(~scanf("%lld %lld %lld %lld", &a, &b, &n, &iMod))
{
Inite_Matrix();
c = Quick_Mod_Matrix(n);
printf("%lld\n", (2*(c.iMatrix[0][0]%iMod)%iMod));
}
return 0;
}


• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120