結果
| 問題 |
No.443 GCD of Permutation
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2016-11-12 01:47:26 |
| 言語 | Java (openjdk 23) |
| 結果 |
AC
|
| 実行時間 | 203 ms / 1,000 ms |
| コード長 | 846 bytes |
| コンパイル時間 | 2,994 ms |
| コンパイル使用メモリ | 77,304 KB |
| 実行使用メモリ | 43,168 KB |
| 最終ジャッジ日時 | 2024-10-13 23:47:53 |
| 合計ジャッジ時間 | 8,010 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 28 |
ソースコード
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 < i; ++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;
}
}