結果

問題 No.443 GCD of Permutation
ユーザー kk
提出日時 2021-03-21 06:03:26
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 1,000 ms
コード長 1,188 bytes
コンパイル時間 2,054 ms
コンパイル使用メモリ 208,536 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-14 02:10:13
合計ジャッジ時間 3,842 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#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;
}
0