結果
| 問題 |
No.551 夏休みの思い出(2)
|
| コンテスト | |
| ユーザー |
rsk0315
|
| 提出日時 | 2019-03-30 04:02:04 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 78 ms / 4,000 ms |
| コード長 | 1,859 bytes |
| コンパイル時間 | 528 ms |
| コンパイル使用メモリ | 57,072 KB |
| 最終ジャッジ日時 | 2025-01-07 00:37:52 |
|
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 47 |
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:63:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
63 | scanf("%jd %jd %zu", &P, &R, &Q);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
main.cpp:67:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
67 | scanf("%jd %jd %jd", &A, &B, &C);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
ソースコード
#include <cstdio>
#include <cstdint>
#include <vector>
#include <set>
intmax_t modpow(intmax_t base, intmax_t iexp, intmax_t mod) {
intmax_t res = 1;
for (intmax_t dbl = base; iexp; iexp >>= 1) {
if (iexp & 1) (res *= dbl) %= mod;
(dbl *= dbl) %= mod;
}
return res;
}
intmax_t moddiv(intmax_t n, intmax_t d, intmax_t mod) {
return n * modpow(d, mod-2, mod) % mod;
}
std::vector<intmax_t> modsqrt(intmax_t n, intmax_t p) {
if (p % 4 == 3 && false) {
intmax_t r = modpow(n, (p+1)/4, p);
if (r*r % p == n) return {r, p-r};
return {};
}
intmax_t s = __builtin_ctzll(p-1);
intmax_t q = (p-1) >> s;
intmax_t z;
for (z = 2; z < p; ++z)
if (modpow(z, (p-1)/2, p) == p-1) break;
intmax_t m = s;
intmax_t c = modpow(z, q, p);
intmax_t t = modpow(n, q, p);
intmax_t r = modpow(n, (q+1)/2, p);
while (true) {
if (t == 0) return {0};
if (t == 1) return {r, p-r};
intmax_t i = 0;
for (intmax_t tt = t; tt != 1; ++i)
(tt *= tt) %= p;
if (i == m) return {};
intmax_t b = c;
for (intmax_t j = 0; j < m-i-1; ++j)
(b *= b) %= p;
m = i;
c = b * b % p;
(t *= c) %= p;
(r *= b) %= p;
}
}
int main() {
intmax_t P, R;
size_t Q;
scanf("%jd %jd %zu", &P, &R, &Q);
for (size_t i = 0; i < Q; ++i) {
intmax_t A, B, C;
scanf("%jd %jd %jd", &A, &B, &C);
// (-b +- sqrt(b*b-4*a*c)) / (2*a)
std::set<intmax_t> sol;
intmax_t S = (B*B - 4*A*C) % P;
if (S < 0) S += P;
for (intmax_t s: modsqrt(S, P)) {
intmax_t n = (s - B) % P;
if (n < 0) n += P;
intmax_t a = 2*A % P;
sol.insert(moddiv(n, a, P));
}
if (sol.empty()) {
puts("-1");
} else {
bool out = false;
for (auto x: sol) {
printf("%s%jd", out? " ":"", x);
out = true;
}
puts("");
}
}
}
rsk0315