【矩阵快速幂】封装类及测试用例及样例
给定模质数 p p p 域上的 k k k 阶非奇异矩阵列 a a a,给定 q q q 次询问,每次给出 l , r l, r l,r,求 ∏ i = l r a i \prod \limits_{i = l}^r a_i i=l∏rai。 p = 1145141 p = 1145141 p=1145141。
注:模 p p p 域上的非奇异矩阵指:矩阵乘法加法均在模 p p p 下进行,矩阵(在实数域下)的行列式值对 p p p 取余不为 0 0 0。
输入第一行有三个数,依次表示矩阵列长度 n n n、矩阵阶数 k k k 以及询问数 q q q。
接下来 n × k n \times k n×k 行,每行 k k k 个整数,依次表示 n n n 个 k k k 阶矩阵,详见样例。
接下来 q q q 行,每行两个整数 l , r l, r l,r,表示一次询问。
为了避免输出过大,请输出一行一个整数,表示所有询问的答案的所有矩阵元素的按位异或和。
3 3 3
2 2 3
4 5 6
7 8 9
2 2 3
4 5 6
7 8 9
20 20 21
22 23 24
25 26 27
1 2
2 3
1 3
14921
a 1 = ( 2 2 3 4 5 6 7 8 9 ) a_1 = \begin{pmatrix} 2 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9\end{pmatrix} a1= 247258369 , a 2 = ( 2 2 3 4 5 6 7 8 9 ) a_2 = \begin{pmatrix} 2 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9\end{pmatrix} a2= 247258369 , a 3 = ( 20 20 21 22 23 24 25 26 27 ) a_3 = \begin{pmatrix} 20 & 20 & 21 \\ 22 & 23 & 24 \\ 25 & 26 & 27\end{pmatrix} a3= 202225202326212427 。
a 1 × a 2 = ( 33 38 45 70 81 96 109 126 150 ) a_1 \times a_2 = \begin{pmatrix} 33 & 38 & 45 \\ 70 & 81 & 96 \\ 109 & 126 &150 \end{pmatrix} a1×a2= 337010938811264596150 , a 2 × a 3 = ( 159 164 171 340 351 366 541 558 582 ) a_2 \times a_3 = \begin{pmatrix}159 & 164 & 171 \\ 340 & 351 & 366 \\ 541 & 558& 582 \end{pmatrix} a2×a3= 159340541164351558171366582 , a 1 × a 2 × a 3 = ( 2621 2704 2820 5582 5759 6006 8702 8978 9363 ) a_1 \times a_2 \times a_3 = \begin{pmatrix}2621 &2704& 2820 \\ 5582 & 5759 & 6006 \\ 8702 & 8978 & 9363 \end{pmatrix} a1×a2×a3= 262155828702270457598978282060069363 。
所有数字的按位异或和为 14921 14921 14921。
对于全部的测试点,保证 1 ≤ n , q ≤ 1 0 6 1 \leq n, q \leq 10^6 1≤n,q≤106, 2 ≤ k ≤ 3 2 \leq k \leq 3 2≤k≤3, 1 ≤ l ≤ r ≤ n 1 \leq l \leq r \leq n 1≤l≤r≤n,矩阵元素均为小于 p p p 的正整数。
A*B=C
性质一:如果B的第c1列乘以x,则C的此列也乘以x。证明:
令f(j)=a[r][j]b[j][c] c[r][c] = ∑ j f ( j ) \sum_jf(j) ∑jf(j)
如果c ≠ \neq = c1,则c[r][c]不变;否则所有f(j)都乘以x。
性质二:如果B的第c1列减去第c2列,则C也变成第c1列减去c2列。
新C的第c1列的f(j) = a[r][j](b[j][c1]-b[j][c2])
即原旧C的c1列减c2列。
性质三:如果A的某行全部是0,则C的此行一定全为0,故C无论如何都不会是单位矩阵。即A无逆矩阵。如果遇到全0行,改成任意非全0行,在结果的对应行改成全0。
高斯消元法流程:
初始B为单位矩阵,C=A。C成为单位矩阵时,B就是逆矩阵。
一,先将主对角线变换成1,不考虑非对角线单格。
a,如果(r,r)是0,选择任意C[r][c]$\neq$0,BC都执行r列+c列。
b,如果C[r][r]不是1,r列除以C[r][c]。
以上操作不影响主对角线的其它元素。
二,r = 0 to R-1 ,c = 0 to C-1处理非主对角线的单格。
a,如果C[r][c]等于0,则无需处理。
b,如果C[r][c]不为1,整列除以C[r][c]。
c,c列-r列。由于r列的前r行都是0,故不会影响c类的第[0…r-1]行。
时间复杂度:O(nnn)
二阶三阶的矩阵,伴随矩阵快些。因为只做一次除法。
注意:本题任意运行过程中,不会产生奇异矩阵,故一定存在可逆矩阵。
性质一:单位矩形 I − n I-n I−n,n阶方阵,主对角线1,其它0。XI=X,IX=X。下面以AB=C为例。
c[r][c] = ∑ j : 0 n − 1 ( a [ r ] [ j ] ∗ b [ j ] [ c ] ) \sum_{j:0}^{n-1}(a[r][j]*b[j][c]) ∑j:0n−1(a[r][j]∗b[j][c])
B是单位矩阵时:b[j][c],当j== c时为1,其它为0,故c[r][c]=a[r][c]。
A是单位矩阵时:a[r][j],当j==r 时为1,其它为0,故c[r][c]=b[r][c]。
性质二:AB等于(BA)的转置矩阵,单位矩阵的转置矩阵是本身。故AB=单元矩阵,则BA也等于单元矩阵。
性质三:矩阵的乘法具有结合律,不具备分配率和交换律。结合律:ABC = A(BC)。图像的仿射变换(平移、缩放)就是通过矩阵实现的。A是图像,BCD是仿射矩阵,ABCD等于A(BCD)。
CD = (AB)-1(AB)CD
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
template<class T1, class T2>
std::istream& operator >> (std::istream& in, pair<T1, T2>& pr) {
in >> pr.first >> pr.second;
return in;
}
template<class T1, class T2, class T3 >
std::istream& operator >> (std::istream& in, tuple<T1, T2, T3>& t) {
in >> get<0>(t) >> get<1>(t) >> get<2>(t);
return in;
}
template<class T1, class T2, class T3, class T4 >
std::istream& operator >> (std::istream& in, tuple<T1, T2, T3, T4>& t) {
in >> get<0>(t) >> get<1>(t) >> get<2>(t) >> get<3>(t);
return in;
}
template<class T = int>
vector<T> Read() {
int n;
scanf("%d", &n);
vector<T> ret(n);
for (int i = 0; i < n; i++) {
cin >> ret[i];
}
return ret;
}
template<class T = int>
vector<T> Read(int n) {
vector<T> ret(n);
for (int i = 0; i < n; i++) {
cin >> ret[i];
}
return ret;
}
template<int N = 1'000'000>
class COutBuff
{
public:
COutBuff() {
m_p = puffer;
}
template<class T>
void write(T x) {
int num[28], sp = 0;
if (x < 0)
*m_p++ = '-', x = -x;
if (!x)
*m_p++ = 48;
while (x)
num[++sp] = x % 10, x /= 10;
while (sp)
*m_p++ = num[sp--] + 48;
AuotToFile();
}
void writestr(const char* sz) {
strcpy(m_p, sz);
m_p += strlen(sz);
AuotToFile();
}
inline void write(char ch)
{
*m_p++ = ch;
AuotToFile();
}
inline void ToFile() {
fwrite(puffer, 1, m_p - puffer, stdout);
m_p = puffer;
}
~COutBuff() {
ToFile();
}
private:
inline void AuotToFile() {
if (m_p - puffer > N - 100) {
ToFile();
}
}
char puffer[N], * m_p;
};
template<int N = 1'000'000>
class CInBuff
{
public:
inline CInBuff() {}
inline CInBuff<N>& operator>>(char& ch) {
FileToBuf();
ch = *S++;
return *this;
}
inline CInBuff<N>& operator>>(int& val) {
FileToBuf();
int x(0), f(0);
while (!isdigit(*S))
f |= (*S++ == '-');
while (isdigit(*S))
x = (x << 1) + (x << 3) + (*S++ ^ 48);
val = f ? -x : x; S++;//忽略空格换行
return *this;
}
inline CInBuff& operator>>(long long& val) {
FileToBuf();
long long x(0); int f(0);
while (!isdigit(*S))
f |= (*S++ == '-');
while (isdigit(*S))
x = (x << 1) + (x << 3) + (*S++ ^ 48);
val = f ? -x : x; S++;//忽略空格换行
return *this;
}
template<class T1, class T2>
inline CInBuff& operator>>(pair<T1, T2>& val) {
*this >> val.first >> val.second;
return *this;
}
template<class T1, class T2, class T3>
inline CInBuff& operator>>(tuple<T1, T2, T3>& val) {
*this >> get<0>(val) >> get<1>(val) >> get<2>(val);
return *this;
}
template<class T1, class T2, class T3, class T4>
inline CInBuff& operator>>(tuple<T1, T2, T3, T4>& val) {
*this >> get<0>(val) >> get<1>(val) >> get<2>(val) >> get<3>(val);
return *this;
}
template<class T = int>
inline CInBuff& operator>>(vector<T>& val) {
int n;
*this >> n;
val.resize(n);
for (int i = 0; i < n; i++) {
*this >> val[i];
}
return *this;
}
template<class T = int>
vector<T> Read(int n) {
vector<T> ret(n);
for (int i = 0; i < n; i++) {
*this >> ret[i];
}
return ret;
}
template<class T = int>
vector<T> Read() {
vector<T> ret;
*this >> ret;
return ret;
}
private:
inline void FileToBuf() {
const int canRead = m_iWritePos - (S - buffer);
if (canRead >= 100) { return; }
if (m_bFinish) { return; }
for (int i = 0; i < canRead; i++)
{
buffer[i] = S[i];//memcpy出错
}
m_iWritePos = canRead;
buffer[m_iWritePos] = 0;
S = buffer;
int readCnt = fread(buffer + m_iWritePos, 1, N - m_iWritePos, stdin);
if (readCnt <= 0) { m_bFinish = true; return; }
m_iWritePos += readCnt;
buffer[m_iWritePos] = 0;
S = buffer;
}
int m_iWritePos = 0; bool m_bFinish = false;
char buffer[N + 10], * S = buffer;
};
class CMatMul
{
public:
CMatMul(long long llMod = 1e9 + 7) :m_llMod(llMod) {}
// 矩阵乘法
vector<vector<long long>> multiply(const vector<vector<long long>>& a, const vector<vector<long long>>& b) {
const int r = a.size(), c = b.front().size(), iK = a.front().size();
assert(iK == b.size());
vector<vector<long long>> ret(r, vector<long long>(c));
for (int i = 0; i < r; i++)
{
for (int j = 0; j < c; j++)
{
for (int k = 0; k < iK; k++)
{
ret[i][j] = (ret[i][j] + a[i][k] * b[k][j]) % m_llMod;
}
}
}
return ret;
}
// 矩阵快速幂
vector<vector<long long>> pow(const vector<vector<long long>>& a, vector<vector<long long>> b, long long n) {
vector<vector<long long>> res = a;
for (; n; n /= 2) {
if (n % 2) {
res = multiply(res, b);
}
b = multiply(b, b);
}
return res;
}
vector<vector<long long>> TotalRow(const vector<vector<long long>>& a)
{
vector<vector<long long>> b(a.front().size(), vector<long long>(1, 1));
return multiply(a, b);
}
protected:
const long long m_llMod;
};
template<int MOD = 1000000007>
class C1097Int
{
public:
C1097Int(int iData = 0) :m_iData(iData% MOD)
{
}
C1097Int(long long llData) :m_iData(llData% MOD) {
}
C1097Int operator+(const C1097Int& o)const
{
return C1097Int((m_iData + o.m_iData) % MOD);
}
C1097Int& operator+=(const C1097Int& o)
{
m_iData = (m_iData + o.m_iData) % MOD;
return *this;
}
C1097Int& operator-=(const C1097Int& o)
{
m_iData = (m_iData - o.m_iData) % MOD;
return *this;
}
C1097Int operator-(const C1097Int& o)
{
return C1097Int((m_iData - o.m_iData) % MOD);
}
C1097Int operator*(const C1097Int& o)const
{
return((long long)m_iData * o.m_iData) % MOD;
}
C1097Int& operator*=(const C1097Int& o)
{
m_iData = ((long long)m_iData * o.m_iData) % MOD;
return *this;
}
C1097Int operator/(const C1097Int& o)const
{
return *this * o.PowNegative1();
}
C1097Int& operator/=(const C1097Int& o)
{
*this /= o.PowNegative1();
return *this;
}
bool operator==(const C1097Int& o)const
{
return m_iData == o.m_iData;
}
bool operator<(const C1097Int& o)const
{
return m_iData < o.m_iData;
}
C1097Int pow(long long n)const
{
C1097Int iRet = 1, iCur = *this;
while (n)
{
if (n & 1)
{
iRet *= iCur;
}
iCur *= iCur;
n >>= 1;
}
return iRet;
}
C1097Int PowNegative1()const
{
return pow(MOD - 2);
}
int ToInt()const
{
return (m_iData + MOD) % MOD;
}
private:
int m_iData = 0;;
};
//伴随矩阵求逆,只需一次除法,似乎只能处理3阶4阶
template<int MOD>
class CInverseMatrixAdjoint {
public:
CInverseMatrixAdjoint(int N) :m_iN(N), m_temp(2, vector<long long>(2)) {}
vector<vector<long long>> InverseMatrix(vector<vector<long long>>& mat)
{
const long long det = Determinant(mat, m_iN);
const auto div = C1097Int<MOD>(det).PowNegative1();
vector<vector<long long>> ret(m_iN, vector<long long>(m_iN));
for (int r = 0; r < m_iN; r++) {
for (int c = 0; c < m_iN; c++) {
Copy(mat, m_iN, r, c);
int sign = ((r + c) & 1) ? -1 : 1;
ret[c][r] = (div * Determinant(m_temp, m_iN - 1) * sign).ToInt();//AB=E,求B
}
}
return ret;
}
const int m_iN;
protected:
long long Determinant(const vector<vector<long long>>& mat, int n) {
if (1 == n) { return mat[0][0]; }
if (2 == n) {
long long ans = (mat[0][0] * mat[1][1] - mat[0][1] * mat[1][0]) % MOD;;
if (ans < 0) { ans += MOD; }
return ans;
}
long long ans = 0;
for (int c = 0; c < n; c++) {
long long cur1 = 1, cur2 = 1;//cur1主对角线,cur2反对角线
for (int r = 0, c1 = c, c2 = c, cnt = 0; cnt < n; cnt++, r++, c1++, c2--) {
if (c1 >= n) { c1 = 0; }
if (c2 < 0) { c2 = n - 1; }
cur1 = (cur1 * mat[r][c1]) % MOD;
cur2 = (cur2 * mat[r][c2]) % MOD;
}
ans += cur1; if (ans > MOD) { ans -= MOD; }
ans -= cur2; if (ans < 0) { ans += MOD; }
}
return ans;
}
void Copy(const vector<vector<long long>>& mat, int n, const int rRemove, const int cRemove) {
for (int r1 = 0, r = 0; r1 + 1 < n; r1++, r++) {
if (rRemove == r) { r++; }
for (int c1 = 0, c = 0; c1 + 1 < n; c1++, c++) {
if (cRemove == c) { c++; }
m_temp[r1][c1] = mat[r][c];
}
}
}
vector<vector<long long>> m_temp;
};
class Solution {
public:
int Ans(vector<vector<vector<long long>>>& mats, vector<pair<int, int>>& que) {
const int N = mats.size();
const int K = mats[0].size();
CMatMul matMul(1145141);
vector<vector<vector<long long>>> preSum(N + 1);
preSum[0].assign(K, vector<long long>(K));
for (int i = 0; i < K; i++) { preSum[0][i][i] = 1; }
for (int i = 1; i <= N; i++) {
preSum[i] = matMul.multiply(preSum[i - 1], mats[i - 1]);
}
int ans = 0;
for (const auto& [l, r] : que) {
auto invMat = CInverseMatrixAdjoint<1145141>(K).InverseMatrix(preSum[l - 1]);
auto ansMat = matMul.multiply(invMat, preSum[r]);
for (auto& v : ansMat) {
for (const auto& i : v) {
ans ^= i;
}
}
}
return ans;
}
};
int main() {
#ifdef _DEBUG
freopen("a.in", "r", stdin);
#endif // DEBUG
/*ios::sync_with_stdio(0); std::cin.tie(nullptr);
int n, m, k;
cin >> n >> k >> m;
vector>> mats(n, vector>(k));
for (int i = 0; i < n; i++) {
for (int j = 0; j < k; j++) {
mats[i][j] = Read(k);
}
}
auto que = Read>(m);*/
int n, m, k;
CInBuff ib;
ib >> n >> k >> m;
vector<vector<vector<long long>>> mats(n, vector<vector<long long>>(k));
for (int i = 0; i < n; i++) {
for (int j = 0; j < k; j++) {
mats[i][j] = ib.Read<long long>(k);
}
}
auto que = ib.Read<pair<int, int>>(m);
#ifdef _DEBUG
Out(mats, "mats=");
Out(que, ",que=");
/*printf("T=%lld,", T);
*/
/*Out(edge, "edge=");
Out(que, "que=");*/
#endif // DEBUG
auto res = Solution().Ans(mats, que);
cout << res;
return 0;
}
vector<vector<vector<long long>>> mats;
vector<pair<int, int>> que;
TEST_METHOD(TestMethod1)
{
mats = { {{2,2,3},{4,5,6},{7,8,9}},{{2,2,3},{4,5,6},{7,8,9}},{{20,20,21},{22,23,24}
,{25,26,27}} }, que = { {1,2},{2,3},{1,3} };
auto res = Solution().Ans(mats,que);
AssertEx(14921, res);
}
我想对大家说的话 |
---|
工作中遇到的问题,可以按类别查阅鄙人的算法文章,请点击《算法与数据汇总》。 |
学习算法:按章节学习《喜缺全书算法册》,大量的题目和测试用例,打包下载。重视操作 |
有效学习:明确的目标 及时的反馈 拉伸区(难度合适) 专注 |
闻缺陷则喜(喜缺)是一个美好的愿望,早发现问题,早修改问题,给老板节约钱。 |
子墨子言之:事无终始,无务多业。也就是我们常说的专业的人做专业的事。 |
如果程序是一条龙,那算法就是他的是睛 |
失败+反思=成功 成功+反思=成功 |
先学简单的课程,请移步CSDN学院,听白银讲师(也就是鄙人)的讲解。
https://edu.csdn.net/course/detail/38771
如何你想快速形成战斗了,为老板分忧,请学习C#入职培训、C++入职培训等课程
https://edu.csdn.net/lecturer/6176
操作系统:win7 开发环境: VS2019 C++17
或者 操作系统:win10 开发环境: VS2022 C++17
如无特殊说明,本算法用**C++**实现。