当给定p,q,r时,我们可以定义
对于给定的p,q,r,n,请计算
对于p=2 q=2 r=1 n=1这组数据,
所以答案是
Input
共1行,4个整数数p, q, r, n中间用空格分隔(1 <= p, q, r, n<=1000000000)。
Output
对于每一个数据,在一行中输出答案。
Input示例
2 2 1 1
Output示例
3
思路:
参考:
https://blog.csdn.net/qingshui23/article/details/72897467
#include <iostream>
#include <cstring>
using namespace std;
typedef long long ll;
const ll MOD = 1e9 + 7;
struct Node
{
Node()
{
memset(val, 0, sizeof(val));
}
ll val[3][3];
};
Node mul(Node &a, Node &b)
{
Node result;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
for (int k = 0; k < 3; k++)
{
result.val[i][j] = result.val[i][j] + (a.val[i][k] * b.val[k][j]) % MOD;
result.val[i][j] = (result.val[i][j] % MOD + MOD) % MOD;
}
}
}
return result;
}
Node q_pow(Node a, ll n)
{
Node result;
for (int i = 0; i < 3; i++)
{
result.val[i][i] = 1;
}
while (n)
{
if (n & 1)
{
result = mul(result, a);
}
n /= 2;
a = mul(a, a);
}
return result;
}
int main()
{
ll p, q, r, n;
cin >> p >> q >> r >> n;
Node result;
result.val[0][0] = q;
result.val[1][0] = (3*p) % MOD;
result.val[2][0] = 3;
result.val[1][1] = p;
result.val[2][1] = 1;
result.val[2][2] = 1;
result = q_pow(result, n);
result.val[2][0] = ((result.val[2][0] * r) % MOD + MOD) % MOD;
cout << result.val[2][0] << endl;
return 0;
}