結果

問題 No.2051 Divide
ユーザー ManjushriMitra
提出日時 2025-03-16 16:21:41
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 4,024 bytes
コンパイル時間 4,104 ms
コンパイル使用メモリ 287,580 KB
実行使用メモリ 7,324 KB
最終ジャッジ日時 2025-03-16 16:21:46
合計ジャッジ時間 5,402 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 23
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
#define rep(i,m,n) for(int i=m; i<int(n); ++i)
#define repll(i,m,n) for(ll i=m; i<ll(n); ++i)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define leng(a) int(a.size())
#define fi first
#define se second

template<typename T> bool chmin(T& a, T b){ if(a > b){a = b; return true;} return false; }
template<typename T> bool chmax(T& a, T b){ if(a < b){a = b; return true;} return false; }

template<typename T> T gcd(T a, T b){ return a % b ? gcd(b, a % b) : b; }
template<typename T> T lcm(T a, T b){ return a / gcd(a, b) * b; }

// Pollard’s Rho の平均計算量: O(n^(1/4))
// Miller-Rabin 素数判定 の計算量: O(k*(log(⁡n))^3) (kは試行回数)

// 64bitの安全な (a * b) % m 計算(乗算分割法)
ll mod_mul(ll a, ll b, ll m) {
    ll res = 0;
    while (b) {
        if (b & 1) res = (res + a) % m;
        a = (a + a) % m;
        b >>= 1;
    }
    return res;
}

// 繰り返し二乗法による (a^b) % m 計算
ll mod_pow(ll a, ll b, ll m) {
    ll res = 1;
    while (b) {
        if (b & 1) res = mod_mul(res, a, m);
        a = mod_mul(a, a, m);
        b >>= 1;
    }
    return res;
}

// Miller-Rabin素数判定(確率的)
bool is_prime(ll n) {
    if (n < 2) return false;
    if (n % 2 == 0) return (n == 2);
    ll d = n - 1;
    int s = 0;
    while (d % 2 == 0) d /= 2, ++s;

    vector<ll> test_bases = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37}; // 試行基底
    for (ll a : test_bases) {
        if (a >= n) break;
        ll x = mod_pow(a, d, n);
        if (x == 1 || x == n - 1) continue;
        bool composite = true;
        for (int r = 0; r < s - 1; ++r) {
            x = mod_mul(x, x, n);
            if (x == n - 1) {
                composite = false;
                break;
            }
        }
    if (composite) return false;
    }
    return true;
}

// Pollard's Rho 法による因数探索
ll pollard_rho(ll n) {
    if (n % 2 == 0) return 2;
    static mt19937_64 rng(random_device{}()); // 乱数生成
    uniform_int_distribution<ll> dist(1, n - 1);

    ll x = dist(rng), y = x, c = dist(rng);
    auto f = [&](ll v) { return (mod_mul(v, v, n) + c) % n; };

    while (true) {
        x = f(x);
        y = f(f(y));
        ll d = gcd(abs(x - y), n);
        if (d > 1 && d < n) return d;
        if (d == n) return pollard_rho(n);
    }
}

// 素因数分解
void factorize(ll n, vector<ll>& factors) {
    if (n == 1) return;
    if (is_prime(n)) {
        factors.push_back(n);
        return;
    }

    ll divisor = pollard_rho(n);
    factorize(divisor, factors);
    factorize(n / divisor, factors);
}

// 素因数列挙
vector<ll> prime_factorization(ll n) {
    vector<ll> factors;
    while (n % 2 == 0) { // 偶数処理
        factors.push_back(2);
        n /= 2;
    }
    factorize(n, factors);
    sort(factors.begin(), factors.end());
    return factors;
}

// 約数列挙
vector<ll> divisor_list(ll n) {
    vector<ll> res({1LL});
    auto pf = prime_factorization(n);

    vector<pair<ll, ll>> pf2;
    ll ex = 1LL;
    for(int i = 1; i < int(pf.size()); ++i){
        if(pf[i] != pf[i-1]){
            pf2.push_back({ll(pf[i-1]), ex});
            ex = 1LL;
        }else{
            ++ex;
        }
    }
    pf2.push_back({ll(pf.back()), ex});

    for(auto p : pf2){
        int sz = res.size();
        for(int i = 0; i < sz; ++i){
            ll v = 1LL;
            for(int j = 0; j < p.second; ++j){
                v *= p.first;
                res.push_back(res[i] * v);
            }
        }
    }
    // sort(res.begin(), res.end());
    return res;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    ll A, B;
    cin >> A >> B;

    int ans = 0;
    if(A == 1LL){
        if(A % B == 0LL) ++ans;
    }else{
        auto d_list = divisor_list(A);
        for(ll d : d_list) if(d % B == 0LL) ++ans;
    }
    cout << ans << endl;

    return 0;
}
0