結果
問題 | No.551 夏休みの思い出(2) |
ユーザー | 37zigen |
提出日時 | 2017-07-29 01:16:59 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 908 ms / 4,000 ms |
コード長 | 4,275 bytes |
コンパイル時間 | 2,652 ms |
コンパイル使用メモリ | 81,844 KB |
実行使用メモリ | 51,176 KB |
最終ジャッジ日時 | 2024-10-10 18:21:03 |
合計ジャッジ時間 | 25,596 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 158 ms
42,392 KB |
testcase_01 | AC | 178 ms
43,128 KB |
testcase_02 | AC | 231 ms
43,380 KB |
testcase_03 | AC | 295 ms
46,180 KB |
testcase_04 | AC | 317 ms
47,216 KB |
testcase_05 | AC | 391 ms
47,768 KB |
testcase_06 | AC | 404 ms
46,968 KB |
testcase_07 | AC | 185 ms
42,692 KB |
testcase_08 | AC | 185 ms
42,860 KB |
testcase_09 | AC | 189 ms
42,508 KB |
testcase_10 | AC | 186 ms
42,544 KB |
testcase_11 | AC | 198 ms
42,968 KB |
testcase_12 | AC | 182 ms
42,528 KB |
testcase_13 | AC | 189 ms
42,820 KB |
testcase_14 | AC | 186 ms
42,520 KB |
testcase_15 | AC | 186 ms
43,128 KB |
testcase_16 | AC | 182 ms
42,840 KB |
testcase_17 | AC | 191 ms
42,524 KB |
testcase_18 | AC | 194 ms
43,128 KB |
testcase_19 | AC | 188 ms
42,864 KB |
testcase_20 | AC | 194 ms
42,736 KB |
testcase_21 | AC | 218 ms
43,256 KB |
testcase_22 | AC | 202 ms
42,992 KB |
testcase_23 | AC | 216 ms
43,308 KB |
testcase_24 | AC | 233 ms
43,160 KB |
testcase_25 | AC | 191 ms
42,924 KB |
testcase_26 | AC | 191 ms
42,976 KB |
testcase_27 | AC | 856 ms
50,552 KB |
testcase_28 | AC | 619 ms
50,132 KB |
testcase_29 | AC | 835 ms
50,528 KB |
testcase_30 | AC | 862 ms
50,776 KB |
testcase_31 | AC | 605 ms
48,440 KB |
testcase_32 | AC | 843 ms
50,812 KB |
testcase_33 | AC | 631 ms
48,820 KB |
testcase_34 | AC | 792 ms
51,176 KB |
testcase_35 | AC | 613 ms
49,028 KB |
testcase_36 | AC | 625 ms
48,440 KB |
testcase_37 | AC | 637 ms
50,560 KB |
testcase_38 | AC | 908 ms
51,032 KB |
testcase_39 | AC | 631 ms
48,920 KB |
testcase_40 | AC | 653 ms
48,944 KB |
testcase_41 | AC | 846 ms
50,860 KB |
testcase_42 | AC | 642 ms
48,900 KB |
testcase_43 | AC | 617 ms
49,068 KB |
testcase_44 | AC | 622 ms
49,204 KB |
testcase_45 | AC | 632 ms
48,724 KB |
testcase_46 | AC | 895 ms
50,520 KB |
testcase_47 | AC | 151 ms
41,672 KB |
testcase_48 | AC | 158 ms
42,220 KB |
ソースコード
import java.io.PrintWriter; import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { new Main().run(); } static long time = 0; public void run() { Scanner sc = new Scanner(System.in); int P = sc.nextInt(); int R = sc.nextInt(); int N = sc.nextInt(); long[] A = new long[N]; long[] B = new long[N]; long[] C = new long[N]; PrintWriter pw = new PrintWriter(System.out); long inv2 = inv(2, P); for (int i = 0; i < N; ++i) { A[i] = sc.nextInt(); B[i] = sc.nextInt(); C[i] = sc.nextInt(); long invA = inv(A[i], P); B[i] = invA * B[i] % P; C[i] = invA * C[i] % P; A[i] = 1; long v = B[i] * B[i] - 4 * C[i]; v %= P; while (v < 0) v += P; if (v == 0 || pow(v, (P - 1) / 2, P) == 1) { v = calc(v, P); long[] ans = { inv2 * (-B[i] + v), inv2 * (-B[i] + P - v) }; for (int j = 0; j < ans.length; ++j) { ans[j] %= P; while (ans[j] < 0) ans[j] += P; } if (ans[0] > ans[1]) { long tmp = ans[0]; ans[0] = ans[1]; ans[1] = tmp; } if (ans[0] == ans[1]) { pw.println(ans[0]); } else { pw.println(ans[0] + " " + ans[1]); } } else { pw.println(-1); } } pw.close(); } // x^2=aとなるxを探す static long calc(long a, long mod) { for (int b = 0; b < 100; ++b) { Poly p1 = new Poly(mod); Poly p2 = new Poly(mod);// (x-b)^2-a=0 mod p p2.add(2, 1); p2.add(1, -2 * b); p2.add(0, b * b - a); p1.add(1, 1); p1 = p1.pow((mod - 1) / 2, p2); p1.add(0, -1); Poly gcd = p1.gcd(p2); if (gcd.deg() == 1) { long ans = (-b - inv(gcd.val[1], mod) * gcd.val[0]) % mod; while (ans < 0) ans += mod; return ans; } } throw new AssertionError(); } static class Poly { long[] val = new long[3]; long mod; public Poly(long mod) { this.mod = mod; } void add(int deg, long v) { val[deg] += v; while (val[deg] < 0) val[deg] += mod; val[deg] %= mod; } int deg() { int deg = val.length - 1; while (deg > 0 && val[deg] == 0) --deg; return deg; } Poly mul(Poly p) { Poly ret = new Poly(mod); for (int i = 0; i < this.val.length; ++i) { if (val[i] == 0) continue; for (int j = 0; j < p.val.length; ++j) { if (p.val[j] == 0) continue; ret.add(i + j, this.val[i] * p.val[j]); } } return ret; } Poly mod(Poly p) { Poly ret = new Poly(mod); Poly modPoly = new Poly(mod); for (int i = 0; i < val.length; ++i) { ret.add(i, val[i]); } int max = p.deg(); if (max == 0 && p.val[max] == 0) return new Poly(mod); long rev = (long) inv(p.val[max], mod); for (int i = 0; i < p.val.length; ++i) { modPoly.add(i, p.val[i] * rev); } for (int i = ret.val.length - 1; i >= max; --i) { if (ret.val[i] == 0) continue; long tmp = ret.val[i]; for (int j = 0; j <= max; ++j) { ret.add(i - j, -tmp % mod * modPoly.val[max - j] % mod); } } return ret; } Poly gcd(Poly p) { int deg1 = this.deg(); int deg2 = p.deg(); if (deg1 > deg2) { return p.gcd(this); } if (deg1 == 0 && val[deg1] == 0) return p; return p.mod(this).gcd(this); } Poly pow(long n, Poly polyMod) { int deg = this.deg(); if (val[deg] == 0) return new Poly(mod); Poly ret = new Poly(mod); ret.add(0, 1); Poly pow = new Poly(mod); for (int i = 0; i < val.length; ++i) { pow.add(i, val[i]); } for (; n > 0; n >>= 1, pow = pow.mul(pow), pow = pow.mod(polyMod)) { if (n % 2 == 1) { ret = ret.mul(pow); ret = ret.mod(polyMod); } } return ret; } } public static long inv(long a, long mod) { long b = mod; long p = 1, q = 0; while (b > 0) { long c = a / b; long d; d = a; a = b; b = d % b; d = p; p = q; q = d - c * q; } return p < 0 ? p + mod : p; } // static long inv(long a, long mod) { // return pow(a, mod - 2, mod); // } static long pow(long a, long n, long mod) { long ret = 1; for (; n > 0; n >>= 1, a = (a * a) % mod) { if (n % 2 == 1) { ret = (ret * a) % mod; } } return ret; } static void tr(Object... objects) { System.out.println(Arrays.deepToString(objects)); } }