求助,为啥不能交换乘法的顺序
查看原帖
求助,为啥不能交换乘法的顺序
105820
阿尔托莉雅丶楼主2021/10/19 17:53

RT

#include <iostream>
#include <algorithm>
#include <math.h>
#include <vector>
#include <cstdio>
#include <cstring>
using namespace std;
const int N = 2e5 + 5;   //remember to modify the range of the data!!
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
typedef long long ll;

ll n, m, t;

struct matrix 
{
    ll a[3][3];

    matrix operator * (const matrix &b) const
    {
        matrix res;
        
        for(int i = 1; i <= 2; i++)
            for(int j = 1; j <= 2; j++)
            {
                res.a[i][j] = 0;
                for(int k = 1; k <= 2; k++)
                    res.a[i][j] = (res.a[i][j] + (a[i][k] * b.a[k][j]) % mod) % mod;
            }
        return res;
    }
} a;

void qpow(matrix & a, ll p)
{
    matrix b = a;
    a.a[1][2] = 0;
    while(p)
    {
        if(p & 1)
                a = b * a; //a = a * b会wa
        b = b * b;
        p >>= 1;
    }
}

int main(void)
{
    a.a[1][1] = 1;
    a.a[1][2] = 1;
    a.a[2][1] = 1;
    a.a[2][2] = 0;

    cin >> n;
    if(n <= 2)
    {
        cout << 1;
        return 0;
    }  

    

    qpow(a, n - 2);


    cout << (a.a[1][1]) % mod;


    return 0;
}
2021/10/19 17:53
加载中...