結果
| 問題 | No.443 GCD of Permutation | 
| コンテスト | |
| ユーザー |  beet | 
| 提出日時 | 2018-11-08 12:26:05 | 
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 23 ms / 1,000 ms | 
| コード長 | 1,039 bytes | 
| コンパイル時間 | 2,333 ms | 
| コンパイル使用メモリ | 201,456 KB | 
| 最終ジャッジ日時 | 2025-01-06 15:51:41 | 
| ジャッジサーバーID (参考情報) | judge1 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 4 | 
| other | AC * 28 | 
ソースコード
#include<bits/stdc++.h>
using namespace std;
using Int = long long;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}
//INSERT ABOVE HERE
signed main(){
  string s;
  cin>>s;
  {
    string t(s);
    t.erase(unique(t.begin(),t.end()),t.end());
    if(t.size()==1){
      cout<<s<<endl;
      return 0;
    }
  }
  
  if(s.size()<10){
    Int ans=stoll(s);
    sort(s.begin(),s.end());
    do{
      Int res=0;
      for(char c:s) res=res*10+(c-'0');
      ans=__gcd(ans,res);      
    }while(next_permutation(s.begin(),s.end()));
    cout<<ans<<endl;
    return 0;
  }
  vector<Int> cnt(10,0);
  for(char c:s) cnt[c-'0']++;
  Int ans=-1;
  for(Int i=0;i<10;i++){
    for(Int j=0;j<10;j++){
      if(!cnt[i]||!cnt[j]) continue;
      Int k=abs(i-j)*9;
      if(ans<0) ans=k;
      ans=__gcd(ans,k);
    }
  }
  Int res=0;
  for(char c:s){
    res=res*10+(c-'0');
    res%=ans;
  }
  cout<<__gcd(res,ans)<<endl;
  
  return 0;
}
            
            
            
        