結果

問題 No.551 夏休みの思い出(2)
ユーザー Min_25Min_25
提出日時 2017-08-15 22:15:34
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 9 ms / 4,000 ms
コード長 3,073 bytes
コンパイル時間 727 ms
コンパイル使用メモリ 93,940 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-21 12:34:56
合計ジャッジ時間 2,075 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 3 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 1 ms
5,376 KB
testcase_11 AC 1 ms
5,376 KB
testcase_12 AC 1 ms
5,376 KB
testcase_13 AC 1 ms
5,376 KB
testcase_14 AC 1 ms
5,376 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 2 ms
5,376 KB
testcase_17 AC 2 ms
5,376 KB
testcase_18 AC 1 ms
5,376 KB
testcase_19 AC 2 ms
5,376 KB
testcase_20 AC 2 ms
5,376 KB
testcase_21 AC 2 ms
5,376 KB
testcase_22 AC 2 ms
5,376 KB
testcase_23 AC 1 ms
5,376 KB
testcase_24 AC 1 ms
5,376 KB
testcase_25 AC 1 ms
5,376 KB
testcase_26 AC 2 ms
5,376 KB
testcase_27 AC 8 ms
5,376 KB
testcase_28 AC 8 ms
5,376 KB
testcase_29 AC 7 ms
5,376 KB
testcase_30 AC 8 ms
5,376 KB
testcase_31 AC 7 ms
5,376 KB
testcase_32 AC 8 ms
5,376 KB
testcase_33 AC 8 ms
5,376 KB
testcase_34 AC 8 ms
5,376 KB
testcase_35 AC 8 ms
5,376 KB
testcase_36 AC 7 ms
5,376 KB
testcase_37 AC 8 ms
5,376 KB
testcase_38 AC 8 ms
5,376 KB
testcase_39 AC 8 ms
5,376 KB
testcase_40 AC 8 ms
5,376 KB
testcase_41 AC 9 ms
5,376 KB
testcase_42 AC 8 ms
5,376 KB
testcase_43 AC 8 ms
5,376 KB
testcase_44 AC 8 ms
5,376 KB
testcase_45 AC 8 ms
5,376 KB
testcase_46 AC 9 ms
5,376 KB
testcase_47 AC 1 ms
5,376 KB
testcase_48 AC 2 ms
5,376 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;

int get_int() {
  int c, n;
  while ((c = getchar()) < '0');
  n = c - '0';
  while ((c = getchar()) >= '0') n = n * 10 + (c - '0');
  return n;
}

struct FastDiv {
  FastDiv() {}
  FastDiv(u64 n) : m(n) {
    s = (n == 1) ? 0 : 64 + __lg(n - 1);
    x = ((__uint128_t(1) << s) + n - 1) / n;
  }
  friend u64 operator / (u64 n, FastDiv d) { return __uint128_t(n) * d.x >> d.s; }
  friend u64 operator % (u64 n, FastDiv d) { return n - n / d * d.m; }
  u64 m, s, x;
};

inline int pow_mod(int a, int e, FastDiv& mod) {
  int ret = 1;
  for (; e; e >>= 1, a = i64(a) * a % mod) if (e & 1) ret = i64(ret) * a % mod;
  return ret;
}

int msqrt(i64 a64, FastDiv& p, int z) {
  int a = a64 % p.m;
  if (a <= 1) return a;
  if (pow_mod(a, (p.m - 1) / 2, p) != 1) return -1;
  if ((p.m & 3) == 3) {
    return pow_mod(a, (p.m + 1) >> 2, p);
  }
  auto mul = [&] (int a, int b) { return i64(a) * b % p; };
  if ((p.m & 7) == 5) {
    int v = pow_mod(2 * a, (p.m - 5) >> 3, p);
    int i = mul(mul(2 * a, v), v);
    return mul(mul(a, v), i - 1);
  }
  int q = p.m - 1, s = __builtin_ctz(q);
  q >>= s;
  int c = pow_mod(z, q, p);
  int x = pow_mod(a, (q - 1) >> 1, p);
  int r = mul(a, x), t = mul(r, x);
  while (t != 1) {
    int s2 = 1;
    for (int tmp = mul(t, t); tmp != 1; tmp = mul(tmp, tmp), ++s2);
    int b = c;
    rep(_, s - s2 - 1) b = mul(b, b);
    c = mul(b, b), r = mul(r, b), t = mul(t, c);
    s = s2;
  }
  return r;
}

void solve() {
  int PP, R;
  while (~scanf("%d %d", &PP, &R)) {
    int Q; scanf("%d", &Q);
    auto P = FastDiv(PP);
    int inv2 = pow_mod(2, P.m - 2, P);
    rep(_, Q) {
      int a = get_int(), b = get_int(), c = get_int();
      int inv = pow_mod(a, P.m - 2, P);
      b = i64(b) * inv % P;
      c = i64(c) * inv % P;
      b = i64(b) * inv2 % P;
      int rhs = ((P.m - c) + i64(b) * b) % P;
      int r = msqrt(rhs, P, R);
      if (r < 0) {
        puts("-1");
      } else {
        int ans1 = (i64(r) + P.m - b) % P;
        int ans2 = (i64(P.m) - r + P.m - 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