QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#641917#426. 传统艺能Elegia100 ✓737ms11724kbC++143.9kb2024-10-15 03:18:332024-10-15 03:18:34

Judging History

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

  • [2024-10-15 03:18:34]
  • 评测
  • 测评结果:100
  • 用时:737ms
  • 内存:11724kb
  • [2024-10-15 03:18:33]
  • 提交

answer

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cctype>

#include <algorithm>
#include <random>
#include <bitset>
#include <queue>
#include <functional>
#include <set>
#include <map>
#include <vector>
#include <chrono>
#include <iostream>
#include <limits>
#include <numeric>

#define LOG(FMT...) fprintf(stderr, FMT)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

template<class T>
istream &operator>>(istream &is, vector<T> &v) {
  for (T &x : v)
    is >> x;
  return is;
}

template<class T>
ostream &operator<<(ostream &os, const vector<T> &v) {
  if (!v.empty()) {
    os << v.front();
    for (int i = 1; i < v.size(); ++i)
      os << ' ' << v[i];
  }
  return os;
}

const int P = 998244353;

int norm(int x) { return x >= P ? (x - P) : x; }

void add(int &x, int y) { if ((x += y) >= P) x -= P; }

void sub(int &x, int y) { if ((x -= y) < 0) x += P; }

void exGcd(int a, int b, int &x, int &y) {
  if (!b) {
    x = 1;
    y = 0;
    return;
  }
  exGcd(b, a % b, y, x);
  y -= a / b * x;
}

int inv(int a) {
  int x, y;
  exGcd(a, P, x, y);
  return norm(x + P);
}

int mpow(int x, int k) {
  int ret = 1;
  while (k) {
    if (k & 1)
      ret = ret * (ll) x % P;
    x = x * (ll) x % P;
    k >>= 1;
  }
  return ret;
}

struct Mat {
  int g[3][3];

  Mat() { memset(g, 0, sizeof(g)); }

  Mat operator*(const Mat &rhs) const {
    Mat ret = Mat();
    for (int i = 0; i < 3; ++i)
      for (int j = 0; j < 3; ++j)
        for (int k = 0; k < 3; ++k)
          ret.g[i][k] = (ret.g[i][k] + g[i][j] * (ll) rhs.g[j][k]) % P;
    return ret;
  }
};

Mat power(Mat mat, int k) {
  Mat ret = Mat();
  for (int i = 0; i < 3; ++i) ret.g[i][i] = 1;
  while (k) {
    if (k & 1)
      ret = ret * mat;
    mat = mat * mat;
    k >>= 1;
  }
  return ret;
}

int len(int x) { return x * (1LL + x) / 2 % P; }

int main() {
#ifdef ELEGIA
  freopen("test.in", "r", stdin);
  int nol_cl = clock();
#endif
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int n, k;
  cin >> n >> k;
  int nv = inv(n * (n + 1LL) / 2 % P);
  int ans = 0;
  function<void(int, int, int, int, int)> dfs = [&](int l, int r, int cov, int oth, int exa) {
    // 0: no, noprt
    // 1: have
    // 2: no, haveprt

    // u: cover but dont pushto
    // v: go across
    // w: pushto
    // y: nothing happened
    // z: exact

    //cerr << l << ", " << r << ": covdontpush " << cov << " pushto " << oth << " across "
    //     << ((r - l) * (n - (r - l + 1LL)) + len(r - l + 1) - 1) << ' ';

    int u = cov * (ll) nv % P, v = ((r - l) * (n - (r - l + 1LL)) + len(r - l + 1) - 1) % P * nv % P, w =
            oth * (ll) nv % P, z = exa * (ll)nv % P, y = (P * 5LL + 1 - u - v - w - z) % P;
    //cerr << "exact " << exa << " rest " << (y * (ll)len(n) % P) << '\n';
    Mat trans = Mat();
    trans.g[0][0] = norm(norm(v + w) + y);
    trans.g[0][1] = z;
    trans.g[0][2] = u;
    trans.g[1][0] = v;
    trans.g[1][1] = norm(norm(w + norm(u + z)) + y);
    trans.g[2][0] = v;
    trans.g[2][1] = norm(w + z);
    trans.g[2][2] = norm(u + y);

    add(ans, power(trans, k).g[0][1]);
    //cerr << l << ", " << r << ": " << (power(trans, k).g[0][1] * (ll) mpow(len(n), k) % P) << "/" << mpow(len(n), k)
    //     << '\n';

    add(cov, exa);
    if (l < r) {
      int mid;
      cin >> mid;
      dfs(l, mid, cov, ((r - mid) * (ll) (n - r) + len(r - mid)) % P, l * (ll)(r - mid) % P);
      dfs(mid + 1, r, cov, ((mid - l + 1) * (l - 1LL) + len(mid - l + 1)) % P, (mid - l + 1LL) * (n - r + 1) % P);
    }
  };
  dfs(1, n, 0, 0, 1);
  //cerr << (ans * (ll)mpow(len(n), k) % P) << '\n';
  cout << ans << '\n';

#ifdef ELEGIA
  LOG("Time: %dms\n", int ((clock()
          -nol_cl) / (double)CLOCKS_PER_SEC * 1000));
#endif
  return 0;
}

