結果

問題 No.443 GCD of Permutation
ユーザー 🍡yurahuna🍡yurahuna
提出日時 2016-07-18 22:37:47
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 3 ms / 1,000 ms
コード長 2,835 bytes
コンパイル時間 1,773 ms
コンパイル使用メモリ 173,608 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-04 02:35:34
合計ジャッジ時間 3,285 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 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 2 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 2 ms
4,376 KB
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 2 ms
4,380 KB
testcase_26 AC 2 ms
4,380 KB
testcase_27 AC 2 ms
4,376 KB
testcase_28 AC 3 ms
4,380 KB
testcase_29 AC 2 ms
4,384 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;
// #define int long long   // <-----!!!!!!!!!!!!!!!!!!!

#define rep(i,n) for (int i=0;i<(n);i++)
#define rep2(i,a,b) for (int i=(a);i<(b);i++)
#define rrep(i,n) for (int i=(n)-1;i>=0;i--)
#define rrep2(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define all(a) (a).begin(),(a).end()

typedef long long ll;
typedef pair<int, int> Pii;
typedef tuple<int, int, int> TUPLE;
typedef vector<int> V;
typedef vector<V> VV;
typedef vector<VV> VVV;
typedef vector<vector<int>> Graph;
const int inf = 1e9;
const int mod = 1e9 + 7;

int gcd(int a, int b) {
    if (b == 0) return a;
    return gcd(b, a % b);
}

// 約数を列挙(12 -> {1, 2, 3, 6, 12}, 8 -> {1, 2, 4, 8})
vector<int> divisor(int n) {
    vector<int> res;
    for (int i = 1; i * i <= n; i++) {
        if (n % i == 0) {
            res.emplace_back(i);
            if (i != n / i) res.emplace_back(n / i);
        }
    }
    return res;
}

bool divisible(string s, int x) {
    int r = 0;
    rep(i, s.size()) {
        r = (r * 10 + s[i] - '0') % x;
    }
    return r == 0;
}

signed main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);

    string s;
    cin >> s;

    // cnt[i] = 数字 i が 文字列中に存在するか
    vector<bool> exist(10);
    rep(i, s.size()) exist[s[i] - '0'] = true;

    // S の要素で、下2桁の異なる数字を入れ替えた要素 A, A + b を考える (b:下2桁の差)
    // (ex. A = xxx48, B = xxx84 -> b = 36)
    // G は A, A + b の共通の約数より、 b の約数でなければならない
    // よって、 G は ありうるすべての下2桁の差の GCD (g) の約数でなければならない
    int g = -1;
    rep(i, 10) {
        rep2(j, i + 1, 10) {
            if (exist[i] && exist[j]) {
                int diff = 9 * (j - i);     // (10 * j + i) - (10 * i + j)
                if (g == -1)    g = diff;
                else            g = gcd(g, diff);
            }
        }
    }

    // もし下2桁の入れ替えが起こりえなかった場合
    // S の要素は N 自身のみ
    if (g == -1) {
        cout << s << endl;
        return 0;
    }

    // あとは何が言えれば十分か?
    // 実は、下2桁の入れ替えを考えたことにより、任意の異なる桁の入れ替えが考慮できている
    // (∵ x 桁目の数字 a と y 桁目の数字 b を入れ替えたときの差は、 (x > y として、)
    //      (10^x - 10^y) * (b - a) = 9 * (b - a) の倍数)
    // あとは G が N を割り切れさえすれば OK
    // よって、 g の約数のうち、 N を割り切る最大の値が答え
    auto d = divisor(g);
    sort(all(d));
    reverse(all(d));
    for (auto x : d) {
        if (divisible(s, x)) {
            cout << x << endl;
            return 0;
        }
    }
}
0