結果

問題 No.443 GCD of Permutation
ユーザー ytft
提出日時 2021-03-27 08:14:47
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 3 ms / 1,000 ms
コード長 828 bytes
コンパイル時間 1,724 ms
コンパイル使用メモリ 174,424 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-11-29 08:06:29
合計ジャッジ時間 2,883 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 28
権限があれば一括ダウンロードができます

ソースコード

diff #

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

int natural_gcd(int a,int b){
    int m,M;
    if(a>b){
        M=a;
        m=b;
    }else{
        M=b;
        m=a;
    }
    int temp;
    while(m>0){
        temp=m;
        m=M%m;
        M=temp;
    }
    return M;
}


int main(){
    string S;
    cin>>S;
    vector<int> count(10);
    vector<int> f;
    for(int i=0;i<S.size();i++){
        if(!count[S[i]-'0'])f.push_back(S[i]-'0');
        count[S[i]-'0']++;
    }
    sort(f.begin(),f.end());
    if(f.size()==1){
        cout<<S<<endl;
        return 0;
    }
    int g=f[1]-f[0];
    for(int i=2;i<f.size();i++){
        g=natural_gcd(g,f[i]-f[i-1]);
    }
    g*=9;
    int temp=0;
    for(int i=0;i<S.size();i++){
        temp=(temp*10+(S[i]-'0'))%g;
    }
    if(temp==0)temp+=g;
    cout<<natural_gcd(g,temp);
}
0