結果

問題 No.751 Frac #2
ユーザー htensaihtensai
提出日時 2020-05-08 08:47:47
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 1,789 bytes
コンパイル時間 2,066 ms
コンパイル使用メモリ 76,784 KB
実行使用メモリ 58,932 KB
最終ジャッジ日時 2023-09-16 08:18:26
合計ジャッジ時間 9,254 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 152 ms
56,772 KB
testcase_01 AC 150 ms
56,796 KB
testcase_02 AC 152 ms
56,664 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 150 ms
56,784 KB
testcase_08 AC 150 ms
56,836 KB
testcase_09 AC 152 ms
56,508 KB
testcase_10 WA -
testcase_11 AC 152 ms
56,840 KB
testcase_12 AC 159 ms
56,512 KB
testcase_13 WA -
testcase_14 AC 153 ms
54,848 KB
testcase_15 AC 155 ms
56,784 KB
testcase_16 AC 155 ms
57,128 KB
testcase_17 WA -
testcase_18 AC 153 ms
56,764 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 AC 152 ms
56,664 KB
testcase_23 AC 152 ms
56,792 KB
testcase_24 WA -
testcase_25 AC 151 ms
56,872 KB
testcase_26 WA -
testcase_27 AC 150 ms
56,704 KB
testcase_28 WA -
testcase_29 WA -
testcase_30 AC 150 ms
56,768 KB
testcase_31 WA -
testcase_32 AC 150 ms
56,748 KB
testcase_33 AC 151 ms
56,500 KB
testcase_34 WA -
testcase_35 AC 152 ms
56,932 KB
testcase_36 WA -
testcase_37 AC 150 ms
56,876 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
	public static void main (String[] args) {
		Scanner sc = new Scanner(System.in);
		int n1 = sc.nextInt();
		ArrayDeque<Integer> mothers = new ArrayDeque<>();
		ArrayDeque<Integer> children = new ArrayDeque<>();
		int x = sc.nextInt();
		boolean isMPlus = true;
		boolean isCPlus = true;
		if (x < 0) {
		    isCPlus ^= true;
		    x *= -1;
		}
		children.add(x);
		for (int i = 1; i < n1; i++) {
		    int y = sc.nextInt();
		    if (y < 0) {
		        isMPlus ^= true;
		        y *=  -1;
		    }
		    mothers.add(y);
		}
		int n2 = sc.nextInt();
		for (int i = 0; i < n2; i++) {
		    int y = sc.nextInt();
		    if (i % 2 == 0) {
		        if (y < 0) {
		            isMPlus ^= true;
		            y *= -1;
		        }
		        mothers.add(y);
		    } else {
		        if (y < 0) {
		            isCPlus ^= true;
		            y *= -1;
		        }
		        children.add(y);
		    }
		}
		long mother = 1;
		long child = 1;
		ArrayDeque<Integer> next = new ArrayDeque<>();
		while (mothers.size() > 0) {
		    int m = mothers.poll();
		    while (children.size() > 0) {
		        int c = children.poll();
		        int gcd = getGCD(m, c);
		        m /= gcd;
		        c /= gcd;
		        if (c != 1) {
		            next.add(c);
		        }
		        if (m == 1) {
		            next.add(c);
		            break;
		        }
		    }
		    if (m != 1) {
		        mother *= m;
		    }
		    children.addAll(next);
		    next.clear();
		}
		while (children.size() > 0) {
		    child *= children.poll();
		}
		if (isMPlus ^ isCPlus) {
		    child *= -1;
		}
		System.out.println(child + " " + mother);
	}
	
	static int getGCD(int x, int y) {
	    if (x % y == 0) {
	        return y;
	    } else {
	        return getGCD(y, x % y);
	    }
	}
}
0