結果
| 問題 | No.443 GCD of Permutation |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2026-10-20 22:41:19 |
| 言語 | C++23(gcc16) (gcc 16.2.0 + boost 1.92.0 + ACL) |
| 結果 |
AC
不安定
|
| 実行時間 | 2 ms / 1,000 ms |
| + 473µs | |
| コード長 | 943 bytes |
| 記録 | |
| コンパイル時間 | 3,061 ms |
| コンパイル使用メモリ | 176,692 KB |
| 実行使用メモリ | 6,400 KB |
| 最終ジャッジ日時 | 2026-10-20 22:41:50 |
| 合計ジャッジ時間 | 5,658 ms |
|
ジャッジサーバーID (参考情報) |
judge4_0 / judge1_0 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 30 |
ソースコード
#include <algorithm>
#include <iostream>
#include <numeric>
#include <string>
int main() {
using namespace std;
auto divide = [&](string s, int d) -> int {
int res = 0;
for (char c : s) { res = (res * 10 + (c - '0')) % d; }
return res == 0;
};
auto perm_gcd = [&](string s) -> string {
int n = s.size();
sort(s.begin(), s.end());
if (s[0] == s[n - 1]) { return s; }
int dif_gcd = 0;
for (int i = 0; i < n - 1; i++) {
int c = s[i] - '0';
int nc = s[i + 1] - '0';
dif_gcd = (dif_gcd == 0) ? nc - c : gcd(dif_gcd, nc - c);
}
if (divide(s, dif_gcd * 9)) { return to_string(dif_gcd * 9); }
if (divide(s, dif_gcd * 3)) { return to_string(dif_gcd * 3); }
if (divide(s, dif_gcd)) { return to_string(dif_gcd); }
return "1";
};
string s;
cin >> s;
cout << perm_gcd(s) << endl;
}