QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#163986#4281. Subspaceucup-team004#TL 1ms3640kbC++201.7kb2023-09-04 17:44:462023-09-04 17:44:46

Judging History

你现在查看的是最新测评结果

  • [2023-09-04 17:44:46]
  • 评测
  • 测评结果:TL
  • 用时:1ms
  • 内存:3640kb
  • [2023-09-04 17:44:46]
  • 提交

answer

#include <bits/stdc++.h>

using i64 = long long;
using i128 = __int128;

i64 power(i64 a, i64 b, i64 p) {
    i64 res = 1;
    for (; b; b /= 2, a = i128(a) * a % p) {
        if (b % 2) {
            res = i128(res) * a % p;
        }
    }
    return res;
}

std::pair<int, int> qint(int q, int n, int p) {
    q %= p;
    for (int x = 2; x * x <= n; x++) {
        if (n % x == 0) {
            auto [v1, e1] = qint(q, x, p);
            auto [v2, e2] = qint(power(q, x, p), n / x, p);
            return {1LL * v1 * v2 % p, e1 + e2};
        }
    }
    if (q == 1) {
        if (n == p) {
            return {0, 1};
        }
        return {n, 0};
    }
    // std::cerr << q << " " << n << " " << p << "\n";
    i64 v = 1 - power(q, n, 1LL * p * p);
    if (v < 0) {
        v += 1LL * p * p;
    }
    assert(v != 0);
    int inv = power(1 - q + p, p - 2, p);
    if (v % p == 0) {
        return {(v / p) * inv % p, 1};
    } else {
        return {v % p * inv % p, 0};
    }
}

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    
    int q, n, k, p;
    std::cin >> q >> n >> k >> p;
    
    // int t = k / p;
    // k -= t * p;
    // n -= t * p;
    // k = std::min(k, n - k);
    
    int num = 1, den = 1;
    int e = 0;
    for (int i = 1; i <= k; i++) {
        auto [v1, e1] = qint(q, n - i + 1, p);
        num = 1LL * num * v1 % p;
        e += e1;
        auto [v2, e2] = qint(q, i, p);
        den = 1LL * den * v2 % p;
        e -= e2;
    }
    int ans;
    if (e) {
        ans = 0;
    } else {
        ans = 1LL * num * power(den, p - 2, p) % p;
    }
    std::cout << ans << "\n";
    
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3640kb

input:

2 3 2 100003

output:

7

result:

ok 1 number(s): "7"

Test #2:

score: -100
Time Limit Exceeded

input:

408456817 56768983 38223849 50147

output:


result: