結果

問題 No.3231 2×2行列相似判定 〜hard〜
ユーザー Ryuhei Mori
提出日時 2025-08-09 17:46:20
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,008 bytes
コンパイル時間 762 ms
コンパイル使用メモリ 71,100 KB
実行使用メモリ 7,720 KB
最終ジャッジ日時 2025-08-09 17:46:23
合計ジャッジ時間 2,555 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 37
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <tuple>

using i32 = int;
using u32 = unsigned;
using i64 = long long;

template <i32 MOD>
struct Mint {
  i32 n;
  constexpr Mint(i32 n = 0): n(n) {}
  constexpr Mint operator-() const { return Mint(n ? MOD - n: 0); }
  constexpr Mint &operator+=(const Mint &rhs){ n += rhs.n; if(n >= MOD) n -= MOD; return *this; }
  constexpr Mint &operator-=(const Mint &rhs){ if(rhs.n > n) n += MOD; n -= rhs.n; return *this; }
  constexpr Mint &operator*=(const Mint &rhs){ n = (i64) n * rhs.n % MOD; return *this; }
  constexpr Mint inv() const {
    i32 x = MOD;
    i32 y = n;
    i32 b = 0, d = 1;
    while(y){
      i32 q = x / y;
      x = x % y;
      b -= q * d;
      std::swap(x, y);
      std::swap(b, d);
    }
    if(b < 0) b += MOD;
    return b;
  }
  constexpr Mint &operator/=(const Mint &rhs){ n = (i64) n * rhs.inv().n % MOD; return *this; }
  friend constexpr Mint operator+(const Mint &lhs, const Mint &rhs){ return Mint(lhs) += rhs; }
  friend constexpr Mint operator-(const Mint &lhs, const Mint &rhs){ return Mint(lhs) -= rhs; }
  friend constexpr Mint operator*(const Mint &lhs, const Mint &rhs){ return Mint(lhs) *= rhs; }
  friend constexpr Mint operator/(const Mint &lhs, const Mint &rhs){ return Mint(lhs) /= rhs; }
  friend constexpr bool operator==(const Mint &lhs, const Mint &rhs){ return lhs.n == rhs.n; }
  friend constexpr bool operator!=(const Mint &lhs, const Mint &rhs){ return lhs.n != rhs.n; }
  friend std::ostream &operator<<(std::ostream &os, const Mint &rhs){ return os << rhs.n; }
};

constexpr i32 mod = 1000000007;
using mint = Mint<mod>;

std::tuple<mint, mint, bool> hash(mint a, mint b, mint c, mint d){
  return { a + d, a * d - b * c, (a == d) && (b == 0) && (c == 0) };
}

int main(){
  u32 a, b, c, d;
  std::cin >> a >> b >> c >> d;
  auto h0 = hash(a, b, c, d);
  std::cin >> a >> b >> c >> d;
  auto h1 = hash(a, b, c, d);
  if(h0 == h1)
    std::cout << "Yes";
  else
    std::cout << "No";
  std::cout << std::endl;
  return 0;
}
0