結果

問題 No.551 夏休みの思い出(2)
ユーザー Min_25Min_25
提出日時 2017-07-29 06:05:29
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 41 ms / 4,000 ms
コード長 3,377 bytes
コンパイル時間 1,029 ms
コンパイル使用メモリ 97,380 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-19 04:43:38
合計ジャッジ時間 2,979 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 3 ms
6,940 KB
testcase_04 AC 3 ms
6,940 KB
testcase_05 AC 5 ms
6,940 KB
testcase_06 AC 6 ms
6,944 KB
testcase_07 AC 2 ms
6,944 KB
testcase_08 AC 2 ms
6,944 KB
testcase_09 AC 2 ms
6,944 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 2 ms
6,940 KB
testcase_12 AC 2 ms
6,944 KB
testcase_13 AC 2 ms
6,940 KB
testcase_14 AC 2 ms
6,940 KB
testcase_15 AC 2 ms
6,940 KB
testcase_16 AC 2 ms
6,944 KB
testcase_17 AC 2 ms
6,944 KB
testcase_18 AC 2 ms
6,940 KB
testcase_19 AC 2 ms
6,940 KB
testcase_20 AC 2 ms
6,940 KB
testcase_21 AC 2 ms
6,944 KB
testcase_22 AC 2 ms
6,940 KB
testcase_23 AC 2 ms
6,944 KB
testcase_24 AC 2 ms
6,940 KB
testcase_25 AC 2 ms
6,944 KB
testcase_26 AC 2 ms
6,944 KB
testcase_27 AC 27 ms
6,944 KB
testcase_28 AC 22 ms
6,944 KB
testcase_29 AC 21 ms
6,940 KB
testcase_30 AC 35 ms
6,944 KB
testcase_31 AC 20 ms
6,940 KB
testcase_32 AC 31 ms
6,940 KB
testcase_33 AC 22 ms
6,944 KB
testcase_34 AC 22 ms
6,944 KB
testcase_35 AC 20 ms
6,944 KB
testcase_36 AC 21 ms
6,940 KB
testcase_37 AC 25 ms
6,940 KB
testcase_38 AC 29 ms
6,944 KB
testcase_39 AC 23 ms
6,940 KB
testcase_40 AC 23 ms
6,940 KB
testcase_41 AC 41 ms
6,940 KB
testcase_42 AC 24 ms
6,944 KB
testcase_43 AC 24 ms
6,944 KB
testcase_44 AC 24 ms
6,940 KB
testcase_45 AC 23 ms
6,944 KB
testcase_46 AC 26 ms
6,944 KB
testcase_47 AC 2 ms
6,944 KB
testcase_48 AC 2 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cassert>
#include <cmath>
#include <cstring>

#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <functional>
#include <stack>
#include <queue>

#include <tuple>

#define getchar getchar_unlocked
#define putchar putchar_unlocked

#define _rep(_1, _2, _3, _4, name, ...) name
#define rep2(i, n) rep3(i, 0, n)
#define rep3(i, a, b) rep4(i, a, b, 1)
#define rep4(i, a, b, c) for (int i = int(a); i < int(b); i += int(c))
#define rep(...) _rep(__VA_ARGS__, rep4, rep3, rep2, _)(__VA_ARGS__)

using namespace std;

using i64 = long long;
using u8 = unsigned char;
using u32 = unsigned;
using u64 = unsigned long long;
using f80 = long double;

vector< pair<int, int> > factors(int n) {
  vector< pair<int, int> > ret;
  for (int i = 2; i64(i) * i <= n; ++i) {
    if (n % i == 0) {
      int e = 1; n /= i;
      while (n % i == 0) n /= i, ++e;
      ret.emplace_back(i, e);
    }
  }
  if (n > 1) ret.emplace_back(n, 1);
  return ret;
}

int gcd(int a, int b) {
  return b ? gcd(b, a % b) : a;
}

int pow_mod(int b, int e, int p) {
  int ret = 1;
  for (; e; e >>= 1, b = i64(b) * b % p) {
    if (e & 1) ret = i64(ret) * b % p;
  }
  return ret;
}

int mod_inv(int a, int mod) {
  int b = mod, s = 1, u = 0;
  while (b) {
    int q = a / b;
    swap(b, a %= b);
    swap(s -= q * u, u);
  }
  if (a != 1) assert(0);
  return s < 0 ? s + mod : s;
}

int msqrtp_p(int a, int p, int e, int mod) {
  int q = mod - 1, s = 0;
  while (q % p == 0) q /= p, ++s;
  int pe = 1;
  rep(_, e) pe *= p;
  int d = mod_inv(pe - q % pe, pe) * q;
  int r = pow_mod(a, (d + 1) / pe, mod);
  int t = pow_mod(a, d, mod);
  if (t == 1) return r;
  int ps = 1; rep(_, s - 1) ps *= p;
  int c = -1;
  for (int z = 2; ; ++z) {
    c = pow_mod(z, q, mod);
    if (pow_mod(c, ps, mod) != 1) break;
  }
  int b = -1;
  while (t != 1) {
    int tmp = pow_mod(t, p, mod), s2 = 1;
    while (tmp != 1) tmp = pow_mod(tmp, p, mod), ++s2;
    if (s2 + e <= s) {
      b = c;
      rep(_, s - s2 - e) b = pow_mod(b, p, mod);
      c = pow_mod(b, pe, mod);
      s = s2;
    }
    r = i64(r) * b % mod;
    t = i64(t) * c % mod;
  }
  return r;
}

int msqrtn_p(int a, int n, int p) {
  assert(n >= 1);
  a %= p, n %= p - 1;
  if (a <= 1) return a;
  int g = gcd(p - 1, n);
  if (pow_mod(a, (p - 1) / g, p) != 1) return -1;
  a = pow_mod(a, mod_inv(n / g, (p - 1) / g), p);
  for (auto pp : factors(g)) {
    a = msqrtp_p(a, pp.first, pp.second, p);
  }
  return a;
}

void solve() {
  int P, R;
  while (~scanf("%d %d", &P, &R)) {
    int Q; scanf("%d", &Q);
    int inv2 = pow_mod(2, P - 2, P);
    rep(_, Q) {
      int a, b, c; scanf("%d %d %d", &a, &b, &c);
      int inv = pow_mod(a, P - 2, P);
      b = i64(b) * inv % P;
      c = i64(c) * inv % P;
      b = i64(b) * inv2 % P;
      int rhs = (P - c + i64(b) * b) % P;
      int r = msqrtn_p(rhs, 2, P);
      if (r < 0) {
        puts("-1");
      } else {
        int ans1 = (r + P - b) % P;
        int ans2 = (P - r + P - b) % P;
        if (ans1 > ans2) swap(ans1, ans2);
        if (ans1 == ans2) {
          printf("%d\n", ans1);
        } else {
          printf("%d %d\n", ans1, ans2);
        }
      }
    }
  }
}

int main() {
  clock_t beg = clock();
  solve();
  clock_t end = clock();
  fprintf(stderr, "%.3f sec\n", double(end - beg) / CLOCKS_PER_SEC);
  return 0;
}
0