結果

問題 No.443 GCD of Permutation
ユーザー rpy3cpp
提出日時 2019-05-19 12:50:26
言語 C++14
(gcc 8.3.0)
結果
AC  
実行時間 4 ms
コード長 2,210 Byte
コンパイル時間 1,397 ms
使用メモリ 6,876 KB
最終ジャッジ日時 2019-09-21 09:03:20

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
a_sample1.in AC 3 ms
6,876 KB
a_sample2.in AC 3 ms
6,872 KB
a_sample3.in AC 4 ms
6,872 KB
a_sample4.in AC 3 ms
6,872 KB
b_small1.in AC 3 ms
6,872 KB
b_small2.in AC 3 ms
6,876 KB
b_small3.in AC 3 ms
6,876 KB
b_small4.in AC 3 ms
6,872 KB
b_small5.in AC 4 ms
6,876 KB
b_small6.in AC 3 ms
6,872 KB
b_small7.in AC 3 ms
6,876 KB
b_small8.in AC 3 ms
6,876 KB
b_small9.in AC 4 ms
6,872 KB
c_large1.in AC 3 ms
6,872 KB
c_large2.in AC 4 ms
6,876 KB
c_large4.in AC 3 ms
6,876 KB
c_large5.in AC 3 ms
6,876 KB
c_large6.in AC 4 ms
6,876 KB
c_large7.in AC 3 ms
6,872 KB
c_large8.in AC 3 ms
6,876 KB
c_large9.in AC 3 ms
6,876 KB
challenge01.txt AC 4 ms
6,876 KB
challenge02.txt AC 4 ms
6,876 KB
d_challenge1.in AC 3 ms
6,872 KB
d_challenge2.in AC 4 ms
6,876 KB
d_challenge3.in AC 3 ms
6,872 KB
d_challenge4.in AC 3 ms
6,876 KB
d_challenge5.in AC 4 ms
6,876 KB
d_challenge6.in AC 4 ms
6,872 KB
d_challenge7.in AC 4 ms
6,876 KB
system_test1.txt AC 4 ms
6,876 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