QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#544473#7612. Matrix InverseyqrTL 12ms57156kbC++203.4kb2024-09-02 17:05:442024-09-02 17:05:44

Judging History

你现在查看的是最新测评结果

  • [2024-09-02 17:05:44]
  • 评测
  • 测评结果:TL
  • 用时:12ms
  • 内存:57156kb
  • [2024-09-02 17:05:44]
  • 提交

answer

#include<stdio.h>
#include<ctype.h>
#include<random>
#include<time.h>
#include<vector>
namespace IO {
	constexpr int bufsize = 230005;
	char buf[bufsize], *f1, *f2;
	#define gtchar() (f1 == f2 && (f2 = buf + fread(f1 = buf, 1, bufsize, stdin)) == buf? EOF: *f1++)
	template<typename t> void read(t &ret)
	{
		int f = ret = 0;
		char ch = gtchar();
		while(!isdigit(ch)) f = ch == '-', ch = gtchar();
		while(isdigit(ch)) ret = (ret << 3) + (ret << 1) + (ch ^ 48), ch = gtchar();
		if(f) ret = -ret;
	}
	#undef gtchar
	template<typename t, typename ...T> void read(t &a, T &...b) {read(a), read(b...);}
}using IO::read;
typedef long long ll;
constexpr int maxn = 2005, mod = 1e9 + 7;
int n, cx, cy, idxx[maxn], idxy[maxn], nx[15], ny[15], g[maxn][maxn];
void Add(int &a, int b) {if((a += b) >= mod) a -= mod;}
struct matrix {
	int x, y, c[maxn][maxn];
	void init(int _x, int _y)
	{
		x = _x, y = _y;
		for(int i = 1; i <= x; ++i)
			for(int j = 1; j <= y; ++j)
				c[i][j] = 0;
	}
	friend matrix operator * (const matrix &a, const matrix &b)
	{
		matrix ret;
		ret.init(a.x, b.y);
		for(int i = 1; i <= a.x; ++i)
			for(int j = 1; j <= a.y; ++j)
				for(int k = 1; k <= b.y; ++k)
					Add(ret.c[i][k], (ll) a.c[i][j] * b.c[j][k] % mod);
		return ret;
	}
}A, B, C, D;
std::mt19937 rnd(std::random_device{}() ^ time(0));
typedef std::pair<int, int> pii;
ll qpow(ll a, int b)
{
	ll ret = 1;
	while(b)
	{
		if(b & 1) ret = ret * a % mod;
		a = a * a % mod;
		b >>= 1;
	}
	return ret;
}
void swap(int &a, int &b) {a ^= b ^= a ^= b;}
void gauss(int m)
{
	int line = 1;
	for(int i = 1; i <= m; i++)
	{
		int cur = line;
		for(int j = line + 1; j <= n; j++) if(g[j][i] > g[cur][i]) cur = j;
		// if(!g[cur][i]) continue;
		if(line != cur) for(int j = i; j <= m + 1; j++) swap(g[line][j], g[cur][j]);
		ll inv = qpow(g[line][i], mod - 2);
		for(int j = i; j <= m + 1; j++) g[line][j] = g[line][j] * inv % mod;
		for(int j = 1; j <= n; j++) if(j != line && g[j][i])
		{
			ll base = g[j][i];
			g[j][i] = 0;
			for(int k = i + 1; k <= m + 1; k++) Add(g[j][k], mod - base * g[line][k] % mod);
		}
		++line;
	}
}
int main()
{
	// freopen(".in", "r", stdin);
	// freopen(".out", "w", stdout);
	read(n);
	for(int i = 1; i <= n; ++i) for(int j = 1; j <= n; ++j) read(A.c[i][j]);
	for(int i = 1; i <= n; ++i) for(int j = 1; j <= n; ++j) read(B.c[i][j]);
	//find the illegal columns
	C.init(1, n);
	for(int i = 1; i <= n; i++) C.c[1][i] = rnd();
	D = C * A * B;
	for(int i = 1; i <= n; i++) if(D.c[1][i] != C.c[1][i]) ny[idxy[i] = ++cy] = i;

	//find the illegal rows
	C.init(n, 1);
	for(int i = 1; i <= n; i++) C.c[i][1] = rnd();
	D = B * A * C;
	for(int i = 1; i <= n; i++) if(D.c[i][1] != C.c[i][1]) nx[idxx[i] = ++cx] = i;
	std::vector<std::pair<pii, int> > ans;
	for(int i = 1; i <= cy; i++)
	{
		int ii = ny[i];
		for(int j = 1; j <= n; j++)
		{
			int val = 0;//常数项
			for(int k = 1; k <= n; k++)
			{
				if(idxx[k]) g[j][idxx[k]] = A.c[j][k];
				else Add(val, (ll) A.c[j][k] * B.c[k][ii] % mod);
			}
			val = val? mod - val: 0;
			if(j == ii) Add(val, 1);
			g[j][cx + 1] = val;
		}
		gauss(cx);
		for(int j = 1; j <= cx; j++) if(B.c[nx[j]][ii] != g[j][cx + 1])
			ans.push_back({{nx[j], ii}, g[j][cx + 1]});
	}
	printf("%llu\n", ans.size());
	for(auto t : ans) printf("%d %d %d\n", t.first.first, t.first.second, t.second);
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 12ms
memory: 57156kb

input:

1
953176428
107682094

output:

0

result:

ok single line: '0'

Test #2:

score: -100
Time Limit Exceeded

input:

1995
586309310 548144807 578573993 437893403 641164340 712256053 172321263 108058526 768610920 123320669 762746291 856047593 979279376 29067913 309867338 292286426 45124325 239705174 675003623 213743652 620561338 116308277 695369179 669459894 682522334 846995555 159510341 999359657 645579085 7499563...

output:


result: