結果

問題 No.751 Frac #2
ユーザー htensaihtensai
提出日時 2020-05-08 08:53:15
言語 Java21
(openjdk 21)
結果
AC  
実行時間 155 ms / 1,000 ms
コード長 1,762 bytes
コンパイル時間 2,481 ms
コンパイル使用メモリ 76,784 KB
実行使用メモリ 58,428 KB
最終ジャッジ日時 2023-09-16 08:20:14
合計ジャッジ時間 9,576 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 149 ms
56,984 KB
testcase_01 AC 149 ms
56,576 KB
testcase_02 AC 150 ms
56,732 KB
testcase_03 AC 151 ms
56,704 KB
testcase_04 AC 150 ms
56,912 KB
testcase_05 AC 150 ms
56,784 KB
testcase_06 AC 151 ms
56,548 KB
testcase_07 AC 151 ms
56,828 KB
testcase_08 AC 153 ms
56,800 KB
testcase_09 AC 150 ms
56,428 KB
testcase_10 AC 153 ms
56,880 KB
testcase_11 AC 151 ms
56,624 KB
testcase_12 AC 150 ms
56,820 KB
testcase_13 AC 149 ms
56,640 KB
testcase_14 AC 150 ms
56,864 KB
testcase_15 AC 150 ms
56,764 KB
testcase_16 AC 152 ms
56,976 KB
testcase_17 AC 150 ms
56,784 KB
testcase_18 AC 150 ms
56,628 KB
testcase_19 AC 149 ms
57,232 KB
testcase_20 AC 150 ms
57,196 KB
testcase_21 AC 149 ms
56,696 KB
testcase_22 AC 149 ms
56,752 KB
testcase_23 AC 150 ms
56,436 KB
testcase_24 AC 149 ms
56,812 KB
testcase_25 AC 150 ms
56,628 KB
testcase_26 AC 149 ms
56,788 KB
testcase_27 AC 150 ms
56,856 KB
testcase_28 AC 155 ms
56,892 KB
testcase_29 AC 155 ms
56,544 KB
testcase_30 AC 150 ms
57,040 KB
testcase_31 AC 148 ms
56,796 KB
testcase_32 AC 149 ms
56,648 KB
testcase_33 AC 150 ms
58,428 KB
testcase_34 AC 149 ms
56,696 KB
testcase_35 AC 150 ms
56,628 KB
testcase_36 AC 149 ms
56,600 KB
testcase_37 AC 149 ms
56,824 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) {
		            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