結果

問題 No.443 GCD of Permutation
ユーザー hermione17hermione17
提出日時 2016-11-12 01:03:56
言語 Java21
(openjdk 21)
結果
AC  
実行時間 137 ms / 1,000 ms
コード長 1,191 bytes
コンパイル時間 3,422 ms
コンパイル使用メモリ 74,032 KB
実行使用メモリ 56,552 KB
最終ジャッジ日時 2023-08-04 02:41:28
合計ジャッジ時間 9,010 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 115 ms
55,904 KB
testcase_01 AC 118 ms
53,872 KB
testcase_02 AC 119 ms
56,096 KB
testcase_03 AC 118 ms
56,024 KB
testcase_04 AC 119 ms
55,784 KB
testcase_05 AC 120 ms
55,768 KB
testcase_06 AC 118 ms
55,720 KB
testcase_07 AC 118 ms
55,592 KB
testcase_08 AC 119 ms
55,488 KB
testcase_09 AC 118 ms
55,356 KB
testcase_10 AC 119 ms
56,056 KB
testcase_11 AC 115 ms
55,400 KB
testcase_12 AC 117 ms
56,240 KB
testcase_13 AC 117 ms
56,064 KB
testcase_14 AC 135 ms
55,788 KB
testcase_15 AC 131 ms
55,740 KB
testcase_16 AC 123 ms
55,936 KB
testcase_17 AC 125 ms
55,632 KB
testcase_18 AC 135 ms
55,836 KB
testcase_19 AC 124 ms
56,168 KB
testcase_20 AC 135 ms
56,424 KB
testcase_21 AC 132 ms
55,964 KB
testcase_22 AC 118 ms
55,564 KB
testcase_23 AC 119 ms
55,784 KB
testcase_24 AC 118 ms
55,840 KB
testcase_25 AC 136 ms
56,552 KB
testcase_26 AC 136 ms
55,472 KB
testcase_27 AC 131 ms
55,464 KB
testcase_28 AC 137 ms
55,580 KB
testcase_29 AC 134 ms
55,660 KB
testcase_30 AC 136 ms
55,712 KB
testcase_31 AC 136 ms
55,780 KB
権限があれば一括ダウンロードができます

ソースコード

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;
		}
		
		for (int i = 0; i < s.length(); i++) {
			flag[s.charAt(i) - '0'] = true;
		}
		
		long g = 0;
		
		for (int i = 0; i < 10; i++) {
			for (int j = i + 1; j < 10; j++) {
				if (flag[i] && flag[j]) {
					g = gcd(g, 9 * (j - i));
				}
			}
		}
		
	
		for (int i = (int)g; i>0; i--) {
			if (g % i == 0 && divisible(s, i)) {
				out.println(i);
				return;
			}
		}
	}
	
	boolean isRepetitive(String s) {
		for (int i = 0; i < s.length(); i++) {
			if (s.charAt(0) != s.charAt(i)) {
				return false;
			}
		}
		return true;
	}

	boolean divisible(String s, int x) {
		int r = 0;
		for (int i = 0; i < s.length(); i++) {
			r = (r * 10 + s.charAt(i) - '0') % x;
		}
		return r == 0;
	}
	
	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