LOJ1070(SummerTrainingDay05-B 矩阵快速幂)

时间:2023-03-09 16:44:56
LOJ1070(SummerTrainingDay05-B 矩阵快速幂)

Algebraic Problem

Given the value of a+b and ab you will have to find the value of an+bna and bnot necessarily have to be real numbers.

Input

Input starts with an integer T (≤ 10000), denoting the number of test cases.

Each case contains three non-negative integers, p, q and n. Here p denotes the value of a+b and q denotes the value of ab. Each number in the input file fits in a signed 32-bit integer. There will be no such input so that you have to find the value of 00.

Output

For each test case, print the case number and (an+bn) modulo 264.

Sample Input

2

10 16 2

7 12 3

Sample Output

Case 1: 68

Case 2: 91

令Sn =  an+bn  ,则有递推公式Sn = p×Sn-1 - q×Sn-2 ,构造矩阵[[p, 1], [-q, 0]],初始矩阵为[S1, S0]。

 //2017-08-05
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define ll unsigned long long using namespace std; ll p, q;
const int N = ; struct Matrix{
ll a[N][N];
int r, c;
}ori, res; void init(){
memset(res.a, , sizeof(res.a));
res.r = ; res.c = ;
res.a[][] = p;
res.a[][] = ;
ori.r = ; ori.c = ;//构造矩阵
ori.a[][] = p;
ori.a[][] = ;
ori.a[][] = -q;
ori.a[][] = ;
} Matrix multi(Matrix x, Matrix y)//矩阵乘法
{
Matrix z;
memset(z.a, , sizeof(z.a));
z.r = x.r, z.c = y.c;
for(int i = ; i <= x.r; i++){
for(int k = ; k <= x.c; k++)//加速优化
{
if(x.a[i][k] == ) continue;
for(int j = ; j<= y.c; j++)
z.a[i][j] = (z.a[i][j] + (x.a[i][k] * y.a[k][j]));
}
}
return z;
} void Matrix_pow(int n)//矩阵快速幂
{
while(n){
if(n & )
res = multi(res, ori);
ori = multi(ori, ori);
n >>= ;
}
printf("%llu\n", res.a[][]);
} int main(){
int T, n;
scanf("%d", &T);
int kase = ;
while(T--){
scanf("%lld%lld%d", &p, &q, &n);
init();
printf("Case %d: ", ++kase);
if(n == )printf("2\n");
else if(n == )printf("%llu\n", p);
else Matrix_pow(n-);
} return ;
}