HDU 2256 Problem of Precision

题目分析

这道题跟我之前发的一道题几乎一样,这里我就不给出证明过程了,直接上代码。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
const int MOD = 1024;

struct Matirx{
    int r,c;
    LL matirx[2][2];
};

Matirx one, start, another;

Matirx Mul(Matirx a, Matirx b){
    Matirx c;
    c.r = a.r;
    c.c = b.c;
    for(int i = 0; i < a.r; i++){
        for(int j = 0; j < b.c; j++){
            c.matirx[i][j] = 0;
            for(int k = 0; k < a.c; k++)
                c.matirx[i][j] = (c.matirx[i][j] + a.matirx[i][k]*b.matirx[k][j])%MOD;
        }
    }
    return c;
}

Matirx Quick_pow(int n){
    Matirx ret = one, temp = start;
    while(n){
        if(n&1) ret = Mul(ret, temp);
        temp = Mul(temp, temp);
        n >>= 1;
    }
    return ret;
}

void init(){
    one.r = one.c = start.c = start.r = 2;
    one.matirx[0][0] = 1;
    one.matirx[0][1] = 0;
    one.matirx[1][0] = 0;
    one.matirx[1][1] = 1;
    start.matirx[0][0] = 0;
    start.matirx[0][1] = 1;
    start.matirx[1][0] = -1;
    start.matirx[1][1] = 10;
    another.r = 2;
    another.c = 1;
    another.matirx[0][0] = 10;
    another.matirx[1][0] = 98;
}

int main(){
    int T,n;
    scanf("%d", &T);
    init();
    while(T--){
        scanf("%d", &n);
        if(n == 1) printf("9\n");
        else if(n == 2) printf("97\n");
        else {
            Matirx ret = Quick_pow(n-2);
            Matirx ans = Mul(ret, another);
            printf("%I64d\n", (ans.matirx[1][0]-1+MOD)%MOD);
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值