詳細信息


Pretests


Final Tests

Test #1:

score: 10
Accepted
time: 1ms
memory: 3648kb

input:

10 4
3 2 1 8 6 5 4 7 9

output:

181617343

result:

ok 1 number(s): "181617343"

Test #2:

score: 10
Accepted
time: 0ms
memory: 3552kb

input:

10 91
9 8 5 3 1 2 4 6 7

output:

979294405

result:

ok 1 number(s): "979294405"

Test #3:

score: 10
Accepted
time: 1ms
memory: 3868kb

input:

5 985433385
4 1 3 2

output:

581616517

result:

ok 1 number(s): "581616517"

Test #4:

score: 10
Accepted
time: 54ms
memory: 11724kb

input:

194809 1
194807 3 2 1 12 9 8 4 5 7 6 10 11 14 13 22 21 19 16 15 18 17 20 194798 24 23 194795 194790 194785 27 26 25 31 28 29 30 40 34 33 32 39 38 36 35 37 48 46 45 44 43 41 42 47 194782 50 49 194778 194770 56 55 54 51 52 53 194764 194762 194753 194747 60 57 58 59 67 63 61 62 64 66 65 74 68 69 73 72 ...

output:

67027862

result:

ok 1 number(s): "67027862"

Test #5:

score: 10
Accepted
time: 1ms
memory: 3616kb

input:

32 946189239
16 8 4 2 1 3 6 5 7 12 10 9 11 14 13 15 24 20 18 17 19 22 21 23 28 26 25 27 30 29 31

output:

602568459

result:

ok 1 number(s): "602568459"

Test #6:

score: 10
Accepted
time: 1ms
memory: 3804kb

input:

64 927627601
32 16 8 4 2 1 3 6 5 7 12 10 9 11 14 13 15 24 20 18 17 19 22 21 23 28 26 25 27 30 29 31 48 40 36 34 33 35 38 37 39 44 42 41 43 46 45 47 56 52 50 49 51 54 53 55 60 58 57 59 62 61 63

output:

289230741

result:

ok 1 number(s): "289230741"

Test #7:

score: 10
Accepted
time: 15ms
memory: 3620kb

input:

4096 915714134
2048 1024 512 256 128 64 32 16 8 4 2 1 3 6 5 7 12 10 9 11 14 13 15 24 20 18 17 19 22 21 23 28 26 25 27 30 29 31 48 40 36 34 33 35 38 37 39 44 42 41 43 46 45 47 56 52 50 49 51 54 53 55 60 58 57 59 62 61 63 96 80 72 68 66 65 67 70 69 71 76 74 73 75 78 77 79 88 84 82 81 83 86 85 87 92 90...

output:

246047302

result:

ok 1 number(s): "246047302"

Test #8:

score: 10
Accepted
time: 17ms
memory: 3612kb

input:

4634 906063294
2144 1427 815 694 205 105 55 10 5 1 3 2 4 6 7 8 9 43 41 24 18 16 12 11 13 15 14 17 23 19 21 20 22 34 32 31 29 25 26 27 28 30 33 37 36 35 38 40 39 42 46 45 44 52 47 49 48 50 51 53 54 100 77 72 57 56 64 60 58 59 61 62 63 70 69 65 67 66 68 71 75 74 73 76 85 82 80 78 79 81 84 83 87 86 90 ...

output:

416904570

result:

ok 1 number(s): "416904570"

Test #9:

score: 10
Accepted
time: 329ms
memory: 7592kb

input:

95552 924470405
7 3 1 2 4 6 5 10 8 9 95547 16 11 15 14 12 13 95543 95533 25 24 23 19 17 18 22 21 20 27 26 95528 34 29 28 33 30 32 31 40 35 39 36 38 37 95519 47 44 41 43 42 45 46 95517 95514 56 49 48 55 54 53 52 50 51 59 57 58 64 60 62 61 63 95509 66 65 69 68 67 71 70 78 77 72 75 73 74 76 95504 82 81...

output:

778611049

result:

ok 1 number(s): "778611049"

Test #10:

score: 10
Accepted
time: 737ms
memory: 11576kb

input:

194473 918039998
194469 5 1 2 3 4 10 6 7 9 8 194466 19 12 11 17 13 16 15 14 18 194462 194457 194450 194445 194437 194428 194422 194420 194415 194413 194407 194405 194402 194400 23 21 20 22 194396 194393 194389 194382 31 24 30 29 28 27 26 25 194375 34 32 33 39 36 35 38 37 42 40 41 194372 45 43 44 194...

output:

128548459

result:

ok 1 number(s): "128548459"

Extra Test:

score: 0
Extra Test Passed