結果

問題 No.443 GCD of Permutation
ユーザー hermione17
提出日時 2016-11-12 00:18:12
言語 Java
(openjdk 23)
結果
WA  
実行時間 -
コード長 2,058 bytes
コンパイル時間 4,004 ms
コンパイル使用メモリ 79,948 KB
実行使用メモリ 54,972 KB
最終ジャッジ日時 2024-11-25 10:36:41
合計ジャッジ時間 8,127 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 20 WA * 8
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.PrintStream;
import java.util.Scanner;


public class Y443 {
	Scanner in = new Scanner(System.in);
	PrintStream out = new PrintStream(System.out);

	Y443() throws Exception {
		String s = in.next();
		
		boolean[] flag = new boolean[10];
		
		if (isRepetitive(s)) {
			out.println(s);
			return;
		}
		
		if (s.length() == 2) {
			int a = Integer.parseInt(s);
			int b = Integer.parseInt("" + s.charAt(1) + s.charAt(0));
			
			out.println(gcd(a, b));
			return;
		}

		if (s.length() == 3) {
			int a = Integer.parseInt(s);
			int b = Integer.parseInt("" + s.charAt(0) + s.charAt(2) + s.charAt(1));
			int c = Integer.parseInt("" + s.charAt(1) + s.charAt(0) + s.charAt(2));
			int d = Integer.parseInt("" + s.charAt(1) + s.charAt(2) + s.charAt(0));
			int e = Integer.parseInt("" + s.charAt(2) + s.charAt(0) + s.charAt(1));
			int f = Integer.parseInt("" + s.charAt(2) + s.charAt(1) + s.charAt(0));
			long ans = gcd(a, gcd(b, gcd(c, gcd(d, gcd(e, f)))));
			out.println(ans);
			return;
		}
		
		int sum = 0;
		for (int i = 0; i < s.length(); i++) {
			sum += s.charAt(i) - '0';
		}

		if (sum % 9 == 0) {
			flag[9] = true;
		}
		if (sum % 3 == 0) {
			flag[3] = true;
		}
		
		for (int i = 1; i < 10; i++) {
			if (i == 6) continue;
			if (allDigitsDivided(s, i)) {
				flag[i] = true;
			}
		}
		
		if (flag[4]) flag[2] = false;
		if (flag[8]) flag[4] = false;
		if (flag[9]) flag[3] = false;
		
		int ans = 1;
		for (int i = 0; i < 10; i++) {
			if (flag[i]) {
				ans *= i;
			}
		}
		
		out.println(ans);
	}
	
	boolean isRepetitive(String s) {
		for (int i = 0; i < s.length(); i++) {
			if (s.charAt(0) != s.charAt(i)) {
				return false;
			}
		}
		return true;
	}

	boolean allDigitsDivided(String s, int x) {
		for (int i = 0; i < s.length(); i++) {
			int y = s.charAt(i) - '0';
			if (y % x > 0) {
				return false;
			}
		}
		return true;
	}
	
	long gcd(long x, long y) {
		while (y > 0) {
			x %= y;
			long t = x; x = y; y = t;
		}
		return x;
	}

	public static void main(String argv[]) throws Exception {
		new Y443();
	}
}
0