結果
問題 | No.551 夏休みの思い出(2) |
ユーザー |
|
提出日時 | 2017-07-28 23:53:20 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 52 ms / 4,000 ms |
コード長 | 3,995 bytes |
コンパイル時間 | 1,232 ms |
コンパイル使用メモリ | 82,016 KB |
最終ジャッジ日時 | 2025-01-05 01:58:08 |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 47 |
コンパイルメッセージ
main.cpp: In function ‘int main()’: main.cpp:117:23: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 117 | int p, r, q; scanf("%d%d%d", &p, &r, &q); | ~~~~~^~~~~~~~~~~~~~~~~~~~~~ main.cpp:119:27: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 119 | int a, b, c; scanf("%d%d%d", &a, &b, &c); | ~~~~~^~~~~~~~~~~~~~~~~~~~~~
ソースコード
#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)x %= p; if (x < 0) x += p;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 pint 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;}