結果
問題 |
No.3225 2×2行列相似判定 〜easy〜
|
ユーザー |
![]() |
提出日時 | 2025-07-25 12:55:37 |
言語 | D (dmd 2.109.1) |
結果 |
TLE
(最新)
AC
(最初)
|
実行時間 | - |
コード長 | 3,406 bytes |
コンパイル時間 | 571 ms |
コンパイル使用メモリ | 87,944 KB |
実行使用メモリ | 18,112 KB |
最終ジャッジ日時 | 2025-07-27 20:11:55 |
合計ジャッジ時間 | 7,024 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | TLE * 3 |
other | AC * 6 TLE * 27 |
ソースコード
import std.stdio, std.algorithm, std.array, std.conv, std.typecons; immutable p = 181; alias F = FiniteField!p; void main() { Matrix A, B; { auto tmp = readln.split.to!(int[]); A[0][0] = F(tmp[0]), A[0][1] = F(tmp[1]); } { auto tmp = readln.split.to!(int[]); A[1][0] = F(tmp[0]), A[1][1] = F(tmp[1]); } { auto tmp = readln.split.to!(int[]); B[0][0] = F(tmp[0]), B[0][1] = F(tmp[1]); } { auto tmp = readln.split.to!(int[]); B[1][0] = F(tmp[0]), B[1][1] = F(tmp[1]); } writeln(is_similar(A, B) ? "Yes" : "No"); } bool is_similar(Matrix A, Matrix B) { foreach (a; 0 .. 2) foreach (b; 0 .. p) foreach (c; 0 .. p) foreach (d; 0 .. p) if (F(a*d - b*c) != F(0)) { Matrix P; P[0][0] = F(a), P[0][1] = F(b), P[1][0] = F(c), P[1][1] = F(d); auto M = multiply(P, A); auto N = multiply(B, P); if (M[0][0] == N[0][0] && M[1][0] == N[1][0] && M[0][1] == N[0][1] && M[1][1] == N[1][1]) return true; } return false; } alias Matrix = F[2][2]; Matrix multiply(Matrix a, Matrix b) { Matrix c; c[0][0] = a[0][0]*b[0][0] + a[0][1]*b[1][0], c[0][1] = a[0][0]*b[0][1] + a[0][1]*b[1][1], c[1][0] = a[1][0]*b[0][0] + a[1][1]*b[1][0], c[1][1] = a[1][0]*b[0][1] + a[1][1]*b[1][1]; return c; } // the struct of finite fields with p elements // p must be a prime number struct FiniteField(long p) if (p > 1) { ulong n; this(long n) { if (n < 0) this.n = n%p + p; else this.n = n%p; } FiniteField!p opUnary(string op: "+")() { return this; } FiniteField!p opUnary(string op: "-")() { return FiniteField!p(-n); } FiniteField!p opBinary(string op)(long rhs) { static if (op == "^^") { if (rhs < 0) { return this.inv() ^^ rhs; } auto result = FiniteField!p(1); auto i = 0, pow_2_i = this; // pow_2_i = n^{2^i} rhs %= (p-1); while (rhs > 0) { if (rhs % 2 == 1) { result = result * pow_2_i; } rhs >>= 1; i++; pow_2_i = pow_2_i * pow_2_i; } return result; } else { return this.opBinary!op(FiniteField!p(rhs)); } } FiniteField!p opBinary(string op)(FiniteField!p rhs) { auto result = this; static if (op == "+") { result.n = (result.n + rhs.n) % p; } else if (op == "-") { result.n = (result.n + p - rhs.n) % p; } else if (op == "*") { result.n = (result.n * rhs.n) % p; } else if (op == "/") { assert (rhs.n != 0); result.n = (result.n * rhs.inv().n) % p; } else assert(0); return result; } FiniteField!p opOpAssign(string op)(long rhs) { return this = this.opBinary!op(rhs); } FiniteField!p opOpAssign(string op)(FiniteField!p rhs) { return this = this.opBinary!op(rhs); } bool opEquals(FiniteField!p rhs) { return (this.n + p - rhs.n) % p == 0; } bool opEquals(long rhs) { return (this.n + p - rhs) % p == 0; } FiniteField!p inv() { assert (this.n != 0); return this ^^ (p-2); } string toString() { import std.conv: to; return n.to!string; } }