結果

問題 No.551 夏休みの思い出(2)
ユーザー 37zigen37zigen
提出日時 2017-07-29 03:50:03
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,603 ms / 4,000 ms
コード長 4,331 bytes
コンパイル時間 3,510 ms
コンパイル使用メモリ 87,116 KB
実行使用メモリ 194,724 KB
最終ジャッジ日時 2024-10-10 21:01:57
合計ジャッジ時間 36,705 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 80 ms
50,748 KB
testcase_01 AC 82 ms
51,248 KB
testcase_02 AC 92 ms
52,776 KB
testcase_03 AC 117 ms
51,656 KB
testcase_04 AC 134 ms
52,572 KB
testcase_05 AC 159 ms
52,756 KB
testcase_06 AC 166 ms
53,028 KB
testcase_07 AC 84 ms
52,692 KB
testcase_08 AC 82 ms
50,740 KB
testcase_09 AC 80 ms
51,112 KB
testcase_10 AC 85 ms
51,184 KB
testcase_11 AC 82 ms
50,908 KB
testcase_12 AC 81 ms
50,892 KB
testcase_13 AC 86 ms
52,248 KB
testcase_14 AC 93 ms
52,588 KB
testcase_15 AC 80 ms
50,832 KB
testcase_16 AC 91 ms
52,652 KB
testcase_17 AC 289 ms
75,228 KB
testcase_18 AC 661 ms
114,772 KB
testcase_19 AC 624 ms
114,744 KB
testcase_20 AC 652 ms
114,664 KB
testcase_21 AC 776 ms
147,284 KB
testcase_22 AC 171 ms
60,320 KB
testcase_23 AC 252 ms
65,896 KB
testcase_24 AC 437 ms
100,448 KB
testcase_25 AC 281 ms
75,124 KB
testcase_26 AC 434 ms
100,388 KB
testcase_27 AC 1,132 ms
152,608 KB
testcase_28 AC 1,495 ms
176,912 KB
testcase_29 AC 1,045 ms
147,992 KB
testcase_30 AC 1,138 ms
167,300 KB
testcase_31 AC 1,045 ms
147,988 KB
testcase_32 AC 1,100 ms
149,272 KB
testcase_33 AC 1,210 ms
178,220 KB
testcase_34 AC 1,234 ms
184,896 KB
testcase_35 AC 1,441 ms
176,352 KB
testcase_36 AC 1,482 ms
175,992 KB
testcase_37 AC 1,485 ms
194,724 KB
testcase_38 AC 1,410 ms
190,464 KB
testcase_39 AC 1,270 ms
190,784 KB
testcase_40 AC 1,311 ms
190,696 KB
testcase_41 AC 1,124 ms
153,556 KB
testcase_42 AC 1,434 ms
190,628 KB
testcase_43 AC 1,585 ms
176,764 KB
testcase_44 AC 1,603 ms
176,820 KB
testcase_45 AC 1,269 ms
186,016 KB
testcase_46 AC 1,431 ms
190,612 KB
testcase_47 AC 81 ms
51,256 KB
testcase_48 AC 82 ms
50,740 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.NoSuchElementException;

public class Main {
	public static void main(String[] args) {
		new Main().run();
	}

	static long time = 0;

	public void run() {
		Scanner sc = new Scanner();
		int P = sc.nextInt();
		int R = sc.nextInt();
		int N = sc.nextInt();
		long[] B = new long[N];
		long[] C = new long[N];
		PrintWriter pw = new PrintWriter(System.out);
		int div = 1;
		while (P / div > 1e6) {
			++div;
		}
		HashMap<Long, Long> map = new HashMap<>();
		long cur = 1;
		long g = pow(R, div, P);
		for (long i = 0; i * div < P; ++i) {
			if (!map.containsKey(cur))
				map.put(cur, i * div);
			cur = (cur * g) % P;
		}
		long inv2 = inv(2, P);
		for (int i = 0; i < N; ++i) {
			long A = sc.nextLong();
			B[i] = sc.nextInt();
			C[i] = sc.nextInt();
			long invA = inv(A, P);
			B[i] = invA * B[i] % P;
			C[i] = invA * C[i] % P;
			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) {
				if (v != 0) {
					v = calc(v, P, map, div, R);
					if (v % 2 == 1) {
						pw.println(-1);
					} else {
						v = pow(R, v / 2, 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, HashMap<Long, Long> map, long div, long R) {
		long cur = a;
		long inv = inv(R, mod);
		for (int i = 0; i < div; ++i) {
			if (map.containsKey(cur)) {
				long v = map.get(cur);
				return (v + i);
			}
			cur = (cur * inv) % mod;
		}
		throw new AssertionError();
	}

	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));
	}

	class Scanner {
		private final InputStream in = System.in;
		private final byte[] buffer = new byte[1024];
		private int ptr = 0;
		private int buflen = 0;

		private boolean hasNextByte() {
			if (ptr < buflen) {
				return true;
			} else {
				ptr = 0;
				try {
					buflen = in.read(buffer);
				} catch (IOException e) {
					e.printStackTrace();
				}
				if (buflen <= 0) {
					return false;
				}
			}
			return true;
		}

		private int readByte() {
			if (hasNextByte())
				return buffer[ptr++];
			else
				return -1;
		}

		private boolean isPrintableChar(int c) {
			return 33 <= c && c <= 126;
		}

		private void skipUnprintable() {
			while (hasNextByte() && !isPrintableChar(buffer[ptr]))
				ptr++;
		}

		public boolean hasNext() {
			skipUnprintable();
			return hasNextByte();
		}

		public String next() {
			if (!hasNext())
				throw new NoSuchElementException();
			StringBuilder sb = new StringBuilder();
			int b = readByte();
			while (isPrintableChar(b)) {
				sb.appendCodePoint(b);
				b = readByte();
			}
			return sb.toString();
		}

		public long nextLong() {
			if (!hasNext())
				throw new NoSuchElementException();
			long n = 0;
			boolean minus = false;
			int b = readByte();
			if (b == '-') {
				minus = true;
				b = readByte();
			}
			if (b < '0' || '9' < b) {
				throw new NumberFormatException();
			}
			while (true) {
				if ('0' <= b && b <= '9') {
					n *= 10;
					n += b - '0';
				} else if (b == -1 || !isPrintableChar(b)) {
					return minus ? -n : n;
				} else {
					throw new NumberFormatException();
				}
				b = readByte();
			}
		}

		public int nextInt() {
			return (int) nextLong();
		}
	}
}
0