QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#686527#9515. 无限地狱hos_lyric#55 1015ms156432kbC++146.2kb2024-10-29 14:02:522024-10-29 14:02:55

Judging History

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

  • [2024-10-29 14:02:55]
  • 评测
  • 测评结果:55
  • 用时:1015ms
  • 内存:156432kb
  • [2024-10-29 14:02:52]
  • 提交

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<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);
    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];
    }
// cerr<<"pal = "<<pal<<endl;
    
    // ignore order
    vector<Mint> f1(N + 1, 0), f2(N + 1, 0), f3(N + 1, 0), iroiro(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) {
      f1[n] = 1;
      f2[n] = two[n - 1] - 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)
      */
      f3[n] = f3[n - 1];
      g3[n] = g3[n - 1];
      for (const int g : divss[n]) if (g >= 2) {
        const int nn = n / g;
        // BC, BCA
        f3[n] += f1[g/2] * ((g2[nn] + g3[nn]) - (g2[nn - 1] + g3[nn - 1]));
        // C, AC, BC, CA, CB, ACB, BCA, CAB, CBA
        f3[n] += f2[g/2] * (iroiro[nn] - iroiro[nn - 1]);
        g3[n] += pal[g] * (iroiro[nn] - iroiro[nn - 1]);
      }
      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: 3808kb

input:

6

output:

38

result:

ok 1 number(s): "38"

Test #2:

score: 4
Accepted
time: 0ms
memory: 3760kb

input:

7

output:

73

result:

ok 1 number(s): "73"

Test #3:

score: 4
Accepted
time: 0ms
memory: 3828kb

input:

8

output:

148

result:

ok 1 number(s): "148"

Test #4:

score: 4
Accepted
time: 0ms
memory: 4104kb

input:

9

output:

284

result:

ok 1 number(s): "284"

Test #5:

score: 4
Accepted
time: 0ms
memory: 3824kb

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: 3860kb

input:

30

output:

536938322

result:

ok 1 number(s): "536938322"

Test #7:

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

input:

35

output:

210046687

result:

ok 1 number(s): "210046687"

Test #8:

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

input:

38

output:

680532913

result:

ok 1 number(s): "680532913"

Test #9:

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

input:

39

output:

362030079

result:

ok 1 number(s): "362030079"

Test #10:

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

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

input:

2000

output:

686289840

result:

ok 1 number(s): "686289840"

Test #12:

score: 17
Accepted
time: 1ms
memory: 4404kb

input:

2500

output:

672176744

result:

ok 1 number(s): "672176744"

Test #13:

score: 17
Accepted
time: 1ms
memory: 4196kb

input:

2998

output:

77001108

result:

ok 1 number(s): "77001108"

Test #14:

score: 17
Accepted
time: 1ms
memory: 4136kb

input:

2999

output:

337824775

result:

ok 1 number(s): "337824775"

Test #15:

score: 17
Accepted
time: 1ms
memory: 4200kb

input:

3000

output:

636156660

result:

ok 1 number(s): "636156660"

Subtask #4:

score: 21
Accepted

Dependency #3:

100%
Accepted

Test #16:

score: 21
Accepted
time: 58ms
memory: 17528kb

input:

100000

output:

809175948

result:

ok 1 number(s): "809175948"

Test #17:

score: 21
Accepted
time: 119ms
memory: 32480kb

input:

200000

output:

425311829

result:

ok 1 number(s): "425311829"

Test #18:

score: 21
Accepted
time: 398ms
memory: 78264kb

input:

500000

output:

302623178

result:

ok 1 number(s): "302623178"

Test #19:

score: 21
Accepted
time: 887ms
memory: 140908kb

input:

900000

output:

683174559

result:

ok 1 number(s): "683174559"

Test #20:

score: 21
Accepted
time: 1015ms
memory: 156432kb

input:

1000000

output:

126560600

result:

ok 1 number(s): "126560600"

Subtask #5:

score: 0
Time Limit Exceeded

Dependency #4:

100%
Accepted

Test #21:

score: 0
Time Limit Exceeded

input:

100000000

output:


result:


Subtask #6:

score: 0
Skipped

Dependency #5:

0%