vector矩乘被卡常?
查看原帖
vector矩乘被卡常?
290023
369Pai楼主2023/1/14 10:42

我之前的矩阵乘法模板是这样的:

typedef vector<vector<int> >Mat;
Mat NewMat(int n , int m)
{
	Mat c; c.resize(n + 1);
	for(int i = 0 ; i <= n ; i++)c[i].resize(m + 1);
	c[0][0] = n , c[0][1] = m;
	return c;
}
Mat operator + (const Mat& a , const Mat& b);
Mat operator * (const Mat& a , const Mat& b);
Mat operator ^ (Mat x , int p);

然而在这道题被卡常,一直 TLE on test 18。

由于本题矩阵很小,怀疑 vector 在开空间时花费时间较多。

然后把 vector 改成了 array,就过了。

typedef array<array<int , 3> , 3> Mat;
Mat NewMat(int n , int m)
{
	Mat c;
	c[0] = {n , m , 0};
	c[1] = {0 , 0 , 0};
	c[2] = {0 , 0 , 0};
	return c;
}
Mat operator + (const Mat& a , const Mat& b);
Mat operator * (const Mat& a , const Mat& b);
Mat operator ^ (Mat x , int p);

但在【模板】矩阵快速幂 上这两种写法时间上并没有什么差别。

所以 vector 写矩阵乘法会被卡常吗?如果会,会在什么情况下?

2023/1/14 10:42
加载中...