QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#686448 | #9515. 无限地狱 | hos_lyric# | 17 | 0ms | 3848kb | C++14 | 6.0kb | 2024-10-29 13:16:39 | 2024-10-29 13:16:40 |
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>;
int main() {
Int N;
for (; ~scanf("%lld", &N); ) {
/*
[1, n-1]: palindrome
A,B appears in this order
gcd(B) = 1
*/
vector<Mint> P(N + 1, 0);
for (int n = 2; n <= N; ++n) {
vector<Mint> hs(n, 0);
for (int d = 1; d < n; ++d) {
hs[d] = 1;
for (int i = 1; i <= n - i; ++i) {
bool canB = true;
canB = canB && (i != 1);
canB = canB && (i % d == 0);
canB = canB && ((n - i) % d == 0);
if (canB) hs[d] *= 2;
}
hs[d] -= 1;
}
for (int d = n - 1; d >= 1; --d) for (int e = 2 * d; e < n; e += d) hs[d] -= hs[e];
P[n] = hs[1];
}
// cerr<<"P = "<<P<<endl;
// ignore order
vector<Mint> F1(N + 1, 0), F2(N + 1, 0), F3(N + 1, 0);
// gcd(B) = 1
vector<Mint> G2(N + 1, 0), G3(N + 1, 0);
for (int n = 1; n <= N; ++n) {
F1[n] = 1;
F2[n] = (Mint(2).pow(n) - 2) / 2;
{
vector<Mint> hs(n + 1, 0);
for (int d = 1; d <= n; ++d) {
hs[d] = 1;
for (int i = 1; i <= n; ++i) {
bool canB = true;
canB = canB && (i != 1);
canB = canB && (i % d == 0);
if (canB) hs[d] *= 2;
}
hs[d] -= 1;
}
for (int d = n; d >= 1; --d) for (int e = 2 * d; e <= n; e += d) hs[d] -= hs[e];
G2[n] = hs[1];
}
/*
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 g = 2; g <= n; ++g) {
// BC, BCA
F3[n] += F1[g/2] * (G2[n/g] + G3[n/g]);
// C, AC, BC, CA, CB, ACB, BCA, CAB, CBA
F3[n] += F2[g/2] * (F1[n/g] + 2*G2[n/g] + 2*F2[n/g] + 2*G3[n/g] + 2*F3[n/g]);
G3[n] += P[g] * (F1[n/g] + 2*G2[n/g] + 2*F2[n/g] + 2*G3[n/g] + 2*F3[n/g]);
}
}
// 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;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 4
Accepted
Test #1:
score: 4
Accepted
time: 0ms
memory: 3812kb
input:
6
output:
38
result:
ok 1 number(s): "38"
Test #2:
score: 4
Accepted
time: 0ms
memory: 3848kb
input:
7
output:
73
result:
ok 1 number(s): "73"
Test #3:
score: 4
Accepted
time: 0ms
memory: 3736kb
input:
8
output:
148
result:
ok 1 number(s): "148"
Test #4:
score: 4
Accepted
time: 0ms
memory: 3784kb
input:
9
output:
284
result:
ok 1 number(s): "284"
Test #5:
score: 4
Accepted
time: 0ms
memory: 3772kb
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: 3776kb
input:
30
output:
536938322
result:
ok 1 number(s): "536938322"
Test #7:
score: 13
Accepted
time: 0ms
memory: 3816kb
input:
35
output:
210046687
result:
ok 1 number(s): "210046687"
Test #8:
score: 13
Accepted
time: 0ms
memory: 3760kb
input:
38
output:
680532913
result:
ok 1 number(s): "680532913"
Test #9:
score: 13
Accepted
time: 0ms
memory: 3792kb
input:
39
output:
362030079
result:
ok 1 number(s): "362030079"
Test #10:
score: 13
Accepted
time: 0ms
memory: 3772kb
input:
40
output:
723529503
result:
ok 1 number(s): "723529503"
Subtask #3:
score: 0
Time Limit Exceeded
Dependency #2:
100%
Accepted
Test #11:
score: 0
Time Limit Exceeded
input:
2000
output:
result:
Subtask #4:
score: 0
Skipped
Dependency #3:
0%
Subtask #5:
score: 0
Skipped
Dependency #4:
0%
Subtask #6:
score: 0
Skipped
Dependency #5:
0%