結果

問題 No.186 中華風 (Easy)
ユーザー ぴろずぴろず
提出日時 2015-04-19 23:36:46
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 1,279 bytes
コンパイル時間 2,893 ms
コンパイル使用メモリ 78,348 KB
実行使用メモリ 56,368 KB
最終ジャッジ日時 2023-09-18 01:29:37
合計ジャッジ時間 6,853 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 125 ms
55,416 KB
testcase_01 AC 126 ms
55,392 KB
testcase_02 AC 124 ms
53,320 KB
testcase_03 AC 123 ms
55,920 KB
testcase_04 AC 124 ms
55,816 KB
testcase_05 AC 124 ms
55,620 KB
testcase_06 AC 123 ms
55,668 KB
testcase_07 AC 128 ms
55,920 KB
testcase_08 AC 123 ms
55,544 KB
testcase_09 AC 124 ms
55,928 KB
testcase_10 AC 124 ms
56,100 KB
testcase_11 AC 125 ms
55,816 KB
testcase_12 AC 125 ms
55,384 KB
testcase_13 AC 124 ms
55,356 KB
testcase_14 AC 124 ms
55,696 KB
testcase_15 AC 124 ms
54,024 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 125 ms
55,788 KB
testcase_19 AC 127 ms
55,976 KB
testcase_20 AC 124 ms
55,308 KB
testcase_21 AC 124 ms
55,620 KB
testcase_22 AC 124 ms
55,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package no186;

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		long[] a = new long[3];
		long[] b = new long[3];
		long[] m = new long[3];
		for(int i=0;i<3;i++) {
			int x = sc.nextInt();
			int y = sc.nextInt();
			a[i] = 1;
			b[i] = x;
			m[i] = y;
		}
		long[] x = Mod.linearCongruence(a, b, m);
		if (x == null || x[0] == 0) {
			System.out.println(-1);
			return;
		}
		System.out.println((x[0]%x[1]+x[1])%x[1]);
	}
}
class Mod {
	public static long inverse(long a,long mod) {
		long b = mod, u = 1, v = 0;
		while(b > 0) {
			long temp;
			long t = a / b;
			a -= t * b;
			temp = a; a = b; b = temp;
			u -= t * v;
			temp = u; u = v; v = temp;
		}
		return (u % mod + mod) % mod;
	}
	public static long[] linearCongruence(long[] A,long[] B,long[] M) {
		long x = 0;
		long m = 1;
		for(int i=0;i<A.length;i++) {
			long a = A[i] * m;
			long b = B[i] - A[i] * x;
			long d = gcd(M[i],a);
			if (b % d != 0) {
				return null;
			}
			long t = b / d * inverse(a / d, M[i] / d) % (M[i] / d);
			x = x + m * t;
			m *= M[i] / d;
		}
		long[] ret = {x%m, m};
		return ret;
	}
	public static long gcd(long a,long b) {
		while(b!=0) {
			long r = a%b;
			a = b;
			b = r;
		}
		return a;
	}
}
0