結果

問題 No.1164 GCD Products hard
ユーザー bokusunny
提出日時 2023-04-15 10:05:49
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
TLE  
実行時間 -
コード長 3,001 bytes
コンパイル時間 2,235 ms
コンパイル使用メモリ 202,564 KB
最終ジャッジ日時 2025-02-12 08:44:01
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 25 TLE * 2
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

#include <atcoder/modint>
using namespace atcoder;

struct Eratosthenes {
   private:
    int _n;
    vector<bool> IsPrime;
    vector<int> MinFactor;
    vector<int> Mobius;

   public:
    Eratosthenes(int n = 1 << 20) : _n(n), IsPrime(n + 1, true), MinFactor(n + 1, -1), Mobius(n + 1, 1) {
        IsPrime[0] = IsPrime[1] = false;
        for (int p = 2; p <= n; p++) {
            if (!IsPrime[p]) continue;
            MinFactor[p] = p;
            Mobius[p] = -1;

            for (int q = 2 * p; q <= n; q += p) {
                IsPrime[q] = false;
                if (MinFactor[q] == -1) MinFactor[q] = p;
                if (q / p % p == 0) {
                    Mobius[q] = 0;
                } else {
                    Mobius[q] = -Mobius[q];
                }
            }
        }
    }

    bool is_prime(int x) { return IsPrime[x]; }

    vector<int> primes() {
        vector<int> res;
        for (int i = 2; i <= _n; i++) {
            if (IsPrime[i]) res.push_back(i);
        }
        return res;
    }

    vector<pair<int, int>> factorize(int n) {
        assert(1 <= n && n <= _n);

        vector<pair<int, int>> res;
        if (n == 1) return res;

        while (n > 1) {
            auto p = MinFactor[n];
            int ex = 0;
            while (MinFactor[n] == p) {
                ex++;
                n /= p;
            }
            res.emplace_back(p, ex);
        }

        return res;
    }

    vector<int> divisors(int n) {
        assert(1 <= n && n <= _n);

        vector<int> res = {1};
        if (n == 1) return res;

        auto pf = factorize(n);
        for (auto [p, ex] : pf) {
            int sz = (int)res.size();
            for (int i = 0; i < sz; i++) {
                int v = 1;
                for (int j = 0; j < ex; j++) {
                    v *= p;
                    res.push_back(res[i] * v);
                }
            }
        }

        return res;
    }

    vector<int> get_mobius() { return Mobius; }
};

long long modpow(long long a, long long n, int mod = 1000000007) {
    assert(mod != 0);
    if (mod == 1) return 0LL;
    a %= mod;
    long long res = 1;
    while (n > 0) {
        if (n & 1) res = res * a % mod;
        a = a * a % mod;
        n >>= 1;
    }
    return res;
}

int main() {
    int A, B, N;
    cin >> A >> B >> N;

    const int MOD = 1e9 + 7;

    Eratosthenes E(B + 1);
    vector<long long> F(B + 1);
    for (int g = 1; g <= B; g++) {
        F[g] = modpow(B / g - (A - 1) / g, N, MOD - 1);
    }
    for (int p = 2; p <= B; p++) {
        if (!E.is_prime(p)) continue;

        for (int k = 1; k * p <= B; k++) {
            F[k] -= F[k * p];
            F[k] += MOD - 1;
            if (F[k] >= MOD - 1) {
                F[k] -= MOD - 1;
            }
        }
    }

    long long ans = 1;
    for (int g = 1; g <= B; g++) {
        ans *= modpow(g, F[g], MOD);
        ans %= MOD;
    }

    cout << ans << endl;

    return 0;
}
0