Think:
1知识点:矩阵快速幂+输入终止条件判断
2题意& 思路:
——参考博客链接 感谢博主
3反思:
(1):题目要去当输入数据仅为两个0时终止,而0 0 n属于正常输入,因此判断while()语句是否输入终止的条件为scanf(“%lld %lld %lld”, &p, &q, &n) == 3而不能为~scanf(“%lld %lld”, &p, &q) && (p || q)
以下为Wrong Answer代码——while()语句输入终止的条件判断错误
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
struct Matrix{
int row, col;
LL v[4][4];
};
Matrix multiply(Matrix a, Matrix b);
Matrix matrix_pow(Matrix x, LL k);
int main(){
LL p, q, n;
while(~scanf("%lld %lld", &p, &q) && (p || q)){
scanf("%lld", &n);
if(n == 0){
printf("2\n");
continue;
}
Matrix tmp;
tmp.row = 2, tmp.col = 2;
tmp.v[0][0] = p, tmp.v[0][1] = -q;
tmp.v[1][0] = 1, tmp.v[1][1] = 0;
tmp = matrix_pow(tmp, n-1);
LL ans = tmp.v[0][0]*p + (tmp.v[0][1]<<1);
printf("%lld\n", ans);
}
return 0;
}
Matrix multiply(Matrix a, Matrix b){
Matrix tmp;
tmp.row = a.row, tmp.col = b.col;
memset(tmp.v, 0, sizeof(tmp.v));
for(int i = 0; i < a.row; i++){
for(int j = 0; j < b.col; j++){
for(int k = 0; k < a.col; k++){
tmp.v[i][j] += (a.v[i][k]*b.v[k][j]);
}
}
}
return tmp;
}
Matrix matrix_pow(Matrix x, LL k){
Matrix ans;
ans.row = x.row, ans.col = x.col;
memset(ans.v, 0, sizeof(ans.v));
for(int i = 0; i < ans.row; i++)
ans.v[i][i] = 1;
while(k){
if(k & 1)
ans = multiply(ans, x);
x = multiply(x, x);
k >>= 1;
}
return ans;
}
以下为Wrong Answer代码——while()语句输入终止的条件判断错误
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
struct Matrix{
int row, col;
LL v[4][4];
};
Matrix multiply(Matrix a, Matrix b);
Matrix matrix_pow(Matrix x, LL k);
int main(){
LL p, q, n;
while(scanf("%lld %lld %lld", &p, &q, &n) == 3 && p+q){;
if(n == 0){
printf("2\n");
continue;
}
Matrix tmp;
tmp.row = 2, tmp.col = 2;
tmp.v[0][0] = p, tmp.v[0][1] = -q;
tmp.v[1][0] = 1, tmp.v[1][1] = 0;
tmp = matrix_pow(tmp, n-1);
LL ans = tmp.v[0][0]*p + (tmp.v[0][1]<<1);
printf("%lld\n", ans);
}
return 0;
}
Matrix multiply(Matrix a, Matrix b){
Matrix tmp;
tmp.row = a.row, tmp.col = b.col;
memset(tmp.v, 0, sizeof(tmp.v));
for(int i = 0; i < a.row; i++){
for(int j = 0; j < b.col; j++){
for(int k = 0; k < a.col; k++){
tmp.v[i][j] += (a.v[i][k]*b.v[k][j]);
}
}
}
return tmp;
}
Matrix matrix_pow(Matrix x, LL k){
Matrix ans;
ans.row = x.row, ans.col = x.col;
memset(ans.v, 0, sizeof(ans.v));
for(int i = 0; i < ans.row; i++)
ans.v[i][i] = 1;
while(k){
if(k & 1)
ans = multiply(ans, x);
x = multiply(x, x);
k >>= 1;
}
return ans;
}
以下为Accepted代码
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
struct Matrix{
int row, col;
LL v[4][4];
};
Matrix multiply(Matrix a, Matrix b);
Matrix matrix_pow(Matrix x, LL k);
int main(){
LL p, q, n;
while(scanf("%lld %lld %lld", &p, &q, &n) == 3){;
if(n == 0){
printf("2\n");
continue;
}
Matrix tmp;
tmp.row = 2, tmp.col = 2;
tmp.v[0][0] = p, tmp.v[0][1] = -q;
tmp.v[1][0] = 1, tmp.v[1][1] = 0;
tmp = matrix_pow(tmp, n-1);
LL ans = tmp.v[0][0]*p + (tmp.v[0][1]<<1);
printf("%lld\n", ans);
}
return 0;
}
Matrix multiply(Matrix a, Matrix b){
Matrix tmp;
tmp.row = a.row, tmp.col = b.col;
memset(tmp.v, 0, sizeof(tmp.v));
for(int i = 0; i < a.row; i++){
for(int j = 0; j < b.col; j++){
for(int k = 0; k < a.col; k++){
tmp.v[i][j] += (a.v[i][k]*b.v[k][j]);
}
}
}
return tmp;
}
Matrix matrix_pow(Matrix x, LL k){
Matrix ans;
ans.row = x.row, ans.col = x.col;
memset(ans.v, 0, sizeof(ans.v));
for(int i = 0; i < ans.row; i++)
ans.v[i][i] = 1;
while(k){
if(k & 1)
ans = multiply(ans, x);
x = multiply(x, x);
k >>= 1;
}
return ans;
}