結果

問題 No.443 GCD of Permutation
ユーザー hermione17hermione17
提出日時 2016-11-12 01:03:56
言語 Java21
(openjdk 21)
結果
AC  
実行時間 145 ms / 1,000 ms
コード長 1,191 bytes
コンパイル時間 4,373 ms
コンパイル使用メモリ 77,624 KB
実行使用メモリ 54,608 KB
最終ジャッジ日時 2024-04-22 01:11:54
合計ジャッジ時間 8,486 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 110 ms
53,720 KB
testcase_01 AC 99 ms
54,608 KB
testcase_02 AC 111 ms
54,096 KB
testcase_03 AC 101 ms
53,100 KB
testcase_04 AC 107 ms
53,732 KB
testcase_05 AC 100 ms
52,624 KB
testcase_06 AC 110 ms
53,984 KB
testcase_07 AC 99 ms
52,976 KB
testcase_08 AC 109 ms
54,384 KB
testcase_09 AC 110 ms
54,036 KB
testcase_10 AC 108 ms
54,048 KB
testcase_11 AC 110 ms
53,920 KB
testcase_12 AC 97 ms
52,708 KB
testcase_13 AC 110 ms
53,972 KB
testcase_14 AC 133 ms
54,172 KB
testcase_15 AC 126 ms
54,012 KB
testcase_16 AC 118 ms
54,044 KB
testcase_17 AC 121 ms
54,100 KB
testcase_18 AC 114 ms
52,896 KB
testcase_19 AC 115 ms
53,928 KB
testcase_20 AC 122 ms
54,036 KB
testcase_21 AC 125 ms
54,008 KB
testcase_22 AC 119 ms
54,444 KB
testcase_23 AC 99 ms
52,816 KB
testcase_24 AC 119 ms
54,132 KB
testcase_25 AC 132 ms
54,112 KB
testcase_26 AC 131 ms
54,016 KB
testcase_27 AC 130 ms
53,704 KB
testcase_28 AC 129 ms
54,236 KB
testcase_29 AC 119 ms
53,004 KB
testcase_30 AC 129 ms
54,076 KB
testcase_31 AC 145 ms
54,172 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