結果
問題 | No.551 夏休みの思い出(2) |
ユーザー | 37zigen |
提出日時 | 2017-07-29 00:07:54 |
言語 | Java21 (openjdk 21) |
結果 |
WA
|
実行時間 | - |
コード長 | 3,792 bytes |
コンパイル時間 | 2,492 ms |
コンパイル使用メモリ | 78,896 KB |
実行使用メモリ | 66,472 KB |
最終ジャッジ日時 | 2024-10-10 06:59:39 |
合計ジャッジ時間 | 15,370 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | WA | - |
testcase_01 | WA | - |
testcase_02 | WA | - |
testcase_03 | WA | - |
testcase_04 | WA | - |
testcase_05 | WA | - |
testcase_06 | WA | - |
testcase_07 | WA | - |
testcase_08 | WA | - |
testcase_09 | WA | - |
testcase_10 | WA | - |
testcase_11 | WA | - |
testcase_12 | WA | - |
testcase_13 | WA | - |
testcase_14 | WA | - |
testcase_15 | WA | - |
testcase_16 | WA | - |
testcase_17 | WA | - |
testcase_18 | WA | - |
testcase_19 | WA | - |
testcase_20 | WA | - |
testcase_21 | WA | - |
testcase_22 | WA | - |
testcase_23 | WA | - |
testcase_24 | WA | - |
testcase_25 | WA | - |
testcase_26 | WA | - |
testcase_27 | TLE | - |
testcase_28 | -- | - |
testcase_29 | -- | - |
testcase_30 | -- | - |
testcase_31 | -- | - |
testcase_32 | -- | - |
testcase_33 | -- | - |
testcase_34 | -- | - |
testcase_35 | -- | - |
testcase_36 | -- | - |
testcase_37 | -- | - |
testcase_38 | -- | - |
testcase_39 | -- | - |
testcase_40 | -- | - |
testcase_41 | -- | - |
testcase_42 | -- | - |
testcase_43 | -- | - |
testcase_44 | -- | - |
testcase_45 | -- | - |
testcase_46 | -- | - |
testcase_47 | -- | - |
testcase_48 | -- | - |
ソースコード
import java.util.ArrayList; import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { new Main().run(); } 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]; for (int i = 0; i < N; ++i) { A[i] = sc.nextInt(); B[i] = sc.nextInt(); C[i] = sc.nextInt(); B[i] = (int) inv(A[i], P); C[i] = (int) inv(C[i], P); A[i] = 1; long v = B[i] * B[i] - 4 * C[i]; v %= P; while (v < 0) v += P; if (pow(v, (P - 1) / 2, P) == 1) { v = calc(v, P); long ans = inv(2, P) * (-B[i] + v); ans %= P; while (ans < 0) ans += P; System.out.println(ans); } else { System.out.println(-1); } } // x^2+Bx+C=0; // A=(2)^(-1)*(-b+(b^2-4ac)^(1/2)) // x^2=tを解きたい。 // x^2=a^2 // x^2-a^2=0 // (x-b)^2-a^2=0 // (x-b-a)(x-b+a)=0 } // 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 x=ans+b (Ax-ans-b) 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 = (long) (-b - inv(gcd.val[1], mod) * gcd.val[0]); while (ans < 0) ans += mod; return ans; } } throw new AssertionError(); } static class Poly { long[] val = new long[10]; 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; } } 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; } void tr(Object... objects) { System.out.println(Arrays.deepToString(objects)); } }