結果

問題 No.443 GCD of Permutation
ユーザー 37zigen
提出日時 2016-11-12 01:46:13
言語 Java
(openjdk 23)
結果
AC  
実行時間 177 ms / 1,000 ms
コード長 847 bytes
コンパイル時間 3,429 ms
コンパイル使用メモリ 77,364 KB
実行使用メモリ 42,936 KB
最終ジャッジ日時 2024-10-13 23:46:14
合計ジャッジ時間 8,277 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 28
権限があれば一括ダウンロードができます

ソースコード

diff #

package yukicoder;

import java.math.BigInteger;
import java.util.Scanner;

public class Q443 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String s = sc.next();
		char[] N = s.toCharArray();
		BigInteger ori = new BigInteger(s);
		if (N.length == 1) {
			System.out.println(N);
			return;
		}

		int[] count = new int[10];
		for (int i = 0; i < N.length; ++i) {
			++count[N[i] - '0'];
		}
		int gcd = 0;
		for (int i = 0; i < 10; ++i) {
			for (int j = 0; j < 10; ++j) {
				--count[i];
				--count[j];
				if (count[i] >= 0 && count[j] >= 0) {
					gcd = gcd(9 * (i - j), gcd);
				}
				++count[i];
				++count[j];
			}
		}
		
		System.out.println(ori.gcd(BigInteger.valueOf(gcd)));

	}

	static int gcd(int a, int b) {
		while (a > 0) {
			b %= a;
			int d = b;
			b = a;
			a = d;
		}
		return b;
	}
}
0