QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#641908 | #2307. 树 | Elegia | 100 ✓ | 47ms | 4176kb | C++14 | 3.2kb | 2024-10-15 03:15:33 | 2024-10-15 03:15:33 |
Judging History
answer
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cctype>
#include <algorithm>
#include <random>
#include <bitset>
#include <queue>
#include <functional>
#include <set>
#include <map>
#include <vector>
#include <chrono>
#include <iostream>
#include <limits>
#include <numeric>
#define LOG(FMT...) fprintf(stderr, FMT)
#define fir first
#define sec second
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template <class T>
istream& operator>>(istream& is, vector<T>& v) {
for (T& x : v) is >> x;
return is;
}
ostream& operator<<(ostream& os, const pair<char, int>& unit) {
return os << unit.first << "^" << unit.second;
}
template <class T>
ostream& operator<<(ostream& os, const vector<T>& v) {
if (!v.empty()) {
os << v.front();
for (int i = 1; i < v.size(); ++i) os << ' ' << v[i];
}
return os;
}
int p;
int mpow(int x, int k) {
if (k == 0)
return 1;
int ret = mpow(x * (ll)x % p, k >> 1);
if (k & 1)
ret = ret * (ll)x % p;
return ret;
}
int main() {
#ifdef ELEGIA
freopen("test.in", "r", stdin);
int nol_cl = clock();
#endif
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, k;
while (cin >> n >> k >> p) {
k %= p - 1;
vector<int> pw(n + 1), ipw(n + 1), inv(n + 1);
for (int i = 1; i <= n; ++i) {
pw[i] = mpow(i, k);
ipw[i] = mpow(i, p - 1 - k);
inv[i] = mpow(i, p - 2);
}
vector<vector<int>> F(n + 1), lnF(n + 1);
for (int t = 1; t <= n; ++t) {
F[t].resize(n / t + 1);
lnF[t].resize(n / t + 1);
F[t][0] = 1;
if (t == 1) for (int i = 1; i <= n / t; ++i) F[t][i] = F[t][i - 1] * (ll)ipw[i] % p;
else for (int i = 1; i <= n / t; ++i) F[t][i] = F[t - 1][i] * (ll)F[1][i] % p;
for (int i = 1; i <= n / t; ++i) {
lnF[t][i] = F[t][i] * (ll) i % p;
for (int j = 1; j <= i; ++j) lnF[t][i] = (lnF[t][i] + (p - F[t][j]) * (ll) lnF[t][i - j]) % p;
}
for (int i = 1; i <= n / t; ++i) lnF[t][i] = lnF[t][i] * (ll) inv[i] % p;
}
vector<vector<int>> lnG(n + 1);
for (int i = 1; i <= n; ++i) lnG[i].resize(n / i + 1);
vector<vector<int>> G(n);
for (int i = 0; i < n; ++i) G[i].resize(n / (i + 1) + 1);
for (int i = 1; i <= n; ++i) G[0][i] = 1;
for (int i = 1; i < n; ++i) {
vector<int> ksum(n / i + 1);
int q = 1;
for (int j = 1; j <= n / i; ++j) {
q = q * (ll)ipw[i] % p;
ksum[j] = G[i - 1][j] * (ll)q % p;
}
for (int j = 1; j <= n / i; ++j)
for (int t = 1; t <= n / i / j; ++t)
lnG[i * j][t] = (lnG[i * j][t] + ksum[j * t] * (ll)lnF[t][j]) % p;
for (int j = 1; j <= i; ++j)
for (int t = 1; t <= n / (i + 1); ++t)
G[i][t] = (G[i][t] + j * (ll)lnG[j][t] % p * G[i - j][t]) % p;
for (int j = 1; j <= n / (i + 1); ++j) G[i][j] = G[i][j] * (ll)inv[i] % p;
}
cout << G[n - 1][1] << endl;
}
#ifdef ELEGIA
LOG("Time: %dms\n", int((clock() - nol_cl) / (double)CLOCKS_PER_SEC * 1000));
#endif
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Pretests
Final Tests
Test #1:
score: 10
Accepted
time: 0ms
memory: 3620kb
input:
5 2 736853057
output:
621719767
result:
ok single line: '621719767'
Test #2:
score: 10
Accepted
time: 0ms
memory: 3608kb
input:
10 2 302939617
output:
145443067
result:
ok single line: '145443067'
Test #3:
score: 10
Accepted
time: 0ms
memory: 3528kb
input:
20 2 662072321
output:
267646252
result:
ok single line: '267646252'
Test #4:
score: 10
Accepted
time: 0ms
memory: 3628kb
input:
50 2 806053187
output:
155980816
result:
ok single line: '155980816'
Test #5:
score: 10
Accepted
time: 0ms
memory: 3820kb
input:
49 2 953748661
output:
691695523
result:
ok single line: '691695523'
Test #6:
score: 10
Accepted
time: 0ms
memory: 3836kb
input:
50 286072115 443534951
output:
284050905
result:
ok single line: '284050905'
Test #7:
score: 10
Accepted
time: 1ms
memory: 3604kb
input:
200 903188218 276454751
output:
40047627
result:
ok single line: '40047627'
Test #8:
score: 10
Accepted
time: 4ms
memory: 3676kb
input:
500 668948355 993729403
output:
636747279
result:
ok single line: '636747279'
Test #9:
score: 10
Accepted
time: 10ms
memory: 4064kb
input:
1000 980382612 268177673
output:
158308282
result:
ok single line: '158308282'
Test #10:
score: 10
Accepted
time: 47ms
memory: 4176kb
input:
2000 178570326 857509859
output:
372489332
result:
ok single line: '372489332'
Extra Test:
score: 0
Extra Test Passed