QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#773097 | #9309. Graph | hos_lyric# | AC ✓ | 477ms | 9664kb | C++14 | 11.2kb | 2024-11-23 00:52:46 | 2024-11-23 00:52:46 |
Judging History
answer
#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using Int = long long;
template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }
#define COLOR(s) ("\x1b[" s "m")
////////////////////////////////////////////////////////////////////////////////
template <unsigned M_> struct ModInt {
static constexpr unsigned M = M_;
unsigned x;
constexpr ModInt() : x(0U) {}
constexpr ModInt(unsigned x_) : x(x_ % M) {}
constexpr ModInt(unsigned long long x_) : x(x_ % M) {}
constexpr ModInt(int x_) : x(((x_ %= static_cast<int>(M)) < 0) ? (x_ + static_cast<int>(M)) : x_) {}
constexpr ModInt(long long x_) : x(((x_ %= static_cast<long long>(M)) < 0) ? (x_ + static_cast<long long>(M)) : x_) {}
ModInt &operator+=(const ModInt &a) { x = ((x += a.x) >= M) ? (x - M) : x; return *this; }
ModInt &operator-=(const ModInt &a) { x = ((x -= a.x) >= M) ? (x + M) : x; return *this; }
ModInt &operator*=(const ModInt &a) { x = (static_cast<unsigned long long>(x) * a.x) % M; return *this; }
ModInt &operator/=(const ModInt &a) { return (*this *= a.inv()); }
ModInt pow(long long e) const {
if (e < 0) return inv().pow(-e);
ModInt a = *this, b = 1U; for (; e; e >>= 1) { if (e & 1) b *= a; a *= a; } return b;
}
ModInt inv() const {
unsigned a = M, b = x; int y = 0, z = 1;
for (; b; ) { const unsigned q = a / b; const unsigned c = a - q * b; a = b; b = c; const int w = y - static_cast<int>(q) * z; y = z; z = w; }
assert(a == 1U); return ModInt(y);
}
ModInt operator+() const { return *this; }
ModInt operator-() const { ModInt a; a.x = x ? (M - x) : 0U; return a; }
ModInt operator+(const ModInt &a) const { return (ModInt(*this) += a); }
ModInt operator-(const ModInt &a) const { return (ModInt(*this) -= a); }
ModInt operator*(const ModInt &a) const { return (ModInt(*this) *= a); }
ModInt operator/(const ModInt &a) const { return (ModInt(*this) /= a); }
template <class T> friend ModInt operator+(T a, const ModInt &b) { return (ModInt(a) += b); }
template <class T> friend ModInt operator-(T a, const ModInt &b) { return (ModInt(a) -= b); }
template <class T> friend ModInt operator*(T a, const ModInt &b) { return (ModInt(a) *= b); }
template <class T> friend ModInt operator/(T a, const ModInt &b) { return (ModInt(a) /= b); }
explicit operator bool() const { return x; }
bool operator==(const ModInt &a) const { return (x == a.x); }
bool operator!=(const ModInt &a) const { return (x != a.x); }
friend std::ostream &operator<<(std::ostream &os, const ModInt &a) { return os << a.x; }
};
////////////////////////////////////////////////////////////////////////////////
constexpr unsigned MO = 998244353;
using Mint = ModInt<MO>;
// quo[i - 1] < x <= quo[i] <=> floor(N/x) = quo[len - i] (1 <= i <= len - 1)
struct Quotients {
long long N;
int N2, N3, N4, N6;
int len;
explicit Quotients(long long N_ = 0) : N(N_) {
N2 = sqrt(static_cast<long double>(N));
N3 = cbrt(static_cast<long double>(N));
for (; static_cast<long long>(N3) * N3 * N3 < N; ++N3) {}
for (; static_cast<long long>(N3) * N3 * N3 > N; --N3) {}
N4 = sqrt(static_cast<long double>(N2));
N6 = sqrt(static_cast<long double>(N3));
len = 2 * N2 + ((static_cast<long long>(N2) * (N2 + 1) <= N) ? 1 : 0);
}
long long operator[](int i) const {
return (i <= N2) ? i : (N / (len - i));
}
int indexOf(long long x) const {
return (x <= N2) ? x : (len - (N / x));
}
friend std::ostream &operator<<(std::ostream &os, const Quotients &quo) {
os << "[";
for (int i = 0; i < quo.len; ++i) {
if (i) os << ", ";
os << quo[i];
}
os << "]";
return os;
}
};
template <class T> struct Dirichlet {
Quotients quo;
vector<T> ts;
explicit Dirichlet(long long N_ = 0) : quo(N_), ts(quo.len) {}
T operator[](int i) const { return ts[i]; }
T &operator[](int i) { return ts[i]; }
T operator()(long long x) const { return ts[quo.indexOf(x)]; }
T &operator()(long long x) { return ts[quo.indexOf(x)]; }
T point(int i) const { return ts[i] - ts[i - 1]; }
friend std::ostream &operator<<(std::ostream &os, const Dirichlet &A) {
os << "[";
for (int i = 0; i < A.quo.len; ++i) {
if (i) os << ", ";
os << A.quo[i] << ":" << A.ts[i];
}
os << "]";
return os;
}
Dirichlet &operator+=(const Dirichlet &A) {
assert(quo.N == A.quo.N);
for (int i = 0; i < quo.len; ++i) ts[i] += A.ts[i];
return *this;
}
Dirichlet &operator-=(const Dirichlet &A) {
assert(quo.N == A.quo.N);
for (int i = 0; i < quo.len; ++i) ts[i] -= A.ts[i];
return *this;
}
Dirichlet &operator*=(const T &t) {
for (int i = 0; i < quo.len; ++i) ts[i] *= t;
return *this;
}
Dirichlet operator+() const {
return *this;
}
Dirichlet operator-() const {
Dirichlet A(quo.N);
for (int i = 0; i < quo.len; ++i) A.ts[i] = -ts[i];
return A;
}
Dirichlet operator+(const Dirichlet &A) const { return Dirichlet(*this) += A; }
Dirichlet operator-(const Dirichlet &A) const { return Dirichlet(*this) -= A; }
Dirichlet operator*(const T &t) const { return Dirichlet(*this) *= t; }
friend Dirichlet operator*(const T &t, const Dirichlet &A) { return A * t; }
// Assumes a: completely multiplicative.
// a(n) *= [n: prime]
// O(N^(3/4) log(N)^-1) time (O(N^(3/4)) if broken)
// for p: prime <= N^(1/2) (incr.):
// for p^2 <= n <= N (decr.):
// A(n) -= a(p) (A(n/p) - A(p-1))
void primeSum() {
vector<int> isPrime(quo.N2 + 1, 1);
isPrime[0] = isPrime[1] = 0;
for (int p = 2; p <= quo.N2; ++p) if (isPrime[p]) {
for (int n = 2 * p; n <= quo.N2; n += p) isPrime[n] = 0;
}
Dirichlet &A = *this;
for (int p = 2; p <= quo.N2; ++p) if (isPrime[p]) {
const T ap = A.point(p);
if (ap) {
const long long p2 = static_cast<long long>(p) * p;
for (int i = quo.len; quo[--i] >= p2; ) {
A[i] -= ap * (A(quo[i] / p) - A[p - 1]);
}
}
}
for (int i = 1; i < quo.len; ++i) A[i] -= 1;
}
// Assumes a(n) = [n: prime] f(n).
// Makes a to be multiplicative, a(p^e) = f(p^e).
// f(p, e, p^e) (e >= 2) should return f(p^e).
// O(N^(3/4) log(N)^-1) time
// for p: prime <= N^(1/2) (decr.):
// for p^2 <= n <= N (decr.):
// A(n) += \sum[e>=1,p^(e+1)<=n] (f(p^e) (A(n/p) - A(p)) + f(p^(e+1)))
template <class F> void multiplicativeSum(F f) {
vector<int> isPrime(quo.N2 + 1, 1);
isPrime[0] = isPrime[1] = 0;
for (int p = 2; p <= quo.N2; ++p) if (isPrime[p]) {
for (int n = 2 * p; n <= quo.N2; n += p) isPrime[n] = 0;
}
Dirichlet &A = *this;
for (int i = 1; i < quo.len; ++i) A[i] += 1;
for (int p = quo.N2; p >= 2; --p) if (isPrime[p]) {
vector<long long> pps{1, p};
vector<T> fs{1, A.point(p)};
for (int e = 2; pps.back() <= quo.N / p; ++e) {
pps.push_back(pps.back() * p);
fs.push_back(f(p, e, pps.back()));
}
for (int i = quo.len; quo[--i] >= pps[2]; ) {
long long nn = quo[i];
for (int e = 1; (nn /= p) >= p; ++e) {
A[i] += fs[e] * (A(nn) - A[p]) + fs[e + 1];
}
}
}
}
// a(n) = n^k
Dirichlet Id(int k) const;
// a(n) = [n: prime] n^k
Dirichlet IdPrime(int k) const;
// a(p) = \sum[k] coefs[k] p^k
// a(p^e) = f(p, e, p^e) (e >= 2)
template <class F>
Dirichlet IdMultiplicative(const vector<T> &coefs, F f) const;
};
template <class T> T powerSum(int k, long long n) {
if (k == 0) {
return T(n);
} else if (k == 1) {
long long ns[2] = {n, n + 1};
ns[n % 2] /= 2;
return T(ns[0]) * T(ns[1]);
} else if (k == 2) {
long long ns[3] = {n, 2 * n + 1, n + 1};
ns[n % 2 * 2] /= 2;
ns[n % 3] /= 3;
return T(ns[0]) * T(ns[1]) * T(ns[2]);
} else if (k == 3) {
const T t = powerSum<T>(1, n);
return t * t;
} else if (k == 4) {
long long ns[5] = {n, n - 1, 2 * n + 1, n + 2, n + 1};
ns[n % 2] /= 2;
ns[n % 5] /= 5;
return T(ns[0]) * T(ns[1]) * T(ns[2]) * T(ns[3]) * T(ns[4]) + powerSum<T>(2, n);
} else if (k == 5) {
long long ns[3] = {n, n - 1, n + 1};
ns[n % 2] /= 2;
ns[n % 3] /= 3;
return T(ns[0]) * T(ns[1]) * T(ns[2]) * T(n) * T(n + 1) * T(n + 2) + powerSum<T>(3, n);
} else {
assert(false);
}
}
template <class T> Dirichlet<T> Id(int k, long long N) {
Dirichlet<T> A(N);
const Quotients quo = A.quo;
for (int n = 1; n <= quo.N2; ++n) {
T t = 1;
for (int j = 0; j < k; ++j) t *= n;
A[n] = A[n - 1] + t;
}
for (int i = quo.N2 + 1; i < quo.len; ++i) A[i] = powerSum<T>(k, quo[i]);
return A;
}
template <class T> Dirichlet<T> IdPrime(int k, long long N) {
auto A = Id<T>(k, N);
A.primeSum();
return A;
}
template <class T, class F>
Dirichlet<T> IdMultiplicative(const vector<T> &coefs, F f, long long N) {
Dirichlet<T> A(N);
for (int k = 0; k < static_cast<int>(coefs.size()); ++k) if (coefs[k]) {
A += coefs[k] * A.IdPrime(k);
}
A.multiplicativeSum(f);
return A;
}
template <class T> Dirichlet<T> Dirichlet<T>::Id(int k) const {
return ::Id<T>(k, quo.N);
}
template <class T> Dirichlet<T> Dirichlet<T>::IdPrime(int k) const {
return ::IdPrime<T>(k, quo.N);
}
template <class T> template <class F>
Dirichlet<T> Dirichlet<T>::IdMultiplicative(const vector<T> &coefs, F f) const {
return ::IdMultiplicative<T>(coefs, f, quo.N);
}
////////////////////////////////////////////////////////////////////////////////
/*
connect for d = N,...,1
(pi(N/d) - pi(N/d/2) + 1) are isolated, others are connected
*/
int main() {
Int N;
for (; ~scanf("%lld", &N); ) {
const auto pi = IdPrime<Int>(0, N);
const auto &quo = pi.quo;
Mint ans = 1;
for (int i = 1; i < quo.len; ++i) {
const Int n = quo[quo.len - i];
const Int isol = pi(n) - pi(n/2) + 1;
const Mint t = (n - isol) ? ((n - isol) * Mint(n).pow((isol + 1) - 2)) : Mint(n).pow(n - 2);
// cerr<<"("<<quo[i-1]<<", "<<quo[i]<<"]: "<<n<<" "<<isol<<" "<<t<<endl;
ans *= t.pow(quo[i] - quo[i - 1]);
}
printf("%u\n", ans.x);
}
return 0;
}
这程序好像有点Bug,我给组数据试试?
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 4160kb
input:
4
output:
8
result:
ok answer is '8'
Test #2:
score: 0
Accepted
time: 1ms
memory: 3900kb
input:
2
output:
1
result:
ok answer is '1'
Test #3:
score: 0
Accepted
time: 1ms
memory: 3784kb
input:
123
output:
671840470
result:
ok answer is '671840470'
Test #4:
score: 0
Accepted
time: 1ms
memory: 3876kb
input:
233
output:
353738465
result:
ok answer is '353738465'
Test #5:
score: 0
Accepted
time: 1ms
memory: 3960kb
input:
5981
output:
970246821
result:
ok answer is '970246821'
Test #6:
score: 0
Accepted
time: 1ms
memory: 3760kb
input:
86422
output:
897815688
result:
ok answer is '897815688'
Test #7:
score: 0
Accepted
time: 1ms
memory: 4092kb
input:
145444
output:
189843901
result:
ok answer is '189843901'
Test #8:
score: 0
Accepted
time: 1ms
memory: 3968kb
input:
901000
output:
819449452
result:
ok answer is '819449452'
Test #9:
score: 0
Accepted
time: 1ms
memory: 3972kb
input:
1000000
output:
113573943
result:
ok answer is '113573943'
Test #10:
score: 0
Accepted
time: 0ms
memory: 3928kb
input:
23333333
output:
949849384
result:
ok answer is '949849384'
Test #11:
score: 0
Accepted
time: 2ms
memory: 3816kb
input:
102850434
output:
604886751
result:
ok answer is '604886751'
Test #12:
score: 0
Accepted
time: 20ms
memory: 4064kb
input:
998244353
output:
0
result:
ok answer is '0'
Test #13:
score: 0
Accepted
time: 20ms
memory: 3984kb
input:
1000000007
output:
318420284
result:
ok answer is '318420284'
Test #14:
score: 0
Accepted
time: 33ms
memory: 4172kb
input:
2147483547
output:
688759898
result:
ok answer is '688759898'
Test #15:
score: 0
Accepted
time: 55ms
memory: 4436kb
input:
5120103302
output:
116870489
result:
ok answer is '116870489'
Test #16:
score: 0
Accepted
time: 150ms
memory: 6216kb
input:
19834593299
output:
523663743
result:
ok answer is '523663743'
Test #17:
score: 0
Accepted
time: 288ms
memory: 7556kb
input:
52500109238
output:
195086665
result:
ok answer is '195086665'
Test #18:
score: 0
Accepted
time: 416ms
memory: 8840kb
input:
84848352911
output:
107959260
result:
ok answer is '107959260'
Test #19:
score: 0
Accepted
time: 477ms
memory: 9664kb
input:
99824435322
output:
0
result:
ok answer is '0'
Test #20:
score: 0
Accepted
time: 465ms
memory: 9576kb
input:
99999999354
output:
316301711
result:
ok answer is '316301711'
Test #21:
score: 0
Accepted
time: 463ms
memory: 9420kb
input:
100000000000
output:
396843576
result:
ok answer is '396843576'
Extra Test:
score: 0
Extra Test Passed