QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#163958#4281. Subspaceucup-team004#WA 30ms3700kbC++201.1kb2023-09-04 17:18:372023-09-04 17:18:37

Judging History

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

  • [2023-09-04 17:18:37]
  • 评测
  • 测评结果:WA
  • 用时:30ms
  • 内存:3700kb
  • [2023-09-04 17:18:37]
  • 提交

answer

#include <bits/stdc++.h>

using i64 = long long;

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

int qint(int q, int n, int p) {
    auto rec = [&](auto self, int n) -> std::pair<int, int> {
        if (n == 0) {
            return {0, 1};
        }
        auto [x, y] = self(self, n / 2);
        x = 1LL * x * (1 + y) % p;
        y = 1LL * y * y % p;
        if (n % 2) {
            x = (x + y) % p;
            y = 1LL * y * q % p;
        }
        return {x, y};
    };
    return rec(rec, n).first;
}

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 ans = 1;
    for (int i = 1; i <= k; i++) {
        ans = 1LL * ans * qint(q, n - i + 1, p) % p;
        ans = 1LL * ans * power(qint(q, i, p), p - 2, p) % p;
    }
    std::cout << ans << "\n";
    
    return 0;
}

詳細信息

Test #1:

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

input:

2 3 2 100003

output:

7

result:

ok 1 number(s): "7"

Test #2:

score: 0
Accepted
time: 8ms
memory: 3700kb

input:

408456817 56768983 38223849 50147

output:

0

result:

ok 1 number(s): "0"

Test #3:

score: -100
Wrong Answer
time: 30ms
memory: 3572kb

input:

129129379 529593615 39885429 143329

output:

0

result:

wrong answer 1st numbers differ - expected: '42442', found: '0'