結果
| 問題 | No.443 GCD of Permutation | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2021-03-21 06:03:26 | 
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 9 ms / 1,000 ms | 
| コード長 | 1,188 bytes | 
| コンパイル時間 | 5,290 ms | 
| コンパイル使用メモリ | 204,380 KB | 
| 最終ジャッジ日時 | 2025-01-19 20:48:23 | 
| ジャッジサーバーID (参考情報) | judge5 / judge1 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 4 | 
| other | AC * 28 | 
ソースコード
#include <bits/stdc++.h>
using namespace std;
bool check(const string &s, int m) {
  int r = 0;
  for (char c: s) {
    r = 10 * r + (c - '0');
    r %= m;
  }
  return r == 0;
}
string solve(string s) {
  sort(s.begin(), s.end());
  if (s.front() == s.back())
    return s;
  vector<int> v;
  for (char c: s)
    v.push_back(c - '0');
  sort(v.begin(), v.end());
  v.erase(unique(v.begin(), v.end()), v.end());
  
  int x = (s.back() - s.front()) * 9;
  for (int y = x; y > 1; y--) {
    if (x % y) continue;
    if (!check(s, y)) continue;
    vector<int> tens(s.length());
    tens[0] = 1;
    for (int i = 1; i < s.length(); i++)
      tens[i] = tens[i-1] * 10 % y;
    bool ck = true;
    for (int i = 0; i < v.size(); i++) {
      for (int j = 0; j < i; j++) {
        for (int n = 0; n < min<int>(y, tens.size()); n++) {
          for (int m = 0; m < min<int>(y, tens.size()); m++) {
            ck &= (tens[n] - tens[m]) * (v[i] - v[j]) % y == 0;
          }
        }
      }
    }
    if (ck)
      return to_string(y);
  }
  return "1";
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  string s;
  cin >> s;
  cout << solve(s) << endl;
  
  return 0;
}
            
            
            
        