結果

問題 No.950 行列累乗
ユーザー koba-e964koba-e964
提出日時 2019-12-19 03:21:25
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 389 ms / 2,000 ms
コード長 5,681 bytes
コンパイル時間 1,449 ms
コンパイル使用メモリ 125,256 KB
実行使用メモリ 19,868 KB
最終ジャッジ日時 2023-09-21 04:52:09
合計ジャッジ時間 9,271 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 94 ms
8,368 KB
testcase_01 AC 156 ms
8,336 KB
testcase_02 AC 25 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 108 ms
8,420 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 46 ms
4,376 KB
testcase_08 AC 46 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 41 ms
4,380 KB
testcase_11 AC 36 ms
4,380 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 57 ms
4,380 KB
testcase_14 AC 59 ms
4,380 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 45 ms
4,376 KB
testcase_17 AC 15 ms
4,380 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 19 ms
4,380 KB
testcase_20 AC 19 ms
4,380 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 AC 241 ms
19,620 KB
testcase_23 AC 106 ms
8,364 KB
testcase_24 AC 2 ms
4,376 KB
testcase_25 AC 109 ms
8,364 KB
testcase_26 AC 121 ms
8,392 KB
testcase_27 AC 1 ms
4,380 KB
testcase_28 AC 242 ms
19,584 KB
testcase_29 AC 241 ms
19,576 KB
testcase_30 AC 2 ms
4,380 KB
testcase_31 AC 205 ms
19,616 KB
testcase_32 AC 204 ms
19,716 KB
testcase_33 AC 2 ms
4,380 KB
testcase_34 AC 201 ms
19,572 KB
testcase_35 AC 247 ms
19,592 KB
testcase_36 AC 2 ms
4,376 KB
testcase_37 AC 92 ms
8,316 KB
testcase_38 AC 100 ms
8,356 KB
testcase_39 AC 1 ms
4,380 KB
testcase_40 AC 117 ms
8,336 KB
testcase_41 AC 119 ms
8,356 KB
testcase_42 AC 2 ms
4,380 KB
testcase_43 AC 250 ms
19,868 KB
testcase_44 AC 236 ms
19,632 KB
testcase_45 AC 2 ms
4,380 KB
testcase_46 AC 389 ms
19,620 KB
testcase_47 AC 52 ms
4,384 KB
testcase_48 AC 308 ms
19,604 KB
testcase_49 AC 280 ms
19,572 KB
testcase_50 AC 232 ms
19,628 KB
testcase_51 AC 339 ms
19,608 KB
testcase_52 AC 98 ms
8,332 KB
testcase_53 AC 96 ms
8,520 KB
testcase_54 AC 2 ms
4,376 KB
testcase_55 AC 2 ms
4,380 KB
testcase_56 AC 98 ms
8,368 KB
testcase_57 AC 113 ms
8,316 KB
testcase_58 AC 19 ms
4,380 KB
testcase_59 AC 2 ms
4,380 KB
testcase_60 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>

#define REP(i,s,n) for(int i=(int)(s);i<(int)(n);i++)
#define DEBUGP(val) cerr << #val << "=" << val << "\n"

using namespace std;
typedef long long int ll;
typedef unsigned long long ull;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef pair<int, int> PI;
typedef pair<ll, ll> PL;

const int DEBUG = 1;

ll p;

ll powmod(ll a, ll e) {
  ll prod = 1;
  ll cur = a % p;
  while (e > 0) {
    if (e % 2) prod = prod * cur % p;
    cur = cur * cur % p;
    e /= 2;
  }
  return prod;
}

typedef vector<VL> mat;

VL factors(ll v);


// Find min {x | a^x = 1 (mod p) }.
ll period(ll a) {
  assert (a >= 1);
  assert (a < p);
  ll v = p - 1;
  VL fs = factors(v);
  for (ll i: fs) {
    while (v % i == 0) {
      if (powmod(a, v / i) == 1) {
        v /= i;
      } else {
        break;
      }
    }
  }
  return v;
}

const ll N = 80000;

ll det(const mat &a) {
  ll tmp = a[0][0] * a[1][1] % p;
  tmp -= a[0][1] * a[1][0] % p;
  return (tmp + p) % p;
}

mat mul(const mat &a, const mat &b) {
  mat c(2, VL(2));
  REP(i, 0, 2) REP(j, 0, 2) REP(k, 0, 2)
    c[i][k] = (c[i][k] + a[i][j] * b[j][k]) % p;
  return c;
}
mat powmat(const mat &a, ll e) {
  mat prod(2, VL(2));
  mat cur(a);
  REP(i, 0, 2) prod[i][i] = 1;
  while (e > 0) {
    if (e % 2) prod = mul(prod, cur);
    cur = mul(cur, cur);
    e /= 2;
  }
  return prod;
}

mat inv(const mat &a) {
  ll d = det(a);
  assert (d != 0);
  d = powmod(d, p - 2);
  mat b(2, VL(2));
  b[0][0] = a[1][1] * d % p;
  b[0][1] = a[0][1] * (p - d) % p;
  b[1][0] = a[1][0] * (p - d) % p;
  b[1][1] = a[0][0] * d % p;
  return b;
}

