結果

問題 No.186 中華風 (Easy)
ユーザー ぴろずぴろず
提出日時 2015-04-19 23:33:02
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 1,249 bytes
コンパイル時間 2,076 ms
コンパイル使用メモリ 76,064 KB
実行使用メモリ 57,616 KB
最終ジャッジ日時 2023-09-18 01:27:00
合計ジャッジ時間 5,929 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 123 ms
53,700 KB
testcase_01 AC 124 ms
55,904 KB
testcase_02 AC 125 ms
55,676 KB
testcase_03 AC 125 ms
55,988 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 123 ms
55,936 KB
testcase_10 AC 129 ms
55,780 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 123 ms
55,884 KB
testcase_14 AC 122 ms
55,980 KB
testcase_15 AC 123 ms
55,900 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 125 ms
55,660 KB
testcase_20 AC 125 ms
55,620 KB
testcase_21 AC 127 ms
55,564 KB
testcase_22 AC 125 ms
55,740 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) {
			System.out.println(-1);
			return;
		}
		System.out.println(x[0]);
	}
}
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