QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#572054 | #9309. Graph | DQ9911 | AC ✓ | 120ms | 12088kb | C++20 | 6.2kb | 2024-09-18 11:35:19 | 2024-09-18 11:35:19 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
typedef double db;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> point;
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
mt19937 gen(chrono::steady_clock::now().time_since_epoch().count());
using u32 = uint32_t;
using i64 = int64_t;
using u64 = uint64_t;
using u128 = __uint128_t;
template <typename T>
T inverse(T a, T m) {
T u = 0, v = 1;
while (a != 0) {
T t = m / a;
m -= t * a; swap(a, m);
u -= t * v; swap(u, v);
}
assert(m == 1);
return u;
}
template<int m, enable_if_t<(m >= 1)>* = nullptr >
struct Mint {
u32 v;
static constexpr int mod() { return m; }
static constexpr u32 umod() { return m; }
template<typename T, enable_if_t<is_integral_v<T> >* = nullptr>
static Mint raw(T _v) {
Mint x;
x.v = _v;
return x;
}
Mint() : v(0) {}
Mint(const Mint &rhs) : v(rhs.v) {}
template<typename T, enable_if_t<is_integral_v<T> >* = nullptr>
Mint(T _v) {
int x = (int) (_v % mod());
if (x < 0) x += mod();
v = (u32) x;
}
u32 val() const { return v; }
Mint inv() const { return Mint(inverse((int) v, mod()));}
Mint &operator ++() { if (++v == umod()) v = 0; return *this;}
Mint &operator --() { if (v-- == 0) v = umod() - 1; return *this; }
Mint operator ++(int) { Mint x = *this; ++(*this); return x; }
Mint operator --(int) { Mint x = *this; --(*this); return x; }
Mint &operator += (const Mint &rhs) { if ((v += rhs.v) >= umod()) v -= umod(); return *this; }
Mint &operator -= (const Mint &rhs) { if ((v += umod() - rhs.v) >= umod()) v -= umod(); return *this; }
Mint &operator *= (const Mint &rhs) { v = (u64) v * rhs.v % umod(); return *this; }
Mint &operator /= (const Mint &rhs) { return (*this) *= rhs.inv(); }
Mint operator+() const { return *this; }
Mint operator-() const { return Mint() - *this; }
Mint pow(long long n) const {
assert(n >= 0);
Mint x = *this, r = 1;
while (n) {
if (n & 1) r *= x;
x *= x;
n >>= 1;
}
return r;
}
friend Mint operator + (const Mint &lhs, const Mint &rhs) { return Mint(lhs) += rhs; }
friend Mint operator - (const Mint &lhs, const Mint &rhs) { return Mint(lhs) -= rhs; }
friend Mint operator * (const Mint &lhs, const Mint &rhs) { return Mint(lhs) *= rhs; }
friend Mint operator / (const Mint &lhs, const Mint &rhs) { return Mint(lhs) /= rhs; }
friend bool operator == (const Mint& lhs, const Mint& rhs) { return lhs._v == rhs._v; }
friend bool operator != (const Mint& lhs, const Mint& rhs) { return lhs._v != rhs._v; }
template<typename U>
friend U &operator << (U &stream, const Mint &rhs) {
return stream << rhs.v;
}
};
constexpr int mod = 998244353;
using mint = Mint<mod>;
vector<bool> np;
vector<u32> primes;
vector<int> mu;
void sieve(int N) {
np.resize(N + 1);
mu.resize(N + 1);
mu[1] = 1;
for (int i = 2; i <= N; i++) {
if (!np[i]) {
primes.push_back(i);
mu[i] = -1;
}
for (int p : primes) {
if (i * p > N)
break;
mu[i * p] = -mu[i];
np[i * p] = true;
if (i % p == 0) {
mu[i * p] = 0;
break;
}
}
}
}
namespace min25 {
u64 n;
uint32_t sqrtn;
int pn;
vector<u64> v;
void init(u64 _n) {
n = _n;
sqrtn = sqrt(_n);
while ((u64)(sqrtn + 1) * (sqrtn + 1) <= _n) {
sqrtn++;
}
sieve(sqrtn);
pn = primes.size();
v.reserve(sqrtn * 2 + 10);
for (u64 l = 1, r; l <= n; l = r + 1) {
r = n / (n / l);
v.push_back(n / l);
}
}
int ind(u64 x) {
if (x <= sqrtn) [[likely]] {
return v.size() - x;
}
return n / x - 1;
}
u64 fastdiv(u64 x, u64 _m) {
// _m = ceil(2^64 / m);
// return x / m;
return (u64)((u128) x * _m >> 64);
}
template<typename T>
struct ExEratosthensSieve {
// fp[i] = \sum_{2 <= k <= v[i], k is prime} f(k)
// sf(x) = \sum_{2 <= i <= x} f(i)
// f(x * y) = f(x) * f(y)
template<typename F, typename G>
pair<vector<T>, vector<T>> calcfp(F f, G sf) {
vector<T> pf(pn + 1);
for (int i = 0; i < pn; i++) {
pf[i + 1] = pf[i] + f(primes[i]);
}
vector<T> fp(v.size());
for (int i = 0; i < (int) v.size(); i++) {
fp[i] = sf(v[i]);
}
for (int j = 0; j < pn; j++) {
auto pj = primes[j];
auto _pj = ~0ull / pj + 1;
u64 z = (u64)pj * pj;
T vv = f(pj);
for (int i = 0; z <= v[i]; i++) {
int k = ind(fastdiv(v[i], _pj));
fp[i] -= vv * (fp[k] - pf[j]);
}
}
return make_pair(move(fp), move(pf));
}
// g[i] = \sum_{2 <= k <= v[i]} f(k)
// fpe(p, e) = f(p^e), p is prime
template<typename F>
vector<T> calcf(vector<T> fp, vector<T> pf, F fpe) {
auto &g = fp;
auto &pg = pf;
for (int j = pn - 1; j >= 0; j--) {
auto pj = primes[j];
auto _pj = ~0ull / pj + 1;
u64 z = (u64)pj * pj;
for (int i = 0; z <= v[i]; i++) {
u64 pe = pj, curv = fastdiv(v[i], _pj);
for (int e = 1; curv >= pj; e++, pe *= pj) {
g[i] += fpe(pj, e) * (g[ind(curv)] - pg[j + 1]) + fpe(pj, e + 1);
curv = fastdiv(curv, _pj);
}
}
}
return g;
}
template<typename F>
T calc(vector<T> fp, vector<T> pf, F fpe) {
auto &g = fp;
auto &pg = pf;
auto dfs = [&](auto && self, i64 n, int k)->T {
T ans = g[ind(n)] - pg[k];
for (int j = k; j < pn; j++) {
auto pj = primes[j];
auto _pj = ~0ull / pj + 1;
u64 z = (u64) pj * pj;
if (z > n) break;
u64 pe = pj, curn = fastdiv(n, _pj);
for (int e = 1; curn >= pj; e++, pe *= pj) {
ans += fpe(pj, e) * self(self, curn, j + 1) + fpe(pj, e + 1);
curn = fastdiv(curn, _pj);
}
}
return ans;
};
return dfs(dfs, n, 0);
}
};
}
namespace min25 {
void solve() {
ll n;
cin >> n;
init(n);
ExEratosthensSieve<u32> solver;
auto [fp, pf] = solver.calcfp([](u64 x) { return 1;}, [](u64 x) { return u32(x - 1);});
auto f = [&](u64 x) {
if (x <= 2) return mint(1);
if (x == 3) return mint(3);
auto num = fp[ind(x)] - fp[ind(x / 2)];
return (x - num - 1) * mint(x).pow(num);
};
mint ans = 1;
u64 last = 0;
for (auto &e : v) {
u64 cur = n / e;
ans *= f(e).pow(cur - last);
last = cur;
}
cout << ans << '\n';
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
min25::solve();
return 0;
}
这程序好像有点Bug,我给组数据试试?
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3804kb
input:
4
output:
8
result:
ok answer is '8'
Test #2:
score: 0
Accepted
time: 0ms
memory: 3640kb
input:
2
output:
1
result:
ok answer is '1'
Test #3:
score: 0
Accepted
time: 0ms
memory: 3728kb
input:
123
output:
671840470
result:
ok answer is '671840470'
Test #4:
score: 0
Accepted
time: 0ms
memory: 3644kb
input:
233
output:
353738465
result:
ok answer is '353738465'
Test #5:
score: 0
Accepted
time: 0ms
memory: 3704kb
input:
5981
output:
970246821
result:
ok answer is '970246821'
Test #6:
score: 0
Accepted
time: 0ms
memory: 3588kb
input:
86422
output:
897815688
result:
ok answer is '897815688'
Test #7:
score: 0
Accepted
time: 0ms
memory: 3588kb
input:
145444
output:
189843901
result:
ok answer is '189843901'
Test #8:
score: 0
Accepted
time: 0ms
memory: 3640kb
input:
901000
output:
819449452
result:
ok answer is '819449452'
Test #9:
score: 0
Accepted
time: 0ms
memory: 3892kb
input:
1000000
output:
113573943
result:
ok answer is '113573943'
Test #10:
score: 0
Accepted
time: 1ms
memory: 3812kb
input:
23333333
output:
949849384
result:
ok answer is '949849384'
Test #11:
score: 0
Accepted
time: 2ms
memory: 3776kb
input:
102850434
output:
604886751
result:
ok answer is '604886751'
Test #12:
score: 0
Accepted
time: 8ms
memory: 4188kb
input:
998244353
output:
0
result:
ok answer is '0'
Test #13:
score: 0
Accepted
time: 8ms
memory: 4232kb
input:
1000000007
output:
318420284
result:
ok answer is '318420284'
Test #14:
score: 0
Accepted
time: 12ms
memory: 4612kb
input:
2147483547
output:
688759898
result:
ok answer is '688759898'
Test #15:
score: 0
Accepted
time: 20ms
memory: 5296kb
input:
5120103302
output:
116870489
result:
ok answer is '116870489'
Test #16:
score: 0
Accepted
time: 45ms
memory: 7248kb
input:
19834593299
output:
523663743
result:
ok answer is '523663743'
Test #17:
score: 0
Accepted
time: 84ms
memory: 9884kb
input:
52500109238
output:
195086665
result:
ok answer is '195086665'
Test #18:
score: 0
Accepted
time: 113ms
memory: 11492kb
input:
84848352911
output:
107959260
result:
ok answer is '107959260'
Test #19:
score: 0
Accepted
time: 120ms
memory: 12020kb
input:
99824435322
output:
0
result:
ok answer is '0'
Test #20:
score: 0
Accepted
time: 120ms
memory: 12016kb
input:
99999999354
output:
316301711
result:
ok answer is '316301711'
Test #21:
score: 0
Accepted
time: 117ms
memory: 12088kb
input:
100000000000
output:
396843576
result:
ok answer is '396843576'
Extra Test:
score: 0
Extra Test Passed