結果

問題 No.1648 Sum of Powers
ユーザー 👑 hos.lyrichos.lyric
提出日時 2021-08-13 22:04:17
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 26 ms / 2,000 ms
コード長 6,308 bytes
コンパイル時間 880 ms
コンパイル使用メモリ 121,052 KB
実行使用メモリ 9,504 KB
最終ジャッジ日時 2023-09-04 13:37:35
合計ジャッジ時間 5,896 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 23 ms
7,276 KB
testcase_01 AC 6 ms
4,380 KB
testcase_02 AC 11 ms
4,376 KB
testcase_03 AC 10 ms
4,380 KB
testcase_04 AC 16 ms
4,376 KB
testcase_05 AC 11 ms
4,376 KB
testcase_06 AC 11 ms
4,376 KB
testcase_07 AC 12 ms
5,148 KB
testcase_08 AC 15 ms
4,376 KB
testcase_09 AC 11 ms
4,380 KB
testcase_10 AC 11 ms
4,384 KB
testcase_11 AC 10 ms
4,376 KB
testcase_12 AC 17 ms
4,376 KB
testcase_13 AC 11 ms
4,376 KB
testcase_14 AC 10 ms
4,376 KB
testcase_15 AC 12 ms
4,376 KB
testcase_16 AC 12 ms
4,376 KB
testcase_17 AC 15 ms
5,136 KB
testcase_18 AC 10 ms
4,380 KB
testcase_19 AC 11 ms
4,376 KB
testcase_20 AC 11 ms
4,380 KB
testcase_21 AC 11 ms
4,376 KB
testcase_22 AC 23 ms
7,320 KB
testcase_23 AC 22 ms
7,212 KB
testcase_24 AC 23 ms
7,268 KB
testcase_25 AC 23 ms
7,232 KB
testcase_26 AC 22 ms
7,192 KB
testcase_27 AC 22 ms
7,220 KB
testcase_28 AC 24 ms
9,504 KB
testcase_29 AC 23 ms
7,512 KB
testcase_30 AC 23 ms
7,240 KB
testcase_31 AC 25 ms
8,092 KB
testcase_32 AC 23 ms
7,260 KB
testcase_33 AC 22 ms
7,256 KB
testcase_34 AC 22 ms
7,228 KB
testcase_35 AC 24 ms
8,360 KB
testcase_36 AC 23 ms
7,224 KB
testcase_37 AC 24 ms
7,276 KB
testcase_38 AC 24 ms
7,540 KB
testcase_39 AC 25 ms
7,276 KB
testcase_40 AC 23 ms
7,276 KB
testcase_41 AC 26 ms
7,304 KB
testcase_42 AC 24 ms
8,356 KB
testcase_43 AC 23 ms
7,300 KB
testcase_44 AC 24 ms
8,096 KB
testcase_45 AC 24 ms
7,536 KB
testcase_46 AC 23 ms
7,276 KB
testcase_47 AC 10 ms
4,376 KB
testcase_48 AC 5 ms
4,380 KB
testcase_49 AC 13 ms
4,376 KB
testcase_50 AC 15 ms
4,380 KB
testcase_51 AC 5 ms
4,952 KB
testcase_52 AC 22 ms
7,196 KB
testcase_53 AC 23 ms
8,332 KB
testcase_54 AC 21 ms
7,284 KB
testcase_55 AC 23 ms
7,228 KB
testcase_56 AC 23 ms
7,248 KB
testcase_57 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;

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)); }


