QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#163991 | #4281. Subspace | ucup-team004# | WA | 117ms | 3468kb | C++20 | 1.8kb | 2023-09-04 17:46:17 | 2023-09-04 17:46:17 |
Judging History
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) {
int e = 0;
while (n % p == 0) {
n /= p;
e++;
}
return {n % p, e};
}
// 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 = std::max(0, k / p - 1);
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: 3468kb
input:
2 3 2 100003
output:
7
result:
ok 1 number(s): "7"
Test #2:
score: 0
Accepted
time: 34ms
memory: 3420kb
input:
408456817 56768983 38223849 50147
output:
0
result:
ok 1 number(s): "0"
Test #3:
score: -100
Wrong Answer
time: 117ms
memory: 3388kb
input:
129129379 529593615 39885429 143329
output:
114749
result:
wrong answer 1st numbers differ - expected: '42442', found: '114749'