結果

問題 No.551 夏休みの思い出(2)
ユーザー Min_25Min_25
提出日時 2017-07-28 22:34:06
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 37 ms / 4,000 ms
コード長 2,778 bytes
コンパイル時間 1,068 ms
コンパイル使用メモリ 93,300 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-18 11:59:10
合計ジャッジ時間 2,517 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

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

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

inline int pow_mod(int a, int e, int 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, int p) {
  int a = a64 % p;
  if (a <= 1) return a;
  if (pow_mod(a, (p - 1) / 2, p) != 1) return -1;
  if ((p & 3) == 3) {
    return pow_mod(a, (p + 1) >> 2, p);
  }
  auto mul = [&] (int a, int b) { return i64(a) * b % p; };
  if ((p & 7) == 5) {
    int v = pow_mod(2 * a, (p - 5) >> 3, p);
    int i = mul(mul(2 * a, v), v);
    return mul(mul(a, v), i - 1);
  }
  int q = p - 1, s = __builtin_ctz(q);
  q >>= s;
  int c;
  for (int z = 2; ; ++z) {
    c = pow_mod(z, q, p);
    int t = c; rep(_, s - 1) t = mul(t, t);
    if (t != 1) break;
  }
  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 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 = msqrt(rhs, P);
      if (r < 0) {
        puts("-1");
      } else {
        int ans1 = (i64(r) + P - b) % P;
        int ans2 = (i64(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