QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#448089#7647. 树哈希JWRuixi77 3270ms8988kbC++204.4kb2024-06-19 10:58:562024-06-19 10:58:58

Judging History

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

  • [2024-06-19 10:58:58]
  • 评测
  • 测评结果:77
  • 用时:3270ms
  • 内存:8988kb
  • [2024-06-19 10:58:56]
  • 提交

answer

#ifdef LOCAL
#include "stdafx.h"
#else
#include <bits/stdc++.h>
#define IL inline
#define LL long long
#define eb emplace_back
#define L(i, j, k) for (int i = (j); i <= (k); ++i)
#define R(i, j, k) for (int i = (j); i >= (k); --i)
using namespace std;

using vi = vector<int>;
#endif

constexpr int N = 1e2 + 9;
int n, q, P;

using u32 = uint32_t;
using u64 = uint64_t;
using u128 = __uint128_t;

struct barrett {
  unsigned m;
  u64 B;
  explicit barrett () : m(0), B(0) {}
  explicit barrett (u32 _m) : m(_m), B((u64)(-1) / _m + 1) {}
  u32 mod () const { return m; }
  u32 rem (u64 z) const {
    static u64 x, y;
    x = (u64)(((u128)(z) * B) >> 64);
    y = x * m;
    return z - y + (z < y ? m : 0);
  }
} bt;

template<class T>
IL void qm (T &x) {
  x += (x >> 31) & P;
}

template<class T>
IL T qpow (T b, unsigned k = P - 2) {
  T r = 1;
  for (; k; k >>= 1, b = bt.rem(u64(b) * b)) if (k & 1) r = bt.rem(u64(r) * b);
  return r;
}

struct mint {
  int v;
  
  mint (int _v = 0) : v(_v) {}

  mint inv () const { return qpow(v, P - 2); }
#define DEF(o, e) \
  mint& operator o##= (const mint &rhs) { e; return *this; } \
  mint operator o (const mint &rhs) const { return mint(*this) o##= rhs; }
  DEF(+, qm(v += rhs.v - P))
  DEF(-, qm(v -= rhs.v))
  DEF(*, v = bt.rem((u64)v * rhs.v))
  DEF(/, *this *= rhs.inv())
#undef DEF
  mint pow (unsigned k) const {
    int r = 1;
    u64 b = v;
    for (; k; k >>= 1, b = b * b % P) if (k & 1) r = r * b % P;
    return r;
  }
};

mint fc[N], ifc[N], iv[N], C[N][N], pq[N], pw[N][N];

mint A[N][N][N];

void init () {
  fc[0] = 1;
  L (i, 1, n) {
    fc[i] = fc[i - 1] * i;
  }
  ifc[n] = fc[n].inv();
  R (i, n - 1, 0) {
    ifc[i] = ifc[i + 1] * (i + 1);
  }
  iv[0] = iv[1] = 1;
  L (i, 2, n) {
    iv[i] = ifc[i] * fc[i - 1];
  }
  L (i, 0, n) {
    C[i][0] = 1;
    L (j, 1, i) {
      C[i][j] = C[i - 1][j] + C[i - 1][j - 1];
    }
  }
  pw[0][0] = 1;
  L (i, 1, n) {
    pw[i][0] = 1;
    L (j, 1, n) {
      pw[i][j] = pw[i][j - 1] * i;
    }
  }
  pq[0] = 1;
  L (i, 1, n) {
    pq[i] = pq[i - 1] * q;
  }
  L (p, 1, n) {
    auto f = A[p];
    L (i, 0, n / p) {
      f[1][i] = ifc[i].pow(p);
    }
    L (c, 2, n / p) {
      L (i, 0, n / p) {
        L (j, 0, i) {
          f[c][i] += f[c - 1][j] * f[1][i - j];
        }
      }
    }
  }
}

namespace Poly {
  mint f[N];

  void Ln (mint *g, int n) {
    L (i, 0, n) {
      f[i] = 0;
    }
    L (i, 1, n) {
      f[i] = g[i] * i;
      L (j, 1, i - 1) {
        f[i] -= f[j] * g[i - j];
      }
    }
    L (i, 0, n) {
      g[i] = f[i] * iv[i];
    }
  }

