結果

問題 No.950 行列累乗
ユーザー 👑 hos.lyrichos.lyric
提出日時 2019-12-13 02:24:27
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 238 ms / 2,000 ms
コード長 8,146 bytes
コンパイル時間 972 ms
コンパイル使用メモリ 125,592 KB
実行使用メモリ 20,472 KB
最終ジャッジ日時 2023-09-04 03:55:13
合計ジャッジ時間 6,651 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 14 ms
5,116 KB
testcase_01 AC 17 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 58 ms
12,364 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 1 ms
4,376 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 40 ms
4,380 KB
testcase_22 AC 126 ms
14,340 KB
testcase_23 AC 67 ms
12,064 KB
testcase_24 AC 140 ms
14,880 KB
testcase_25 AC 59 ms
8,624 KB
testcase_26 AC 86 ms
12,204 KB
testcase_27 AC 27 ms
4,376 KB
testcase_28 AC 115 ms
13,164 KB
testcase_29 AC 98 ms
12,368 KB
testcase_30 AC 157 ms
14,016 KB
testcase_31 AC 122 ms
13,160 KB
testcase_32 AC 161 ms
14,040 KB
testcase_33 AC 163 ms
14,108 KB
testcase_34 AC 166 ms
14,764 KB
testcase_35 AC 143 ms
14,108 KB
testcase_36 AC 1 ms
4,376 KB
testcase_37 AC 11 ms
5,124 KB
testcase_38 AC 13 ms
4,376 KB
testcase_39 AC 1 ms
4,376 KB
testcase_40 AC 102 ms
13,180 KB
testcase_41 AC 80 ms
13,620 KB
testcase_42 AC 38 ms
5,448 KB
testcase_43 AC 167 ms
15,380 KB
testcase_44 AC 172 ms
15,360 KB
testcase_45 AC 238 ms
19,368 KB
testcase_46 AC 191 ms
15,336 KB
testcase_47 AC 82 ms
12,912 KB
testcase_48 AC 191 ms
15,328 KB
testcase_49 AC 193 ms
16,740 KB
testcase_50 AC 208 ms
19,980 KB
testcase_51 AC 230 ms
20,472 KB
testcase_52 AC 23 ms
4,376 KB
testcase_53 AC 24 ms
4,376 KB
testcase_54 AC 1 ms
4,380 KB
testcase_55 AC 2 ms
4,376 KB
testcase_56 AC 15 ms
5,172 KB
testcase_57 AC 79 ms
13,252 KB
testcase_58 AC 1 ms
4,376 KB
testcase_59 AC 2 ms
4,384 KB
testcase_60 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;
import core.bitop, core.stdc.stdlib;

class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }

bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }

int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }


// floor(a^(1/k))
ulong floorKthRoot(ulong a, ulong k) {
  import core.bitop : bsr;
  import std.algorithm : min;
  if (a == 0) {
    return 0;
  } else if (k <= 1) {
    return a;
  } else if (k == 2) {
    ulong b = a, x = 0, y = 0;
    for (int e = bsr(a) & ~1; e >= 0; e -= 2) {
      x <<= 1;
      y <<= 1;
      if (b >= (y | 1) << e) {
        b -= (y | 1) << e;
        x |= 1;
        y += 2;
      }
    }
    return x;
  } else if (k <= 40) {
    // min x s.t. x^k >= 2^64
    enum ulong[] HIS =
        [0, 0, 4294967296UL, 2642246, 65536, 7132, 1626, 566, 256, 139, 85, 57,
         41, 31, 24, 20, 16, 14, 12, 11, 10, 9, 8, 7, 7, 6, 6, 6, 5, 5, 5, 5,
         4, 4, 4, 4, 4, 4, 4, 4, 4];
    ulong lo = 1UL << (bsr(a) / k);
    ulong hi = min(1UL << (bsr(a) / k + 1), HIS[cast(size_t)(k)]);
    for (; lo + 1 < hi; ) {
      const ulong mid = (lo + hi) / 2;
      ulong b = mid * mid;
      foreach (i; 2 .. k) b *= mid;
      ((b <= a) ? lo : hi) = mid;
    }
    return lo;
  } else if (k <= 63) {
    return ((1UL << k) <= a) ? 2 : 1;
  } else {
    return 1;
  }
}


// a^-1 (mod m)
long modInv(long a, long m)
in {
  assert(m > 0, "modInv: m > 0 must hold");
}
do {
  long b = m, x = 1, y = 0, t;
  for (; ; ) {
    t = a / b; a -= t * b;
    if (a == 0) {
      assert(b == 1 || b == -1, "modInv: gcd(a, m) != 1");
      if (b == -1) y = -y;
      return (y < 0) ? (y + m) : y;
    }
    x -= t * y;
    t = b / a; b -= t * a;
    if (b == 0) {
      assert(a == 1 || a == -1, "modInv: gcd(a, m) != 1");
      if (a == -1) x = -x;
      return (x < 0) ? (x + m) : x;
    }
    y -= t * x;
  }
}


Int mod(Int)(Int a, Int m) {
  if ((a %= m) < 0) a += m; return a;
}
Int gcd(Int)(Int a, Int b) { return (b != 0) ? gcd(b, a % b) : a; }
Int lcm(Int)(Int a, Int b) { return a / gcd(a, b) * b; }
Int gojo(Int)(Int a, Int b, out Int x, out Int y) {
  if (b != 0) { Int g = gojo(b, a % b, y, x); y -= (a / b) * x; return g; }
  x = 1; y = 0; return a;
}
Int modInv(Int)(Int a, Int m) { Int x, y; gojo(a, m, x, y); return mod(x, m); }
long modLogP(long a, long b, long m) {
	a = mod(a, m); b = mod(b, m);
	if (m == 1) return 0;
	long k, al = 1;
	Tuple!(long,long)[] as;
	for (k = 0; k * k < m; ++k) {
		as ~= tuple(al, k);
		al = (al * a) % m;
	}
	as.sort;
	al = modInv(al, m);
	for (long i = 0; i < k; ++i) {
		int pos = as.lowerBound(tuple(b, 0L));
		if (pos < as.length && as[pos][0] == b) return i * k + as[pos][1];
		b = (b * al) % m;
	}
	return -1;
}


