【矩阵快速幂+输入终止条件判断】Contemplation! Algebra UVA - 10655

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值