結果

問題 No.443 GCD of Permutation
ユーザー face4face4
提出日時 2019-02-20 11:19:30
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 581 ms / 1,000 ms
コード長 948 bytes
コンパイル時間 491 ms
コンパイル使用メモリ 68,084 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-22 01:32:40
合計ジャッジ時間 2,984 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 1 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 1 ms
5,376 KB
testcase_14 AC 1 ms
5,376 KB
testcase_15 AC 24 ms
5,376 KB
testcase_16 AC 4 ms
5,376 KB
testcase_17 AC 11 ms
5,376 KB
testcase_18 AC 390 ms
5,376 KB
testcase_19 AC 18 ms
5,376 KB
testcase_20 AC 386 ms
5,376 KB
testcase_21 AC 53 ms
5,376 KB
testcase_22 AC 2 ms
5,376 KB
testcase_23 AC 1 ms
5,376 KB
testcase_24 AC 2 ms
5,376 KB
testcase_25 AC 33 ms
5,376 KB
testcase_26 AC 30 ms
5,376 KB
testcase_27 AC 27 ms
5,376 KB
testcase_28 AC 37 ms
5,376 KB
testcase_29 AC 38 ms
5,376 KB
testcase_30 AC 37 ms
5,376 KB
testcase_31 AC 581 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<algorithm>
using namespace std;

int gcd(int a, int b){
    return b == 0 ? a : gcd(b, a%b);
}

int main(){
    string s;
    cin >> s;

    bool b = true;
    for(int i = 1; i < s.length(); i++) b &= s[i] == s[i-1];
    if(b){
        cout << s << endl;
        return 0;
    }

    reverse(s.begin(), s.end());
    auto f = [](char x)->int{return x-'0';};

    int g = 0;
    for(int i = 0; i < s.length(); i++){
        for(int j = i+1; j < s.length(); j++){
            if(s[i] == s[j])    continue;
            if(g == 0)  g = 9*abs(f(s[i])-f(s[j]));
            else        g = gcd(g, 9*abs(f(s[i])-f(s[j])));
        }
    }

    for(int gg = g;; gg--){
        if(g%gg)    continue;
        int acc = 0;
        for(int i = 0; i < s.length(); i++){
            acc = acc*10 + f(s[i]);
            acc %= gg;
        }
        if(acc == 0){
            cout << gg << endl;
            return 0;
        }
    }

}
0