結果

問題 No.950 行列累乗
ユーザー risujirohrisujiroh
提出日時 2019-12-13 02:13:04
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 2,216 bytes
コンパイル時間 1,473 ms
コンパイル使用メモリ 165,824 KB
実行使用メモリ 4,508 KB
最終ジャッジ日時 2023-09-08 10:56:40
合計ジャッジ時間 12,480 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 102 ms
4,376 KB
testcase_04 AC 102 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 102 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 WA -
testcase_09 AC 102 ms
4,380 KB
testcase_10 AC 3 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 103 ms
4,380 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 AC 102 ms
4,380 KB
testcase_16 WA -
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 102 ms
4,376 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 RE -
testcase_28 RE -
testcase_29 RE -
testcase_30 RE -
testcase_31 RE -
testcase_32 RE -
testcase_33 RE -
testcase_34 RE -
testcase_35 RE -
testcase_36 RE -
testcase_37 RE -
testcase_38 RE -
testcase_39 RE -
testcase_40 RE -
testcase_41 RE -
testcase_42 RE -
testcase_43 RE -
testcase_44 RE -
testcase_45 RE -
testcase_46 RE -
testcase_47 AC 2 ms
4,380 KB
testcase_48 RE -
testcase_49 RE -
testcase_50 RE -
testcase_51 RE -
testcase_52 RE -
testcase_53 RE -
testcase_54 AC 1 ms
4,376 KB
testcase_55 AC 1 ms
4,380 KB
testcase_56 RE -
testcase_57 AC 2 ms
4,376 KB
testcase_58 AC 1 ms
4,376 KB
testcase_59 RE -
testcase_60 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

long long mod = 998244353;
struct Mint {
  long long v;
  Mint(long long a = 0) : v((a %= mod) < 0 ? a + mod : a) {}
  Mint& operator*=(Mint r) { v = (long long)v * r.v % mod; return *this; }
  Mint& operator/=(Mint r) { return *this *= r.inv(); }
  Mint& operator+=(Mint r) { if ((v += r.v) >= mod) v -= mod; return *this; }
  Mint& operator-=(Mint r) { if ((v -= r.v) < 0) v += mod; return *this; }
  friend Mint operator*(Mint l, Mint r) { return l *= r; }
  friend Mint operator/(Mint l, Mint r) { return l /= r; }
  friend Mint operator+(Mint l, Mint r) { return l += r; }
  friend Mint operator-(Mint l, Mint r) { return l -= r; }
  Mint pow(long long n) const {
    assert(n >= 0);
    Mint res = 1;
    for (Mint t = *this; n; t *= t, n >>= 1) if (n & 1) res *= t;
    return res;
  }
  Mint inv() const { assert(v); return pow(mod - 2); }
  friend string to_string(Mint a) { return to_string(a.v); }
};
bool operator==(Mint l, Mint r) { return l.v == r.v; }

using Mat = array< array<Mint, 2>, 2 >;
const Mat I{1, 0, 0, 1};
Mat operator*(Mat A, Mat B) {
  Mat res;
  for (int i : {0, 1}) {
    for (int k : {0, 1}) {
      for (int j : {0, 1}) {
        res[i][j] += A[i][k] * B[k][j];
      }
    }
  }
  return res;
}
Mat& operator*=(Mat& A, Mat B) { return A = A * B; }
Mat pow(Mat A, long long n) {
  Mat res = I;
  while (n) {
    if (n & 1) {
      res *= A;
    }
    A *= A;
    n >>= 1;
  }
  return res;
}
istream& operator>>(istream& is, Mat& A) {
  for (int i : {0, 1}) {
    for (int j : {0, 1}) {
      is >> A[i][j].v;
    }
  }
  return is;
}

struct Stopwatch {
  clock_t t = clock();
  void restart() { t = clock(); }
  int elapsed() const { return (clock() - t) * 1000 / CLOCKS_PER_SEC; }
  friend string to_string(Stopwatch sw) {
    return "Time: " + to_string(sw.elapsed()) + " ms";
  }
};

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cin >> mod;
  Mat A, B;
  cin >> A >> B;
  auto C = A;
  int n = 1;
  for (Stopwatch sw; sw.elapsed() < 100; ) {
    if (B == C) {
      cout << n << '\n';
      return 0;
    }
    C *= A;
    ++n;
  }
  if (n > 2 * mod) {
    cout << "-1\n";
    return 0;
  }
  assert(false);
}
0