QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#235437 | #7647. 树哈希 | hos_lyric# | 43 | 0ms | 3912kb | C++14 | 12.4kb | 2023-11-02 19:22:40 | 2024-07-04 02:22:47 |
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")
#ifndef LIBRA_OTHER_INT128_H_
#define LIBRA_OTHER_INT128_H_
#include <stdio.h>
#include <iostream>
constexpr unsigned __int128 toUInt128(const char *s) {
unsigned __int128 x = 0;
for (; *s; ++s) x = x * 10 + (*s - '0');
return x;
}
constexpr __int128 toInt128(const char *s) {
if (*s == '-') return -toInt128(s + 1);
__int128 x = 0;
for (; *s; ++s) x = x * 10 + (*s - '0');
return x;
}
unsigned __int128 inUInt128() {
static char buf[41];
scanf("%s", buf);
return toUInt128(buf);
}
__int128 inInt128() {
static char buf[41];
scanf("%s", buf);
return toInt128(buf);
}
void out(unsigned __int128 x) {
static char buf[41];
int len = 0;
do { buf[len++] = '0' + static_cast<int>(x % 10); } while (x /= 10);
for (int i = len; --i >= 0; ) putchar(buf[i]);
}
void out(__int128 x) {
if (x < 0) {
putchar('-');
out(-static_cast<unsigned __int128>(x));
} else {
out(static_cast<unsigned __int128>(x));
}
}
std::ostream &operator<<(std::ostream &os, unsigned __int128 x) {
static char buf[41];
int len = 0;
do { buf[len++] = '0' + static_cast<int>(x % 10); } while (x /= 10);
for (int i = len; --i >= 0; ) os << buf[i];
return os;
}
std::ostream &operator<<(std::ostream &os, __int128 x) {
if (x < 0) {
os << '-' << -static_cast<unsigned __int128>(x);
} else {
os << static_cast<unsigned __int128>(x);
}
return os;
}
#endif // LIBRA_OTHER_INT128_H_
////////////////////////////////////////////////////////////////////////////////
// Barrett
struct ModInt {
static unsigned M;
static unsigned long long NEG_INV_M;
static void setM(unsigned m) { M = m; NEG_INV_M = -1ULL / M; }
unsigned x;
ModInt() : x(0U) {}
ModInt(unsigned x_) : x(x_ % M) {}
ModInt(unsigned long long x_) : x(x_ % M) {}
ModInt(int x_) : x(((x_ %= static_cast<int>(M)) < 0) ? (x_ + static_cast<int>(M)) : x_) {}
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) {
const unsigned long long y = static_cast<unsigned long long>(x) * a.x;
const unsigned long long q = static_cast<unsigned long long>((static_cast<unsigned __int128>(NEG_INV_M) * y) >> 64);
const unsigned long long r = y - M * q;
x = r - M * (r >= 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; }
};
unsigned ModInt::M;
unsigned long long ModInt::NEG_INV_M;
// !!!Use ModInt::setM!!!
////////////////////////////////////////////////////////////////////////////////
using Mint = ModInt;
using U = unsigned __int128;
constexpr int L = 23;
U fac[L + 1];
U tab[L + 1][L + 1];
int pt[L + 2];
vector<int> szs;
vector<vector<int>> trees;
void dfs(int n, int pos, int rem, int last, vector<int> &now) {
if (rem == 0) {
szs.push_back(n);
trees.push_back(vector<int>(now.begin(), now.begin() + pos));
} else {
for (int x = min(pt[rem + 1] - 1, last); x >= 0; --x) {
now[pos] = x;
dfs(n, pos + 1, rem - szs[x], x, now);
}
}
}
bitset<500'000'000> on;
int add(int x) {
int ret = 0;
if (!on[x]) ++ret;
on.set(x);
for (const int y : trees[x]) {
ret += add(y);
}
return ret;
}
void rem(int x) {
on.reset(x);
for (const int y : trees[x]) {
rem(y);
}
}
void run() {
fac[0] = 1;
for (int i = 1; i <= L; ++i) {
fac[i] = fac[i - 1] * i;
}
for (int n = 1; n <= L; ++n) {
vector<int> now(n - 1);
dfs(n, 0, n - 1, pt[n] - 1, now);
pt[n + 1] = trees.size();
cerr<<"GENED n = "<<n<<endl;
}
for(int n=1;n<=L;++n)cerr<<pt[n+1]-pt[n]<<" ";cerr<<endl;
cerr<<"pt = ";pv(pt,pt+L+2);
cerr<<"szs = "<<szs<<endl;
cerr<<"trees = "<<trees<<endl;
const int len = trees.size();
vector<U> isom(len, 1);
for (int x = 0; x < len; ++x) {
const int deg = trees[x].size();
for (int j = 0, k = 0; j < deg; j = k) {
for (; k < deg && trees[x][j] == trees[x][k]; ++k) {}
isom[x] *= fac[k - j];
}
for (const int y : trees[x]) {
isom[x] *= isom[y];
}
}
memset(tab, 0, sizeof(tab));
for (int n = 1; n <= L; ++n) {
for (int x = pt[n]; x < pt[n + 1]; ++x) {
const int res = add(x);
rem(x);
tab[n][res] += fac[n - 1] / isom[x];
}
printf("{");
for (int i = 0; i <= n; ++i) {
printf("\"");
out(tab[n][i]);
printf("\",");
}
printf("},\n");
fflush(stdout);
}
}
constexpr char TAB[L + 1][L + 1][40] = {{},
{"0","1",},
{"0","0","1",},
{"0","0","1","2",},
{"0","0","1","9","6",},
{"0","0","1","40","60","24",},
{"0","0","1","135","620","420","120",},
{"0","0","1","576","4950","7320","3240","720",},
{"0","0","1","2331","38262","104790","84000","27720","5040",},
{"0","0","1","9976","329056","1337616","1809360","994560","262080","40320",},
{"0","0","1","49167","2585088","17861760","34401024","29620080","12398400","2721600","362880",},
{"0","0","1","261520","23298570","230333760","650991600","770091840","484596000","163900800","30844800","3628800",},
{"0","0","1","1264923","201666410","3048704010","11992068000","19491650640","16352915040","8108100000","2301868800","379209600","39916800",},
{"0","0","1","7499196","2029955400","40907450760","221673303720","476230803840","528497101440","342689719680","140287593600","34328448000","5029516800","479001600",},
{"0","0","1","48145851","18853513692","569381450352","4090163288640","11572348427640","16380248639040","13684449018240","7234033847040","2523759638400","542788646400","71610739200","6227020800",},
{"0","0","1","282624902","208545510264","8152163189352","77086057904856","278283437271840","500100673952880","520865364539520","348317835465600","155595814133760","47326914835200","9081072000000","1089728640000","87178291200",},
{"0","0","1","1816866915","2162998664400","120347621661540","1468956736840320","6748418405088360","15050302055989200","19370055105601200","15895265315529600","8864170595280000","3432547140422400","925996911840000","160408055808000","17653603968000","1307674368000",},
{"0","0","1","13547611072","26305324897680","1848599474688960","28794041030008800","164309578733664000","452558599710497280","707131804060281600","703619397050169600","476068505552640000","227886842902118400","77965463742566400","18904612446720000","2984984690688000","303380453376000","20922789888000",},
{"0","0","1","95657990591","296154537531504","29061239330523120","574300481045592000","4063118701715892000","13596933593369468160","25639747059038112000","30447531987666393600","24644556812542867200","14192444692384358400","5957501607119462400","1827273024357580800","402504785819136000","58392019445760000","5513155135488000","355687428096000",},
{"0","0","1","728122042512","3992150752821090","485883201817925856","11765951504681397120","101850607783224760320","411387714895093372800","924886619869925905920","1302381750288843544320","1241872782824440166400","847186002528696230400","425046860209017446400","159037105867037798400","44240722425265766400","8930511022777344000","1198310945255424000","105639166144512000","6402373705728000",},
{"0","0","1","5844862093851","49629324211196850","8133726351536089362","247590273863499582816","2594284833364382851200","12553781400629160148800","33409626709338411517440","55227445993192438667520","61602037034137852412160","49023018235978449062400","28922067190973897740800","12867542548242897715200","4347066225570292224000","1107072798346548633600","206284747564127232000","25748212919869440000","2128789257154560000","121645100408832000",},
{"0","0","1","45722313018340","731491249474061560","146412283120585358040","5351387318240052546360","67408256560800561799680","387067548577886172576000","1211702321528249181523200","2334991013551935841939200","3019924443314785123353600","2781722291541984203212800","1900162368982181880576000","988504608959262210816000","395427474182225092608000","121871529247120773120000","28635074168588439552000","4955517277904793600000","578219710609981440000","45008687151267840000","2432902008176640000",},
{"0","0","1","409849573362075","9980697826981109340","2643243313029276118560","118749611238270471406080","1783810597926000799670760","12094459277563079465045760","44216858973432482473248000","98756700763495529324908800","147085554484381511430432000","155550588024711605281920000","121975403788232366058086400","73045805344421036295168000","34015932206018981906688000","12371199207720728094720000","3508094687107727388672000","765421504692573560832000","123672011894394163200000","13547614832531619840000","996273372348334080000","51090942171709440000",},
{"0","0","1","3905424441436210","157030294150775211012","51157209382886943733320","2715323184716504538524040","48251942280453907180009440","383106358874012690822708880","1627538131680709191848593920","4188102212468367351585504000","7143777101967256790601408000","8615591548064982329232384000","7695284239431545114963404800","5255839969769648504044800000","2804790139728003882943488000","1183280015704811845556736000","394819430752967688179712000","103743023855753999388672000","21135672218439208894464000","3202886907165950484480000","330643547421246259200000","23042014919440957440000","1124000727777607680000",},
};
int main() {
// run(); return 0;
int N;
Mint Q;
unsigned MO;
for (; ~scanf("%d%u%u", &N, &Q.x, &MO); ) {
Mint::setM(MO);
for (int n = 1; n <= N; ++n) {
Mint ans = 0;
if (n <= L) {
for (int i = n; i >= 0; --i) {
(ans *= Q) += (unsigned)(toUInt128(TAB[n][i]) % MO);
}
}
printf("%u\n", ans.x);
}
}
return 0;
}
详细
Subtask #1:
score: 43
Acceptable Answer
Test #1:
score: 43
Acceptable Answer
time: 0ms
memory: 3852kb
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 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...
result:
points 0.430 You got 43 pts!
Test #2:
score: 43
Acceptable Answer
time: 0ms
memory: 3912kb
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 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0...
result:
points 0.430 You got 43 pts!
Test #3:
score: 43
Acceptable Answer
time: 0ms
memory: 3832kb
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 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0...
result:
points 0.430 You got 43 pts!
Test #4:
score: 43
Acceptable Answer
time: 0ms
memory: 3804kb
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 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0...
result:
points 0.430 You got 43 pts!
Test #5:
score: 43
Acceptable Answer
time: 0ms
memory: 3840kb
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 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0...
result:
points 0.430 You got 43 pts!