結果

問題 No.551 夏休みの思い出(2)
ユーザー 37zigen37zigen
提出日時 2017-07-29 15:09:57
言語 Java21
(openjdk 21)
結果
AC  
実行時間 274 ms / 4,000 ms
コード長 4,931 bytes
コンパイル時間 2,521 ms
コンパイル使用メモリ 84,820 KB
実行使用メモリ 53,984 KB
最終ジャッジ日時 2024-10-10 19:07:21
合計ジャッジ時間 11,896 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 95 ms
52,552 KB
testcase_01 AC 85 ms
51,152 KB
testcase_02 AC 100 ms
51,152 KB
testcase_03 AC 118 ms
51,700 KB
testcase_04 AC 136 ms
52,576 KB
testcase_05 AC 163 ms
52,728 KB
testcase_06 AC 172 ms
53,812 KB
testcase_07 AC 89 ms
52,532 KB
testcase_08 AC 86 ms
51,212 KB
testcase_09 AC 84 ms
51,172 KB
testcase_10 AC 90 ms
50,804 KB
testcase_11 AC 86 ms
51,212 KB
testcase_12 AC 89 ms
52,640 KB
testcase_13 AC 90 ms
50,764 KB
testcase_14 AC 90 ms
50,852 KB
testcase_15 AC 89 ms
52,532 KB
testcase_16 AC 83 ms
51,316 KB
testcase_17 AC 98 ms
52,480 KB
testcase_18 AC 95 ms
52,564 KB
testcase_19 AC 90 ms
52,580 KB
testcase_20 AC 88 ms
52,608 KB
testcase_21 AC 87 ms
50,848 KB
testcase_22 AC 87 ms
50,988 KB
testcase_23 AC 93 ms
51,272 KB
testcase_24 AC 90 ms
50,752 KB
testcase_25 AC 89 ms
52,504 KB
testcase_26 AC 86 ms
51,224 KB
testcase_27 AC 232 ms
53,696 KB
testcase_28 AC 218 ms
53,076 KB
testcase_29 AC 200 ms
53,448 KB
testcase_30 AC 242 ms
53,168 KB
testcase_31 AC 206 ms
53,272 KB
testcase_32 AC 233 ms
53,004 KB
testcase_33 AC 234 ms
53,760 KB
testcase_34 AC 219 ms
53,692 KB
testcase_35 AC 209 ms
53,248 KB
testcase_36 AC 232 ms
53,856 KB
testcase_37 AC 235 ms
53,852 KB
testcase_38 AC 239 ms
53,984 KB
testcase_39 AC 215 ms
53,544 KB
testcase_40 AC 226 ms
53,568 KB
testcase_41 AC 274 ms
53,960 KB
testcase_42 AC 210 ms
53,416 KB
testcase_43 AC 223 ms
53,164 KB
testcase_44 AC 216 ms
53,268 KB
testcase_45 AC 228 ms
53,876 KB
testcase_46 AC 221 ms
53,196 KB
testcase_47 AC 95 ms
51,092 KB
testcase_48 AC 83 ms
51,096 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();
		PrintWriter pw = new PrintWriter(System.out);
		int P = sc.nextInt();
		int R = sc.nextInt();
		int N = sc.nextInt();
		long[] B = new long[N];
		long[] C = new long[N];
		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) {
				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 p) {
		if (a == 0)
			return 0;
		if (a == 1)
			return 1;
		long s = 0;
		long tmp = p - 1;
		while (tmp % 2 == 0) {
			++s;
			tmp /= 2;
		}
		long Q = tmp;
		long z = 1;
		while (pow(z, (p - 1) / 2, p) != p - 1) {
			++z;
		}
		long M = s;
		long c = pow(z, Q, p);
		long t = pow(a, Q, p);
		long R = pow(a, (Q + 1) / 2, p);
		while (t != 1) {
//			if (pow(t, pow(2, M - 1, p - 1), p) != 1)
//				throw new AssertionError();
//			if (pow(c, pow(2, M - 1, p - 1), p) != p - 1)
//				throw new AssertionError(M);
			long cur = t;
			int i;
			for (i = 1; i < M; ++i) {
				cur = cur * cur % p;
				if (cur == 1)
					break;
			}
			if (cur != 1) {
				throw new AssertionError();
			}
			long b = pow(c, pow(2, M - i - 1, p - 1), p);
			M = i;
			c = b * b % p;
			t = t * b % p * b % p;
			R = R * b % p;
		}
		if (R * R % p != a) {
			throw new AssertionError();
		}
		return R;
	}

	static long[] mul(long[] u, long[] v, long b, long a, long p) {
		long[] ret = new long[3];
		for (int i = 0; i < 2; ++i) {
			for (int j = 0; j < 2; ++j) {
				ret[i + j] += u[i] * v[j];
				ret[i + j] %= p;
			}
		}
		ret[0] += ret[2] * (b * b - a);
		ret[0] %= p;
		for (int i = 0; i < ret.length; ++i) {
			while (ret[i] < 0)
				ret[i] += p;
		}
		return Arrays.copyOf(ret, 2);
	}

	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