結果

問題 No.443 GCD of Permutation
ユーザー rpy3cpprpy3cpp
提出日時 2019-05-19 12:50:26
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 3 ms / 1,000 ms
コード長 2,210 bytes
コンパイル時間 1,465 ms
コンパイル使用メモリ 168,016 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-22 01:32:53
合計ジャッジ時間 2,329 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 1 ms
6,940 KB
testcase_05 AC 1 ms
6,940 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 1 ms
6,944 KB
testcase_09 AC 1 ms
6,944 KB
testcase_10 AC 1 ms
6,940 KB
testcase_11 AC 2 ms
6,940 KB
testcase_12 AC 1 ms
6,940 KB
testcase_13 AC 1 ms
6,944 KB
testcase_14 AC 2 ms
6,944 KB
testcase_15 AC 1 ms
6,944 KB
testcase_16 AC 2 ms
6,940 KB
testcase_17 AC 1 ms
6,944 KB
testcase_18 AC 2 ms
6,944 KB
testcase_19 AC 2 ms
6,940 KB
testcase_20 AC 2 ms
6,940 KB
testcase_21 AC 1 ms
6,944 KB
testcase_22 AC 2 ms
6,944 KB
testcase_23 AC 3 ms
6,940 KB
testcase_24 AC 2 ms
6,944 KB
testcase_25 AC 2 ms
6,940 KB
testcase_26 AC 2 ms
6,940 KB
testcase_27 AC 2 ms
6,944 KB
testcase_28 AC 1 ms
6,940 KB
testcase_29 AC 2 ms
6,940 KB
testcase_30 AC 2 ms
6,940 KB
testcase_31 AC 2 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;


bool is_made_of_one_digit(vector<int> &freq){
    int used = 0;
    for (auto f : freq) if (f > 0) ++used;
    return used == 1;
}

int check5(int num, const vector<int> & freq){
    return (num == freq[0] + freq[5])? 5 : 1;
}

int check7(int num, const vector<int> & freq){
    if(num == freq[0] + freq[7]){
        return 7;
    }else if(num == freq[1] + freq[8]){
        return (num % 6 == 0) ? 7 : 1;
    }else if(num == freq[2] + freq[9]){
        return (num % 6 == 0) ? 7 : 1;
    }else{
        return 1;
    }
}

int check2(int num, const vector<int> & freq){
    if (num == freq[0] + freq[8]){
        return 8;
    }else if (num == freq[0] + freq[4] + freq[8]){
        return 4;
    }else if (num == freq[0] + freq[2] + freq[4] + freq[6] + freq[8]){
        return 2;
    }else{
        return 1;
    }
}


int check3(int num, const vector<int> & freq){
    int sum = 0;
    for (int i = 0; i < 10; ++i) sum += i * freq[i];
    if (sum % 9 == 0){
        if (num == freq[0] + freq[9]){
            if (freq[9] % 9 == 0){
                return 81;
            }else if (freq[9] % 3 == 0){
                return 27;
            }else{
                return 9;
            }
        }else if (num == freq[0] + freq[3] + freq[6] + freq[9]){
            return (sum % 27 == 0) ? 27 : 9;
        }else if (num == freq[1] + freq[4] + freq[7]){
            return (num % 27 == 0 and sum % 27 == 0) ? 27 : 9;
        }else if (num == freq[2] + freq[5] + freq[8]){
            return (num % 27 == 0 and sum % 27 == 0) ? 27 : 9;
        }else{
            return 9;
        }
    }else if (sum % 3 == 0){
        return 3;
    }else{
        return 1;
    }
}

int solve(vector<int> &freq){
    int ans = 1;
    int num = accumulate(freq.begin(), freq.end(), 0);
    ans *= check2(num, freq);
    ans *= check5(num, freq);
    ans *= check7(num, freq);
    ans *= check3(num, freq);
    return ans;
}

int main(){
    string N;
    cin >> N;
    vector<int> freq(10, 0);
    for (auto n : N){
        ++freq[n - '0'];
    }
    if (is_made_of_one_digit(freq)){
        cout << N << endl;
    }else{
        cout << solve(freq) << endl;
    }

    return 0;
}
0