結果

問題 No.443 GCD of Permutation
コンテスト
ユーザー himueu
提出日時 2026-10-20 22:44:10
言語 C++23(gcc16)
(gcc 16.2.0 + boost 1.92.0 + ACL)
コンパイル:
g++-16 -O2 -fplugin=/usr/lib/yukicoder/noavx512-gcc16.so -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 2 ms / 1,000 ms
+ 140µs
コード長 917 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,996 ms
コンパイル使用メモリ 179,440 KB
実行使用メモリ 9,872 KB
最終ジャッジ日時 2026-10-20 22:44:19
合計ジャッジ時間 4,530 ms
ジャッジサーバーID
(参考情報)
judge6_0 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#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 = 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;
}
0