結果

問題 No.950 行列累乗
ユーザー pekempeypekempey
提出日時 2019-12-13 01:53:30
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 178 ms / 2,000 ms
コード長 5,046 bytes
コンパイル時間 2,633 ms
コンパイル使用メモリ 189,584 KB
実行使用メモリ 12,384 KB
最終ジャッジ日時 2023-09-09 10:42:51
合計ジャッジ時間 9,091 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 35 ms
7,752 KB
testcase_01 AC 49 ms
7,960 KB
testcase_02 AC 15 ms
4,380 KB
testcase_03 AC 4 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 76 ms
7,804 KB
testcase_06 AC 53 ms
4,376 KB
testcase_07 AC 26 ms
4,380 KB
testcase_08 AC 35 ms
4,380 KB
testcase_09 AC 39 ms
4,376 KB
testcase_10 AC 19 ms
4,380 KB
testcase_11 AC 22 ms
4,380 KB
testcase_12 AC 4 ms
4,380 KB
testcase_13 AC 34 ms
4,380 KB
testcase_14 AC 38 ms
4,380 KB
testcase_15 AC 4 ms
4,376 KB
testcase_16 AC 23 ms
4,376 KB
testcase_17 AC 3 ms
4,380 KB
testcase_18 AC 10 ms
4,376 KB
testcase_19 AC 5 ms
4,380 KB
testcase_20 AC 4 ms
4,380 KB
testcase_21 AC 52 ms
7,828 KB
testcase_22 AC 139 ms
12,212 KB
testcase_23 AC 80 ms
7,760 KB
testcase_24 AC 170 ms
12,324 KB
testcase_25 AC 81 ms
7,760 KB
testcase_26 AC 84 ms
7,960 KB
testcase_27 AC 52 ms
7,800 KB
testcase_28 AC 140 ms
12,140 KB
testcase_29 AC 141 ms
12,284 KB
testcase_30 AC 176 ms
12,120 KB
testcase_31 AC 126 ms
12,108 KB
testcase_32 AC 134 ms
12,336 KB
testcase_33 AC 158 ms
12,140 KB
testcase_34 AC 127 ms
12,176 KB
testcase_35 AC 129 ms
12,264 KB
testcase_36 AC 35 ms
7,756 KB
testcase_37 AC 34 ms
8,024 KB
testcase_38 AC 35 ms
7,800 KB
testcase_39 AC 35 ms
8,024 KB
testcase_40 AC 78 ms
7,884 KB
testcase_41 AC 79 ms
7,764 KB
testcase_42 AC 51 ms
7,804 KB
testcase_43 AC 147 ms
12,140 KB
testcase_44 AC 147 ms
12,124 KB
testcase_45 AC 178 ms
12,188 KB
testcase_46 AC 171 ms
12,176 KB
testcase_47 AC 15 ms
4,376 KB
testcase_48 AC 167 ms
12,312 KB
testcase_49 AC 173 ms
12,384 KB
testcase_50 AC 166 ms
12,136 KB
testcase_51 AC 156 ms
12,140 KB
testcase_52 AC 41 ms
7,812 KB
testcase_53 AC 41 ms
7,804 KB
testcase_54 AC 3 ms
4,384 KB
testcase_55 AC 2 ms
4,380 KB
testcase_56 AC 35 ms
7,764 KB
testcase_57 AC 80 ms
7,836 KB
testcase_58 AC 15 ms
4,380 KB
testcase_59 AC 52 ms
7,800 KB
testcase_60 AC 36 ms
7,860 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
 
#define rep(i, n)      for (int i = 0; i < (n); i++)
#define repr(i, n)     for (int i = (n) - 1; i >= 0; i--)
#define repe(i, l, r)  for (int i = (l); i < (r); i++)
#define reper(i, l, r) for (int i = (r) - 1; i >= (l); i--)
#define repi(i, l, r)  for (int i = (l); i <= (r); i++)
#define repir(i, l, r) for (int i = (r); i >= (l); i--)
#define range(a) a.begin(), a.end()
void initio() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); }

ll MOD;

class mint {
  ll n;
public:
  mint(ll n_ = 0) : n(n_) {}
  explicit operator ll() { return n; }
  friend mint operator-(mint a) { return -a.n + MOD * (a.n != 0); }
  friend mint operator+(mint a, mint b) { ll x = a.n + b.n; return x - (x >= MOD) * MOD; }
  friend mint operator-(mint a, mint b) { ll x = a.n - b.n; return x + (x < 0) * MOD; }
  friend mint operator*(mint a, mint b) { return (long long)a.n * b.n % MOD; }
  friend mint &operator+=(mint &a, mint b) { return a = a + b; }
  friend mint &operator-=(mint &a, mint b) { return a = a - b; }
  friend mint &operator*=(mint &a, mint b) { return a = a * b; }
  friend bool operator==(mint a, mint b) { return a.n == b.n; }
  friend bool operator<(mint a, mint b) { return a.n < b.n; }
  friend bool operator!=(mint a, mint b) { return a.n != b.n; }
  friend istream &operator>>(istream &i, mint &a) { return i >> a.n; }
  friend ostream &operator<<(ostream &o, mint a) { return o << a.n; }
};