struct ModInt(uint M_) {
  import std.conv : to;
  alias M = M_;
  uint x;
  this(ModInt a) { x = a.x; }
  this(uint x_) { x = x_ % M; }
  this(ulong x_) { x = x_ % M; }
  this(int x_) { x = ((x_ %= cast(int)(M)) < 0) ? (x_ + cast(int)(M)) : x_; }
  this(long x_) { x = cast(uint)(((x_ %= cast(long)(M)) < 0) ? (x_ + cast(long)(M)) : x_); }
  ref ModInt opAssign(T)(inout(T) a) if (is(T == uint) || is(T == ulong) || is(T == int) || is(T == long)) { return this = ModInt(a); }
  ref ModInt opOpAssign(string op, T)(T a) {
    static if (is(T == ModInt)) {
      static if (op == "+") { x = ((x += a.x) >= M) ? (x - M) : x; }
      else static if (op == "-") { x = ((x -= a.x) >= M) ? (x + M) : x; }
      else static if (op == "*") { x = cast(uint)((cast(ulong)(x) * a.x) % M); }
      else static if (op == "/") { this *= a.inv(); }
      else static assert(false);
      return this;
    } else static if (op == "^^") {
      if (a < 0) return this = inv()^^(-a);
      ModInt b = this, c = 1U;
      for (long e = a; e; e >>= 1) { if (e & 1) c *= b; b *= b; }
      return this = c;
    } else {
      return mixin("this " ~ op ~ "= ModInt(a)");
    }
  }
  ModInt inv() const {
    uint a = M, b = x; int y = 0, z = 1;
    for (; b; ) { const q = a / b; const c = a - q * b; a = b; b = c; const w = y - cast(int)(q) * z; y = z; z = w; }
    assert(a == 1); return ModInt(y);
  }
  ModInt opUnary(string op)() const {
    static if (op == "+") { return this; }
    else static if (op == "-") { ModInt a; a.x = x ? (M - x) : 0U; return a; }
    else static assert(false);
  }
  ModInt opBinary(string op, T)(T a) const { return mixin("ModInt(this) " ~ op ~ "= a"); }
  ModInt opBinaryRight(string op, T)(T a) const { return mixin("ModInt(a) " ~ op ~ "= this"); }
  bool opCast(T: bool)() const { return (x != 0U); }
  string toString() const { return x.to!string; }
}

enum MO = 998244353;
alias Mint = ModInt!MO;


// !!!old library!!!
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, ak = 1;
	Tuple!(long,long)[] as;
	for (k = 0; k * k < m; ++k) {
		as ~= tuple(ak, k);
		ak = (ak * a) % m;
	}
	as.sort;
	ak = modInv(ak, 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 * ak) % m;
	}
	return -1;
}
long modLog(long a, long b, long m) {
	a = mod(a, m); b = mod(b, m);
	long f, g, r = 1 % m;
	for (f = 0; (g = gcd(a, m)) > 1; ++f) {
		if (b % g != 0) return (r == b) ? f : -1;
		b /= g; m /= g;
		r = (r * (a / g)) % m;
	}
	long res = modLogP(a, b * modInv(r, m) % m, m);
	return (res != -1) ? (f + res) : -1;
}


enum L = 10L^^5;

void main() {
  try {
    for (; ; ) {
      const Mint A = readLong();
      const Mint B = readLong();
      const Mint P = readLong();
      const Mint Q = readLong();
      
      long ans = -1;
      
      if (B) {
        /*
          [ A -B ]
          [ 1    ]
        */
        Mint[2][2] c;
        c[0][0] = c[1][1] = 1;
        long[ulong] app;
        foreach (y; 0 .. L) {
          const p = c[0][0] * P + c[0][1] * Q;
          const q = c[1][0] * P + c[1][1] * Q;
          const key = cast(ulong)(p.x) << 30 | q.x;
          app[key] = y;
          debug {
            if (L - y <= 10) {
              writeln(y, " ", p, " ", q, " ", key);
              stdout.flush;
            }
          }
          
          Mint tmp;
          tmp = A * c[0][0] - B * c[1][0];
          c[1][0] = c[0][0];
          c[0][0] = tmp;
          tmp = A * c[0][1] - B * c[1][1];
          c[1][1] = c[0][1];
          c[0][1] = tmp;
        }
        debug {
          writeln("c = ", c);
          stdout.flush;
        }
        {
          Mint p = A, q = 2;
          for (long x = 1; ; ++x) {
            const pp = c[0][0] * p + c[0][1] * q;
            const qq = c[1][0] * p + c[1][1] * q;
            p = pp;
            q = qq;
            const key = cast(ulong)(p.x) << 30 | q.x;
            debug {
              if (x <= 10) {
                writeln("! ", p, " ", q, " ", key);
                stdout.flush;
              }
            }
            if (key in app) {
              // f^(L x) (A, 2) = f^y (P, Q)
              const y = app[key];
              ans = L * x - y + 1;
              break;
            }
          }
        }
      } else if (A) {
        /*
          X = 0, Y = A
          P = A^N, Q = A^(N-1)
        */
        assert(P == A * Q);
        ans = modLogP(A.x, P.x, MO);
        for (; ans < 2; ans += MO - 1) {}
      } else {
        assert(!P);
        assert(!Q);
        ans = 2;
      }
      
      writeln(ans);
    }
  } catch (EOFException e) {
  }
}
0