結果

問題 No.751 Frac #2
ユーザー htensai
提出日時 2020-05-08 08:53:15
言語 Java
(openjdk 23)
結果
AC  
実行時間 158 ms / 1,000 ms
コード長 1,762 bytes
コンパイル時間 2,105 ms
コンパイル使用メモリ 80,472 KB
実行使用メモリ 55,144 KB
最終ジャッジ日時 2024-07-03 09:46:21
合計ジャッジ時間 8,889 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 36
権限があれば一括ダウンロードができます

ソースコード

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