結果

問題 No.186 中華風 (Easy)
ユーザー ぴろずぴろず
提出日時 2015-04-19 23:39:35
言語 Java19
(openjdk 21)
結果
AC  
実行時間 129 ms / 2,000 ms
コード長 1,381 bytes
コンパイル時間 3,406 ms
コンパイル使用メモリ 73,424 KB
実行使用メモリ 55,936 KB
最終ジャッジ日時 2023-09-27 00:43:20
合計ジャッジ時間 7,461 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 123 ms
55,752 KB
testcase_01 AC 121 ms
55,612 KB
testcase_02 AC 122 ms
55,628 KB
testcase_03 AC 119 ms
55,700 KB
testcase_04 AC 123 ms
55,612 KB
testcase_05 AC 124 ms
55,680 KB
testcase_06 AC 119 ms
55,572 KB
testcase_07 AC 119 ms
55,776 KB
testcase_08 AC 120 ms
55,368 KB
testcase_09 AC 119 ms
55,712 KB
testcase_10 AC 122 ms
55,656 KB
testcase_11 AC 123 ms
55,700 KB
testcase_12 AC 125 ms
55,296 KB
testcase_13 AC 127 ms
55,640 KB
testcase_14 AC 124 ms
55,832 KB
testcase_15 AC 123 ms
55,748 KB
testcase_16 AC 126 ms
55,368 KB
testcase_17 AC 122 ms
55,624 KB
testcase_18 AC 121 ms
55,820 KB
testcase_19 AC 123 ms
55,748 KB
testcase_20 AC 129 ms
55,624 KB
testcase_21 AC 127 ms
55,712 KB
testcase_22 AC 124 ms
55,936 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);
		}else if(x[0] == 0) {
			if (x[1] > 0) {
				System.out.println(x[1]);
			}else{
				System.out.println(-1);
			}
		}else{
			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