矩阵快速幂模板

矩阵快速幂

取模数加const!!!!!

const ll m = 123456789;
ll k;
int N = 6;
struct matrix
{
    ll  g[N][N];
    matrix operator=(const matrix &b)
    {
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < n; j++)
            {
                g[i][j] = b.g[i][j];
            }
        }
        return *this;
    }
}a,b;

inline matrix operator*(const matrix &a, const matrix &b)
{
    matrix c;
    memset(c.g, 0, sizeof(c.g));
    for (ll i = 0; i < N; i++)
        for (ll k = 0; k < N; k++)
        {
            if (a.g[i][k] == 0)
            {
                continue;
            }
            for (ll j = 0; j <N; j++)
                c.g[i][j] = (c.g[i][j] + a.g[i][k]*b.g[k][j] %m) % m;
        }
    return c;
}
inline matrix pow(matrix a, ll b)
{
    matrix ans;
    memset(ans.g, 0, sizeof(ans.g));
    for (int i = 0; i < N; i++)
    {
        ans.g[i][i] = 1;
    }
    while (b)
    {
        if (b & 1)
            ans = ans * a;
        a = a * a;
        b >>= 1;
    }
    return ans;
}

你可能感兴趣的:(Algorithm)