hdu4565 矩阵快速幂

#include <cstdio>
#include <iostream>
#include <fstream>
#include <algorithm>
#include <cstring>
using namespace std;

#define ll long long
#define ull unsigned long long

struct Matrix {
    ull v[2][2];
};

Matrix A, B={1L,0L,0L,1L};//E Matrix
ull M;

Matrix mul(Matrix m1,Matrix m2,ll M) {
    int i,j,k;
    Matrix c;
    for (i=0; i<2;i++)
    for (j=0; j<2;j++) {
        c.v[i][j]=0;
        for (k=0; k<2;k++) {
            c.v[i][j]+=(m1.v[i][k]*m2.v[k][j]) %M;
            c.v[i][j] %=M;
        }
        c.v[i][j] %=M;
    }
    return c;
}

Matrix Mpow(Matrix A, Matrix B, ll n, ll M) {
    Matrix x= A, c = B;
    // great idea
    while (n>=1) {
        if (n&1L) c = mul(c,x,M);
        n>>=1;
        x=mul(x,x,M);
    }
    return c;
}

int main()
{
    freopen("1.in","r",stdin);

    ll a, b, n, t;
    while (cin>>a>>b>>n>>M) {
        if (n==1) {
            cout<<(2*(a%M))%M<<endl;
            continue;
        }
        if (n==2) {
            cout<<((2*((a%M)*(a%M))%M)%M+(2*b)%M)%M<<endl;
            continue;
        }

        A.v[0][0] = (2*(a%M)) %M;
        A.v[0][1] = 1;
        t=b-a*a;
        while (t<0) t+=M;
        A.v[1][0] = t;
        A.v[1][1] = 0;
        Matrix p = Mpow(A,B,n-2,M), q={((2*((a%M)*(a%M))%M)%M+(2*b)%M)%M,
            (2*(a%M))%M,0L,0L};

        p = mul(q,p,M);//zuo cheng
        cout<<p.v[0][0]<<endl;
    }

    return 0;
}

cout 和 printf 在输出longlong时有区别?

用cout就对,printf就错了。。

神奇的题目。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值