QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#686467#9515. 无限地狱hos_lyric#34 89ms4188kbC++145.8kb2024-10-29 13:34:452024-10-29 13:34:47

Judging History

你现在查看的是最新测评结果

  • [2024-10-29 13:34:47]
  • 评测
  • 测评结果:34
  • 用时:89ms
  • 内存:4188kb
  • [2024-10-29 13:34:45]
  • 提交

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); ) {
    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<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> P(N + 1, 0);
    for (int n = 2; n <= N; ++n) {
      // \sum[d|n] mu(d) (2^(floor(n/d/2)) - 1)
      for (int d = 1; d <= n; ++d) if (n % d == 0) P[n] += moe[d] * (two[n/d/2] - 1);
      // start with B
      P[n] -= two[n/2 - 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] = two[n - 1] - 1;
      {
        // \sum[1<=d<=n] mu(d) (2^(n/d) - 1)
        for (int d = 1; d <= n; ++d) G2[n] += moe[d] * (two[n/d] - 1);
        // start with B
        G2[n] -= two[n - 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: 1ms
memory: 4076kb

input:

6

output:

38

result:

ok 1 number(s): "38"

Test #2:

score: 4
Accepted
time: 1ms
memory: 4084kb

input:

7

output:

73

result:

ok 1 number(s): "73"

Test #3:

score: 4
Accepted
time: 1ms
memory: 3728kb

input:

8

output:

148

result:

ok 1 number(s): "148"

Test #4:

score: 4
Accepted
time: 1ms
memory: 3808kb

input:

9

output:

284

result:

ok 1 number(s): "284"

Test #5:

score: 4
Accepted
time: 1ms
memory: 3784kb

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: 1ms
memory: 3800kb

input:

30

output:

536938322

result:

ok 1 number(s): "536938322"

Test #7:

score: 13
Accepted
time: 1ms
memory: 3732kb

input:

35

output:

210046687

result:

ok 1 number(s): "210046687"

Test #8:

score: 13
Accepted
time: 1ms
memory: 3800kb

input:

38

output:

680532913

result:

ok 1 number(s): "680532913"

Test #9:

score: 13
Accepted
time: 1ms
memory: 3800kb

input:

39

output:

362030079

result:

ok 1 number(s): "362030079"

Test #10:

score: 13
Accepted
time: 0ms
memory: 3780kb

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: 37ms
memory: 3896kb

input:

2000

output:

686289840

result:

ok 1 number(s): "686289840"

Test #12:

score: 17
Accepted
time: 63ms
memory: 3804kb

input:

2500

output:

672176744

result:

ok 1 number(s): "672176744"

Test #13:

score: 17
Accepted
time: 88ms
memory: 4188kb

input:

2998

output:

77001108

result:

ok 1 number(s): "77001108"

Test #14:

score: 17
Accepted
time: 89ms
memory: 3908kb

input:

2999

output:

337824775

result:

ok 1 number(s): "337824775"

Test #15:

score: 17
Accepted
time: 88ms
memory: 3968kb

input:

3000

output:

636156660

result:

ok 1 number(s): "636156660"

Subtask #4:

score: 0
Time Limit Exceeded

Dependency #3:

100%
Accepted

Test #16:

score: 0
Time Limit Exceeded

input:

100000

output:


result:


Subtask #5:

score: 0
Skipped

Dependency #4:

0%

Subtask #6:

score: 0
Skipped

Dependency #5:

0%