long P;
long[][] A, B;

long[][] div(long[][] a, long t) {
  const invT = modInv(t, P);
  auto b = new long[][](2, 2);
  foreach (i; 0 .. 2) foreach (j; 0 .. 2) {
    b[i][j] = mod(invT * a[i][j], P);
  }
  return b;
}
long[][] inv(long[][] a) {
  const det = mod(a[0][0] * a[1][1] - a[0][1] * a[1][0], P);
  return div([[a[1][1], -a[0][1]], [-a[1][0], a[0][0]]], det);
}
long[][] mul(long[][] a, long[][] b) {
  auto c = new long[][](2, 2);
  foreach (i; 0 .. 2) foreach (k; 0 .. 2) foreach (j; 0 .. 2) {
    c[i][j] = mod(c[i][j] + a[i][k] * b[k][j], P);
  }
  return c;
}
long[][] power(long[][] a, long e) {
  long[][] b = a, c = [[1, 0], [0, 1]];
  for (; e; e >>= 1) {
    if (e & 1) c = mul(c, b);
    b = mul(b, b);
  }
  return c;
}

long solveBSGS(long[][] a, long[][] b) {
  const m = cast(int)(floorKthRoot(2 * P, 2) + 2);
  auto als = new Tuple!(long[][], int)[m];
  long[][] al = [[1, 0], [0, 1]];
  foreach (l; 0 .. m) {
    als[l] = tuple(al, l);
    al = mul(al, a);
  }
  als.sort;
  al = inv(al);
  long[][] amk = b;
  foreach (k; 0 .. m) {
    const pos = als.lowerBound(tuple(amk, 0));
    if (pos < m && als[pos][0] == amk) {
      return k * cast(long)(m) + als[pos][1];
    }
    amk = mul(amk, al);
  }
  return -1;
}


void main() {
  try {
    for (; ; ) {
      P = readLong();
      A = new long[][](2, 2);
      foreach (i; 0 .. 2) foreach (j; 0 .. 2) {
        A[i][j] = readLong();
      }
      B = new long[][](2, 2);
      foreach (i; 0 .. 2) foreach (j; 0 .. 2) {
        B[i][j] = readLong();
      }
      
      long ans;
      const detA = mod(A[0][0] * A[1][1] - A[0][1] * A[1][0], P);
      const detB = mod(B[0][0] * B[1][1] - B[0][1] * B[1][0], P);
      if (detA == 0) {
        if (detB == 0) {
          // eigenvalues: 0, r
          const r = mod(A[0][0] + A[1][1], P);
          if (r == 0) {
            if (A == B) {
              ans = 1;
            } else if (mul(A, A) == B) {
              ans = 2;
            } else {
              ans = -1;
            }
          } else {
            /*
              C^-1 A C = [[r, 0], [0, 0]]
              
              a00 c00 + a01 c10 = r c00
              a10 c00 + a11 c10 = r c10
              a00 c01 + a01 c11 = 0
              a10 c01 + a11 c11 = 0
            */
            auto c = new long[][](2, 2);
            c[0][0] = mod(-A[0][1], P);
            c[1][0] = mod(A[0][0] - r, P);
            if (c[0][0] == 0 && c[1][0] == 0) {
              c[0][0] = mod(-(A[1][1] - r), P);
              c[1][0] = mod(A[1][0], P);
            }
            if (c[0][0] == 0 && c[1][0] == 0) {
              assert(false);
            }
            c[0][1] = mod(-A[0][1], P);
            c[1][1] = mod(A[0][0], P);
            if (c[0][1] == 0 && c[1][1] == 0) {
              c[0][1] = mod(-A[1][1], P);
              c[1][1] = mod(A[1][0], P);
            }
            if (c[0][1] == 0 && c[1][1] == 0) {
              assert(false);
            }
            debug {
              writeln("r = ", r);
              writeln("c = ", c);
            }
            auto a = mul(mul(inv(c), A), c);
            auto b = mul(mul(inv(c), B), c);
            debug {
              writeln("a = ", a);
              writeln("b = ", b);
            }
            assert(a == [[r, 0L], [0L, 0L]]);
            if (b[0][1] == 0 && b[1][0] == 0 && b[1][1] == 0) {
              const res = modLogP(r, mod(b[0][0] * modInv(r, P), P), P);
              if (res == -1) {
                ans = -1;
              } else {
                ans = res + 1;
              }
            } else {
              ans = -1;
            }
          }
        } else {
          ans = -1;
        }
      } else {
        const period = modLogP(detA, modInv(detA, P), P) + 1;
        long e0 = modLogP(detA, detB, P);
        debug {
          writeln("detA = ", detA, ", detB = ", detB);
          writeln("period = ", period);
          writeln("e0 = ", e0);
        }
        if (e0 == -1) {
          ans = -1;
        } else {
          if (e0 == 0) {
            e0 += period;
          }
          auto a = power(A, period);
          auto b = mul(B, inv(power(A, e0)));
          debug {
            writeln("a = ", a);
            writeln("b = ", b);
          }
          const res = solveBSGS(a, b);
          if (res == -1) {
            ans = -1;
          } else {
            ans = e0 + period * res;
          }
        }
      }
      writeln(ans);
    }
  } catch (EOFException e) {
  }
}
0