mint modpow(mint a, ll b) {
  mint res = 1;
  while (b > 0) {
    if (b & 1) res *= a;
    a *= a;
    b >>= 1;
  }
  return res;
}

mint modinv(mint n) {
  ll a = (ll)n, b = MOD;
  ll s = 1, t = 0;
  while (b != 0) {
    int q = a / b;
    a -= q * b;
    s -= q * t;
    swap(a, b);
    swap(s, t);
  }
  return s >= 0 ? s : s + MOD;
}


using mat = vector<mint>;
mat E = {1, 0, 0, 1};

mint det(mat A) {
  return A[0] * A[3] - A[1] * A[2];
}

mat inv(mat A) {
  mint d = modinv(det(A));
  mat res(4);
  res[0] = A[3] * d;
  res[1] = -A[1] * d;
  res[2] = -A[2] * d;
  res[3] = A[0] * d;
  return res;
}

mat mul(mat A, mat B) {
  mat res(4);
  res[0] = A[0] * B[0] + A[1] * B[2];
  res[1] = A[0] * B[1] + A[1] * B[3];
  res[2] = A[2] * B[0] + A[3] * B[2];
  res[3] = A[2] * B[1] + A[3] * B[3];
  return res;
}

mat matpow(mat A, ll B) {
  mat res = E;
  while (B > 0) {
    if (B & 1) res = mul(res, A);
    A = mul(A, A);
    B >>= 1;
  }
  return res;
}

ll bsgs(mint a, mint b) {
  constexpr ll S = 70000;
  map<mint, ll> mp;
  mint R = 1;
  for (int i = 0; i < S; i++) {
    if (!mp.count(R)) {
      mp[R] = i;
    }
    R *= a;
  }
  // A^{50000i + j} = B
  // A^j = B^A{-50000i}
  R = modinv(R);
  for (int i = 0; i < S; i++) {
    if (mp.count(b)) {
      return S*i + mp[b];
    }
    b *= R;
  }
  return -1;
}

ll bsgs_except_zero(mint a, mint b) {
  constexpr ll S = 70000;
  map<mint, ll> mp;
  mint R = 1;
  for (int i = 0; i < S; i++) {
    if (!mp.count(R * a)) {
      mp[R * a] = i + 1;
    }
    R *= a;
  }
  // A^{50000i + j} = B
  // A^j = B^A{-50000i}
  R = modinv(R);
  for (int i = 0; i < S; i++) {
    if (mp.count(b)) {
      return S*i + mp[b];
    }
    b *= R;
  }
  return -1;
}

ll bsgs_mat(mat A, mat B) {
  constexpr ll S = 70000;
  map<mat, ll> mp;
  mat R = E;
  for (int i = 0; i < S; i++) {
    if (!mp.count(R)) {
      mp[R] = i;
    }
    R = mul(R, A);
  }
  // A^{50000i + j} = B
  // A^j = B^A{-50000i}
  R = inv(R);
  for (int i = 0; i < S; i++) {
    if (mp.count(B)) {
      return S*i + mp[B];
    }
    B = mul(B, R);
  }
  return -1;
}

// ----------------------------------------------------------------
// det(A)=0 のときケーリーハミルトンの定理より A^2-(a+d)A = O が成り立つ。つまり
// A^n = (a+d)^{n-1} A = B
// これは BSGS で解ける。
// ----------------------------------------------------------------
// det(A)!=0 のとき。
// 
// det(A)^N = det(B) より N%T が求められる。ここで T は det(A) の位数とする。
// よって
// A ^ {N%T + Tk} = B
// det(A) != 0 なので A に逆行列があり
// (A^T)^k = B^A^{-N%T}
// 
// det(A^T)=1 なので A^T の周期は 2p 以下。これで BSGS が使える。

int main() { initio();
  cin >> MOD;
  mat A(4), B(4);
  rep(i, 4) cin >> A[i];
  rep(i, 4) cin >> B[i];
  if (det(A) == 0) {
    int k = -1;
    rep(i, 4) if (A[i] != 0) k = i;
    if (k == -1) {
      if (A == B) {
        cout << 1 << endl;
        return 0;
      }
      cout << -1 << endl;
      return 0;
    }
    ll p = bsgs(A[0] + A[3], B[k] * modinv(A[k]));
    if (p != -1 && matpow(A, p + 1) == B) {
      cout << p + 1 << endl;
    } else {
      cout << -1 << endl;
    }
    return 0;
  }
  ll p = bsgs_except_zero(det(A), det(B));
  if (p == -1) {
    cout << -1 << endl;
    return 0;
  }
  ll T = bsgs_except_zero(det(A), 1);
  // (A^T)^k = B^A^{-N%T}
  ll k = bsgs_mat(matpow(A, T), mul(B, matpow(inv(A), p)));
  if (k == -1) {
    cout << -1 << endl;
    return 0;
  }
  cout << p + T*k << endl;
}
0