結果
問題 | No.551 夏休みの思い出(2) |
ユーザー | kimiyuki |
提出日時 | 2017-07-28 23:48:20 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
RE
|
実行時間 | - |
コード長 | 3,964 bytes |
コンパイル時間 | 985 ms |
コンパイル使用メモリ | 85,852 KB |
実行使用メモリ | 6,824 KB |
最終ジャッジ日時 | 2024-10-10 05:41:03 |
合計ジャッジ時間 | 7,473 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | RE | - |
testcase_01 | RE | - |
testcase_02 | RE | - |
testcase_03 | RE | - |
testcase_04 | RE | - |
testcase_05 | RE | - |
testcase_06 | RE | - |
testcase_07 | RE | - |
testcase_08 | RE | - |
testcase_09 | RE | - |
testcase_10 | RE | - |
testcase_11 | RE | - |
testcase_12 | RE | - |
testcase_13 | RE | - |
testcase_14 | RE | - |
testcase_15 | RE | - |
testcase_16 | RE | - |
testcase_17 | RE | - |
testcase_18 | RE | - |
testcase_19 | RE | - |
testcase_20 | RE | - |
testcase_21 | RE | - |
testcase_22 | RE | - |
testcase_23 | RE | - |
testcase_24 | RE | - |
testcase_25 | RE | - |
testcase_26 | RE | - |
testcase_27 | RE | - |
testcase_28 | RE | - |
testcase_29 | RE | - |
testcase_30 | RE | - |
testcase_31 | RE | - |
testcase_32 | RE | - |
testcase_33 | RE | - |
testcase_34 | RE | - |
testcase_35 | RE | - |
testcase_36 | RE | - |
testcase_37 | RE | - |
testcase_38 | RE | - |
testcase_39 | RE | - |
testcase_40 | RE | - |
testcase_41 | RE | - |
testcase_42 | RE | - |
testcase_43 | RE | - |
testcase_44 | RE | - |
testcase_45 | RE | - |
testcase_46 | RE | - |
testcase_47 | AC | 2 ms
6,816 KB |
testcase_48 | AC | 2 ms
6,820 KB |
ソースコード
#include <cstdio> #include <vector> #include <algorithm> #include <array> #include <set> #include <map> #include <queue> #include <tuple> #include <unordered_set> #include <unordered_map> #include <functional> #include <cassert> #define repeat(i, n) for (int i = 0; (i) < int(n); ++(i)) #define repeat_from(i, m, n) for (int i = (m); (i) < int(n); ++(i)) #define repeat_reverse(i, n) for (int i = (n)-1; (i) >= 0; --(i)) #define repeat_from_reverse(i, m, n) for (int i = (n)-1; (i) >= int(m); --(i)) #define whole(f, x, ...) ([&](decltype((x)) whole) { return (f)(begin(whole), end(whole), ## __VA_ARGS__); })(x) #define unittest_name_helper(counter) unittest_ ## counter #define unittest_name(counter) unittest_name_helper(counter) #define unittest __attribute__((constructor)) void unittest_name(__COUNTER__) () using ll = long long; using namespace std; template <class T> inline void setmax(T & a, T const & b) { a = max(a, b); } template <class T> inline void setmin(T & a, T const & b) { a = min(a, b); } template <typename X, typename T> auto vectors(X x, T a) { return vector<T>(x, a); } template <typename X, typename Y, typename Z, typename... Zs> auto vectors(X x, Y y, Z z, Zs... zs) { auto cont = vectors(y, z, zs...); return vector<decltype(cont)>(x, cont); } ll powmod(ll x, ll y, ll p) { // O(log y) assert (0 <= x and x < p); assert (0 <= y); ll z = 1; for (ll i = 1; i <= y; i <<= 1) { if (y & i) z = z * x % p; x = x * x % p; } return z; } ll modinv(ll x, ll p) { // p must be a prime, O(log p) assert ((x % p + p) % p != 0); return powmod(x, p-2, p); } vector<bool> sieve_of_eratosthenes(int n) { // enumerate primes in [2,n] with O(n log log n) vector<bool> is_prime(n+1, true); is_prime[0] = is_prime[1] = false; for (int i = 2; i*i <= n; ++i) if (is_prime[i]) for (int k = i+i; k <= n; k += i) is_prime[k] = false; return is_prime; } vector<int> list_primes(int n) { auto is_prime = sieve_of_eratosthenes(n); vector<int> primes; for (int i = 2; i <= n; ++i) if (is_prime[i]) primes.push_back(i); return primes; } int legendre_symbol(int a, int p) { return powmod(a, (p - 1) / 2, p); // Euler's criterion } int modsqrt(int a, int p) { a %= p; if (a == 0) return 0; if (p == 2) return a; assert (p >= 3); if (legendre_symbol(a, p) != +1) return -1; int b = 1; while (legendre_symbol(b, p) == 1) { b += 1; } int e = 0; int m = p - 1; while (m % 2 == 0) { m /= 2; e += 1; } ll x = powmod(a, (m - 1) / 2, p); ll y = a * x % p * x % p; x = x * a % p; ll z = powmod(b, m, p); while (y != 1) { int j = 0; for (ll t = y; t != 1; t = t * t % p) ++ j; if (e <= j) return -1; z = powmod(z, 1ll << (e - j - 1), p); x =x * z % p; z =z * z % p; y =y * z % p; e = j; } assert (x * x % p == a); return x; } vector<int> solve_modeqn(int a, int b, int c, int p) { // ax^2 + bx + c = 0 mod p int d = (b *(ll) b - 4ll * a * c) % p; if (d < 0) d += p; int w = modsqrt(d, p); if (w == -1) return vector<int>(); vector<int> xs; xs.push_back((- b + w) *(ll) modinv(2 * a, p) % p); xs.push_back((- b - w) *(ll) modinv(2 * a, p) % p); if (xs[0] < 0) xs[0] += p; if (xs[1] < 0) xs[1] += p; whole(sort, xs); xs.erase(whole(unique, xs), xs.end()); return xs; } int main() { vector<int> primes = list_primes(1e5); int p, r, q; scanf("%d%d%d", &p, &r, &q); repeat (i, q) { int a, b, c; scanf("%d%d%d", &a, &b, &c); vector<int> xs = solve_modeqn(a, b, c, p); if (xs.empty()) { printf("-1\n"); } else { repeat (i, xs.size()) { printf("%d%c", xs[i], i + 1 == xs.size() ? '\n' : ' '); } } } return 0; }