結果

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

テストケース

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

ソースコード

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() < 1900; ) {
    if (B == C) {
      cout << n << '\n';
      return 0;
    }
    C *= A;
    ++n;
  }
  if (n >= mod) {
    cout << "-1\n";
    return 0;
  }
  assert(false);
}
0