  void Exp (mint *g, int n) {
    L (i, 1, n) {
      f[i] = 0;
      g[i] *= i;
    }
    f[0] = 1;
    L (i, 1, n) {
      L (j, 1, i) {
        f[i] += f[i - j] * g[j];
      }
      f[i] *= iv[i];
    }
    L (i, 0, n) {
      g[i] = f[i];
    }
  }
}

using Poly::Ln;
using Poly::Exp;

int f[N];
mint g[N], h[N];

IL mint calc (int m) {
  if (m == 1) {
    return pq[f[1]] * pw[f[1]][f[1] - 1] * ifc[f[1]];
  }
  L (i, 1, m) {
    g[i] = 0;
  }
  g[0] = 1;
  L (p, 1, m - 1) {
    if (!f[p]) {
      continue;
    }
    L (i, 0, m) {
      h[i] = 0;
    }
    L (i, 0, m) {
      if (g[i].v) {
        L (j, 0, (m - i) / p) {
          h[i + j * p] += g[i] * A[p][f[p]][j];
        }
      }
    }
    L (i, 0, m) {
      g[i] = h[i];
    }
  }
  g[0] -= 1;
  L (i, 1, m) {
    g[i] *= q;
  }
  g[0] += 1;
  Ln(g, m);
  mint x = g[m], s = 1, ret = 0;
  L (k, 1, f[m] - 1) {
    s *= x;
    ret += s * pw[f[m]][f[m] - k - 1] * k * C[f[m]][k] * pq[f[m] - k];
  }
  ret += s * x;
  return ret * ifc[f[m]];
}

mint ns[N];

int a[N], tot, all;

void dfs (int i,  mint cur) {
  if (tot > 1 && a[tot] ^ a[tot - 1]) {
    cur *= calc(a[tot - 1]);
  }
  ns[all] += cur * calc(a[tot]);
  L (j, i, n - all) {
    ++f[a[++tot] = j];
    all += j;
    dfs(j, cur);
    all -= j;
    --f[a[tot--]];
  }
}

int main () {
  // FIO("1");
  ios::sync_with_stdio(0), cin.tie(0);
  cin >> n >> q >> P;
  int nn = n;
  n = min(n, 57);
  bt = barrett(P);
  init();
  f[1] = a[tot = 1] = all = 1;
  dfs(1, 1);
  L (i, 1, n) {
    ns[i] *= fc[i - 1];
    cout << ns[i].v << '\n';
  }
  L (i, n + 1, nn) {
    cout << "0\n";
  }
  cerr << clock() << '\n';
}
// I love WHQ!

詳細信息

Subtask #1:

score: 77
Acceptable Answer

Test #1:

score: 77
Acceptable Answer
time: 3270ms
memory: 8988kb

input:

100 910342260 935929297

output:

910342260
816177711
569226551
514707635
267406725
391906453
250727611
208481307
81485772
23235693
216730633
285646992
175230876
274553119
174038157
203318484
775234565
322891510
933522659
900692754
745314049
700055439
779013783
855717291
855228480
586396378
894281940
384312444
837857031
272136268
26...

result:

points 0.770 You got 77 pts!

Test #2:

score: 77
Acceptable Answer
time: 3220ms
memory: 8936kb

input:

100 222959056 947643239

output:

222959056
358599927
365062242
287299555
872152310
785181552
689517811
751458049
373969559
887125628
238000283
265869067
862846962
717459206
118380127
903859172
38731072
220551290
311944377
678478487
757437607
696077670
937732236
530238679
704937150
7448691
641846446
371506084
393996598
847615147
228...

result:

points 0.770 You got 77 pts!

Test #3:

score: 77
Acceptable Answer
time: 3223ms
memory: 8828kb

input:

100 135352674 235854343

output:

135352674
116843515
129198122
128256418
202034449
101078108
134511179
26177395
38146936
177689345
171471260
220203615
2725266
54489245
202150371
51581049
9159057
174134120
214954721
6858381
164936156
136507834
11983036
56210425
230637079
37588391
129846550
182944624
160550968
143284554
172157415
229...

result:

points 0.770 You got 77 pts!

Test #4:

score: 77
Acceptable Answer
time: 3227ms
memory: 8832kb

input:

100 538608644 566215339

output:

538608644
365236991
134179965
39370099
416828003
17910602
226317362
529379896
407121368
81806097
249408176
336758120
296361261
35236747
429449088
328368699
409154256
418665686
24463075
203118458
352974481
3351773
506522141
61405204
248921056
351694297
485859431
419342548
150905111
157365902
53232656...

result:

points 0.770 You got 77 pts!

Test #5:

score: 77
Acceptable Answer
time: 3209ms
memory: 8836kb

input:

100 56831820 281897771

output:

56831820
213573518
5338712
114481529
104176011
222091299
258318286
168492731
248042852
279768543
163273831
250332871
125456436
55441194
94771937
85241933
265069860
227132810
189427807
26222782
184487649
201740742
267160664
98981147
101908728
84191074
210184730
48919201
18122051
176229976
226118070
1...

result:

points 0.770 You got 77 pts!