結果

問題 No.443 GCD of Permutation
ユーザー SHIJOUSHIJOU
提出日時 2020-08-22 16:50:15
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 3 ms / 1,000 ms
コード長 4,332 bytes
コンパイル時間 2,415 ms
コンパイル使用メモリ 209,744 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-23 10:56:56
合計ジャッジ時間 3,431 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

//#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define rep(i, n) for(int i=0; i<n; ++i)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
using namespace std;
using ll = int64_t;
using ld = long double;
using P = pair<int, int>;
using vs = vector<string>;
using vi = vector<int>;
using vvi = vector<vi>;
template<class T> using PQ = priority_queue<T>;
template<class T> using PQG = priority_queue<T, vector<T>, greater<T> >;
const int INF = 0xccccccc;
const ll LINF = 922337203685477580LL;
template<typename T1, typename T2>
inline bool chmax(T1 &a, T2 b) {return a < b && (a = b, true);}
template<typename T1, typename T2>
inline bool chmin(T1 &a, T2 b) {return a > b && (a = b, true);}
template<typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) { return is >> p.first >> p.second;}
template<typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) { return os << p.first << ' ' << p.second;}

int64_t gcd(int64_t a, int64_t b) {return b?gcd(b, a%b):a;}
int64_t lcm(int64_t a, int64_t b) {return a/gcd(a,b)*b;}
int64_t extgcd(int64_t a, int64_t b, int64_t &x, int64_t &y) { // ax+by=gcd(a, b)の解を与える
  int64_t d = a;
  if(b) {
    d = extgcd(b, a%b, y, x);
    y -= (a/b)*x;
  } else {
    x = 1; y = 0;
  }
  return d;
}
struct Sieve {
  int n;
  vector<int> f, primes;
  Sieve(int n=1):n(n), f(n+1) {
    f[0] = f[1] = -1;
    for(int64_t i=2; i<=n; ++i) {
      if(f[i]) continue;
      primes.push_back(i);
      f[i] = i;
      for(int64_t j = i*i; j <= n; j += i) {
        if(!f[j]) f[j] = i;
      }
    }
  }
  //素数判定
  bool isPrime(int x) {return f[x] == x;}
  //素数判定(1つ用)(宣言はsqrt(x)のサイズで)
  bool isPrime1(int64_t x) {
    bool m = true;
    for(int z:primes) {
      if(!(x%z)) {
        m = false;
        break;
      }
    }
    return m;
  }
  //素数列挙
  vector<int > factorList(int x) {
    assert(x <= n);
    vector<int> res;
    while(x != 1) {
      res.emplace_back(f[x]);
      x /= f[x];
    }
    return res;
  }
  //素因数分解 pair(素数, 素因数の数)
  vector<pair<int, int> > factor(int x) {
    vector<int> fl = factorList(x);
    if(fl.size() == 0) return {};
    vector<pair<int, int> > res(1, pair<int, int>(fl[0], 0));
    for(int p : fl) {
      if(res.back().first == p) {
        ++res.back().second;
      } else {
        res.emplace_back(p, 1);
      }
    }
    return res;
  }
  //素数列挙(1つ用)(宣言はsqrt(x)のサイズで)
  vector<int64_t> factorList1(int64_t x) {
    vector<int64_t> vec;
    for(int z:primes) {
      while(!(x%z)) {
        vec.emplace_back(z);
        x /= z;
      }
    }
    if(x != 1) vec.emplace_back(x);
    return vec;
  }
  //素数列挙(1つ用)(宣言はsqrt(x)のサイズで) pair(素数, 素因数の数)
  vector<pair<int64_t, int> > factor1(int64_t x) {
    vector<int64_t> vec = factorList1(x);
    vector<pair<int64_t, int> > vecc;
    for(vector<int64_t>::iterator itr = vec.begin(); itr != vec.end(); ++itr) {
      if(itr != vec.begin() && *itr == *(itr-1)) {
        (vecc.end()-1)->second++;
      } else {
        vecc.emplace_back(pair<int64_t, int>(*itr, 1));
      }
    }
    return vecc;
  }
  //約数列挙
  vector<int64_t> div1(int64_t x) {
    vector<int64_t> res(1, 1);
    vector<pair<int64_t, int> > fac = factor1(x);
    for(int i = 0; i < fac.size(); ++i) {
      int si = res.size();
      for(int j = 0; j < fac[i].second; ++j) {
        for(int k = 0; k < si; ++k) {
          res.emplace_back(res[k+j*si]*fac[i].first);
        }
      }
    }
    sort(res.begin(), res.end());
    return res;
  }
}pp(100);

//head

string s;
int n;
int cnt[10];
int diff[10][10];
int ans = 1;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  rep(i, 10) rep(j, 10) {
    diff[i][j] = abs(i*9-j*9);
  }

  cin >> s;
  n = s.size();
  rep(i, n) {
    cnt[s[i]-'0']++;
  }
  int uu = 0;
  rep(i, 10) if(cnt[i]) uu++;
  if(uu == 1) {
    cout << s << endl;
    return 0;
  }

  int gg = 0;
  rep(i, 10) rep(j, 10) {
    if(cnt[i] and cnt[j]) gg = gcd(gg, diff[i][j]);
  }
  auto vec = pp.div1(gg);
  rep(i, vec.size()) {
    int now = 0;
    rep(j, n) {
      now *= 10;
      now += s[j]-'0';
      now %= vec[i];
    }
    if(!now) ans = vec[i];
  }
  
  cout << ans << endl;
}
0