QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#686509 | #9515. 无限地狱 | hos_lyric# | 34 | 4347ms | 86352kb | C++14 | 7.4kb | 2024-10-29 13:55:57 | 2024-10-29 13:55:57 |
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>;
inline long long divide(long long a, int b) {
return a / b;
}
inline long long divide(long long a, long long b) {
return a / b;
}
// quo[i - 1] < x <= quo[i] <=> floor(N/x) = quo[len - i] (1 <= i <= len - 1)
struct Quotients {
long long N;
int N2;
int len;
Quotients(long long N_ = 0) : N(N_) {
N2 = sqrt(static_cast<long double>(N));
len = 2 * N2 + ((static_cast<long long>(N2) * (N2 + 1) <= N) ? 1 : 0);
}
long long operator[](int i) const {
return (i <= N2) ? i : divide(N, len - i);
}
int indexOf(long long x) const {
return (x <= N2) ? x : (len - divide(N, x));
}
friend std::ostream &operator<<(std::ostream &os, const Quotients &quo) {
os << "[";
for (int i = 0; i < quo.len; ++i) {
if (i > 0) os << ", ";
os << quo[i];
}
os << "]";
return os;
}
};
int main() {
Int N;
for (; ~scanf("%lld", &N); ) {
vector<int> lpf(N + 1, 0), moe(N + 1, 0);
for (int p = 2; p <= N; ++p) lpf[p] = p;
moe[1] = 1;
for (int p = 2; p <= N; ++p) if (lpf[p] == p) {
for (int n = p; n <= N; n += p) {
chmin(lpf[n], p);
moe[n] = (n / p % p) ? -moe[n / p] : 0;
}
}
vector<int> Moe(N + 1, 0);
for (int n = 1; n <= N; ++n) Moe[n] = Moe[n - 1] + moe[n];
vector<vector<int>> divss(N + 1);
for (int d = 1; d <= N; ++d) for (int n = d; n <= N; n += d) divss[n].push_back(d);
vector<Mint> two(N + 1);
two[0] = 1;
for (int n = 1; n <= N; ++n) two[n] = two[n - 1] * 2;
/*
[1, n-1]: palindrome
A,B appears in this order
gcd(B) = 1
*/
vector<Mint> pal(N + 1, 0);
vector<Mint> Pal(N + 1, 0);
for (int n = 2; n <= N; ++n) {
// \sum[d|n] mu(d) (2^(floor(n/d/2)) - 1)
for (const int d : divss[n]) pal[n] += moe[d] * (two[n/d/2] - 1);
// start with B
pal[n] -= two[n/2 - 1];
Pal[n] = Pal[n - 1] + pal[n];
}
// cerr<<"pal = "<<pal<<endl;
// ignore order
vector<Mint> f1(N + 1, 0), f2(N + 1, 0), f3(N + 1, 0), iroiro(N + 1, 0);
vector<Mint> F1Half(N + 1, 0), F2Half(N + 1, 0);
// gcd(B) = 1
vector<Mint> g2(N + 1, 0), g3(N + 1, 0);
// mu * (2^(n-1))
for (int d = 1; d <= N; ++d) for (int n = 1; d * n <= N; ++n) g2[d * n] += moe[d] * two[n - 1];
for (int n = 1; n <= N; ++n) g2[n] += g2[n - 1];
// start with B
for (int n = 1; n <= N; ++n) g2[n] -= two[n - 1];
for (int n = 1; n <= N; ++n) {
const Quotients quo(n);
f1[n] = 1;
f2[n] = two[n - 1] - 1;
F1Half[n] = F1Half[n - 1] + f1[n/2];
F2Half[n] = F2Half[n - 1] + f2[n/2];
/*
A,B,C appears in this order
g := gcd(C)
- g >= 2
- non-multiples of g is determined by mod g
- [1, g - 1] is palindrome
- multiples of g: recursively (but gcd = 1)
*/
for (int i = 2; i < quo.len; ++i) {
// quo[i - 1] < g <= quo[i]
const int gL = quo[i - 1];
const int gR = quo[i];
const int nn = quo[quo.len - i];
// BC, BCA
f3[n] += (F1Half[gR] - F1Half[gL]) * (g2[nn] + g3[nn]);
// C, AC, BC, CA, CB, ACB, BCA, CAB, CBA
f3[n] += (F2Half[gR] - F2Half[gL]) * iroiro[nn];
g3[n] += (Pal[gR] - Pal[gL]) * iroiro[nn];
}
iroiro[n] = (f1[n] + 2*g2[n] + 2*f2[n] + 2*g3[n] + 2*f3[n]);
}
// cerr<<"f1 = "<<f1<<endl;
// cerr<<"f2 = "<<f2<<endl;
// cerr<<"f3 = "<<f3<<endl;
// cerr<<"g2 = "<<g2<<endl;
// cerr<<"g3 = "<<g3<<endl;
const Mint ans = f1[N] + f2[N] + f3[N];
printf("%u\n", ans.x);
}
return 0;
}
详细
Subtask #1:
score: 4
Accepted
Test #1:
score: 4
Accepted
time: 0ms
memory: 3804kb
input:
6
output:
38
result:
ok 1 number(s): "38"
Test #2:
score: 4
Accepted
time: 0ms
memory: 4040kb
input:
7
output:
73
result:
ok 1 number(s): "73"
Test #3:
score: 4
Accepted
time: 0ms
memory: 3976kb
input:
8
output:
148
result:
ok 1 number(s): "148"
Test #4:
score: 4
Accepted
time: 0ms
memory: 3856kb
input:
9
output:
284
result:
ok 1 number(s): "284"
Test #5:
score: 4
Accepted
time: 0ms
memory: 3984kb
input:
10
output:
565
result:
ok 1 number(s): "565"
Subtask #2:
score: 13
Accepted
Dependency #1:
100%
Accepted
Test #6:
score: 13
Accepted
time: 0ms
memory: 3864kb
input:
30
output:
536938322
result:
ok 1 number(s): "536938322"
Test #7:
score: 13
Accepted
time: 0ms
memory: 3936kb
input:
35
output:
210046687
result:
ok 1 number(s): "210046687"
Test #8:
score: 13
Accepted
time: 0ms
memory: 3964kb
input:
38
output:
680532913
result:
ok 1 number(s): "680532913"
Test #9:
score: 13
Accepted
time: 0ms
memory: 3988kb
input:
39
output:
362030079
result:
ok 1 number(s): "362030079"
Test #10:
score: 13
Accepted
time: 0ms
memory: 3864kb
input:
40
output:
723529503
result:
ok 1 number(s): "723529503"
Subtask #3:
score: 17
Accepted
Dependency #2:
100%
Accepted
Test #11:
score: 17
Accepted
time: 0ms
memory: 4148kb
input:
2000
output:
686289840
result:
ok 1 number(s): "686289840"
Test #12:
score: 17
Accepted
time: 2ms
memory: 4180kb
input:
2500
output:
672176744
result:
ok 1 number(s): "672176744"
Test #13:
score: 17
Accepted
time: 3ms
memory: 4300kb
input:
2998
output:
77001108
result:
ok 1 number(s): "77001108"
Test #14:
score: 17
Accepted
time: 3ms
memory: 4312kb
input:
2999
output:
337824775
result:
ok 1 number(s): "337824775"
Test #15:
score: 17
Accepted
time: 3ms
memory: 4380kb
input:
3000
output:
636156660
result:
ok 1 number(s): "636156660"
Subtask #4:
score: 0
Time Limit Exceeded
Dependency #3:
100%
Accepted
Test #16:
score: 21
Accepted
time: 387ms
memory: 19060kb
input:
100000
output:
809175948
result:
ok 1 number(s): "809175948"
Test #17:
score: 21
Accepted
time: 1106ms
memory: 35552kb
input:
200000
output:
425311829
result:
ok 1 number(s): "425311829"
Test #18:
score: 21
Accepted
time: 4347ms
memory: 86352kb
input:
500000
output:
302623178
result:
ok 1 number(s): "302623178"
Test #19:
score: 0
Time Limit Exceeded
input:
900000
output:
result:
Subtask #5:
score: 0
Skipped
Dependency #4:
0%
Subtask #6:
score: 0
Skipped
Dependency #5:
0%