// Find min {x > 0 | a^x = b (mod p)}.
ll disc_log(ll a, ll b) {
  if (a <= 1) {
    return a == b ? 1 : -1;
  }
  ll per = period(a);
  map<ll, ll> big;
  REP(i, 0, N) {
    big[powmod(a, p - 1 - (N * (ll)i) % (p - 1)) * b % p] = N * (ll)i;
  }
  ll nxt = -1;
  REP(i, 0, N) {
    ll v = powmod(a, i);
    if (big.count(v)) {
      nxt = big[v] + i;
      break;
    }
  }
  return nxt < 0 ? -1 : (nxt + per - 1) % per + 1;
}

VL factors(ll v) {
  VL a;
  ll i = 2;
  while (i * i <= v) {
    int e = 0;
    while (v % i == 0) {
      e++;
      v /= i;
    }
    if (e) a.push_back(i);
    i++;
  }
  if (v > 1) a.push_back(v);
  return a;
}

ll mat_period(mat a) {
  assert (det(a) == 1);
  mat unit(2, VL(2));
  REP(i, 0, 2) unit[i][i] = 1;
  if (a == unit) return 1;
  if (mul(a, a) == unit) return 2;
  if (powmat(a, p) == unit) return p;
  if (powmat(a, 2 * p) == unit) return 2 * p;
  ll v = p * p - 1;
  assert (powmat(a, v) == unit);
  VL f1 = factors(p - 1), f2 = factors(p + 1);
  for (int f: f2) f1.push_back(f);
  for (int f: f1) {
    while (v % f == 0) {
      if (powmat(a, v / f) == unit) {
        v /= f;
      } else {
        break;
      }
    }
  }
  assert (powmat(a, v) == unit);
  return v;
}

ll disc_log_mat(mat a, mat b) {
  assert (det(a) == 1);
  ll per = mat_period(a);
  assert (per <= 2 * p + 2);
  map<mat, ll> big;
  mat step = powmat(inv(a), N);
  mat cur(b);
  REP(i, 0, N) {
    big[cur] = N * (ll) i;
    cur = mul(cur, step);
  }
  cur = mat(2, VL(2));
  REP(i, 0, 2) cur[i][i] = 1;
  ll nxt = -1;
  REP(i, 0, N) {
    if (big.count(cur)) {
      nxt = big[cur] + i;
      nxt %= per;
      break;
    }
    cur = mul(cur, a);
  }
  return nxt < 0 ? -1 : nxt;
}

int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(0);
  mat a(2, VL(2)), b(2, VL(2));
  cin >> p;
  REP(i, 0, 2) {
    REP(j, 0, 2) {
      cin >> a[i][j];
    }
  }
  REP(i, 0, 2) {
    REP(j, 0, 2) {
      cin >> b[i][j];
    }
  }
  // A = kI?
  if (a[0][1] == 0 && a[1][0] == 0 && a[0][0] == a[1][1]) {
    if (b[0][1] == 0 && b[1][0] == 0 && b[0][0] == b[1][1]) {
      cout << disc_log(a[0][0], b[0][0]) << endl;
    } else {
      cout << -1 << endl;
    }
    return 0;
  }
  if (DEBUG) {
    cerr << det(a) << endl;
    cerr << det(b) << endl;
  }
  // B = xA + yI
  VL cons;
  if (a[0][1] != 0) {
    cons.push_back(b[0][1] * powmod(a[0][1], p - 2) % p);
  }
  if (a[1][0] != 0) {
    cons.push_back(b[1][0] * powmod(a[1][0], p - 2) % p);
  }
  if (a[0][0] != a[1][1]) {
    ll diff = (a[0][0] - a[1][1] + p) % p;
    cons.push_back((b[0][0] - b[1][1] + p) % p * powmod(diff, p - 2) % p);
  }
  assert (cons.size() > 0);
  cons.erase(unique(cons.begin(), cons.end()), cons.end());
  if (cons.size() != 1) {
    cout << -1 << endl;
    return 0;
  }
  ll x = cons[0];
  ll y = (b[0][0] - x * a[0][0] % p + p) % p;
  if (DEBUG) {
    cerr << "B = " << x << " A + " << y << " I" << endl;
  }
  if (det(a) == 0) {
    // A^2 = z A. Find z = tr(A).
    ll z = (a[0][0] + a[1][1]) % p;
    if (y != 0) {
      cout << -1 << endl;
      return 0;
    }
    if (z == 0) {
      ll ans = x == 1 ? 1 : x == 0 ? 2 : -1;
      cout << ans << endl;
      return 0;
    }
    cout << disc_log(z, z * x % p) << endl;
    return 0;
  }
  assert (det(a) != 0);
  ll e = disc_log(det(a), det(b));
  if (e < 0) {
    cout << -1 << endl;
    return 0;
  }
  if (DEBUG) {
    cerr << "|A|^" << e << " = |B|" << endl;
  }
  ull per = period(det(a));
  mat ap = powmat(a, per);
  b = mul(b, powmat(inv(a), e));
  ull tmp = disc_log_mat(ap, b);
  if (DEBUG) {
    cerr << "tmp = " << tmp << endl;
    cerr << "per = " << per << endl;
  }
  if (tmp >= 0) {
    cout << tmp * per + (ull) e << endl;
  } else {
    cout << -1 << endl;
  }
}
0