矩阵快速幂

typedef LL matrix[55][55];

void ident(int size, matrix x)
{
    for (int i = 0; i < size; ++i)
        for (int j = 0; j < size; ++j)
            x[i][j] = (i == j ? 1 : 0);
}
void copy(int size, matrix x, matrix y)
{
    for (int i = 0; i < size; ++i)
        for (int j = 0; j < size; ++j)
            y[i][j] = x[i][j];
}
void mutiply(int size, matrix _x, matrix _y, matrix z)
{
    matrix x, y;
    copy(size, _x, x);
    copy(size, _y, y);
    for (int i = 0; i < size; ++i)
        for (int j = 0; j < size; ++j) {
            z[i][j] = 0;
            for (int k = 0; k < size; ++k)
                z[i][j] = (z[i][j] + (int64)x[i][k] * y[k][j]) % M;
        }
}

void power(int size, matrix _x, LL n, matrix y)
{
    matrix x, r;
    copy(size, _x, x);
    ident(size, r);
    while (n) {
        if (n & 1) mutiply(size, r, x, r);
        n >>= 1;
        if (n == 0) break;
        mutiply(size, x, x, x);
    }
    copy(size, r, y);
}

你可能感兴趣的:(C++,c,C#,J#)