QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#167013 | #7068. Lucky Sequence | ucup-team004 | AC ✓ | 1228ms | 15896kb | C++20 | 15.3kb | 2023-09-06 22:51:10 | 2023-09-06 22:51:10 |
Judging History
answer
#include <bits/stdc++.h>
using i64 = long long;
template<class T>
constexpr T power(T a, i64 b) {
T res = 1;
for (; b; b /= 2, a *= a) {
if (b % 2) {
res *= a;
}
}
return res;
}
template<int P>
struct MInt {
int x;
constexpr MInt() : x{} {}
constexpr MInt(i64 x) : x{norm(x % getMod())} {}
static int Mod;
constexpr static int getMod() {
if (P > 0) {
return P;
} else {
return Mod;
}
}
constexpr static void setMod(int Mod_) {
Mod = Mod_;
}
constexpr int norm(int x) const {
if (x < 0) {
x += getMod();
}
if (x >= getMod()) {
x -= getMod();
}
return x;
}
constexpr int val() const {
return x;
}
explicit constexpr operator int() const {
return x;
}
constexpr MInt operator-() const {
MInt res;
res.x = norm(getMod() - x);
return res;
}
constexpr MInt inv() const {
assert(x != 0);
return power(*this, getMod() - 2);
}
constexpr MInt &operator*=(MInt rhs) & {
x = 1LL * x * rhs.x % getMod();
return *this;
}
constexpr MInt &operator+=(MInt rhs) & {
x = norm(x + rhs.x);
return *this;
}
constexpr MInt &operator-=(MInt rhs) & {
x = norm(x - rhs.x);
return *this;
}
constexpr MInt &operator/=(MInt rhs) & {
return *this *= rhs.inv();
}
friend constexpr MInt operator*(MInt lhs, MInt rhs) {
MInt res = lhs;
res *= rhs;
return res;
}
friend constexpr MInt operator+(MInt lhs, MInt rhs) {
MInt res = lhs;
res += rhs;
return res;
}
friend constexpr MInt operator-(MInt lhs, MInt rhs) {
MInt res = lhs;
res -= rhs;
return res;
}
friend constexpr MInt operator/(MInt lhs, MInt rhs) {
MInt res = lhs;
res /= rhs;
return res;
}
friend constexpr std::istream &operator>>(std::istream &is, MInt &a) {
i64 v;
is >> v;
a = MInt(v);
return is;
}
friend constexpr std::ostream &operator<<(std::ostream &os, const MInt &a) {
return os << a.val();
}
friend constexpr bool operator==(MInt lhs, MInt rhs) {
return lhs.val() == rhs.val();
}
friend constexpr bool operator!=(MInt lhs, MInt rhs) {
return lhs.val() != rhs.val();
}
};
template<>
int MInt<0>::Mod = 1;
template<int V, int P>
constexpr MInt<P> CInv = MInt<P>(V).inv();
constexpr int P = 998244353;
using Z = MInt<P>;
std::vector<int> rev;
template<int P>
std::vector<MInt<P>> roots{0, 1};
template<int P>
constexpr MInt<P> findPrimitiveRoot() {
MInt<P> i = 2;
int k = __builtin_ctz(P - 1);
while (true) {
if (power(i, (P - 1) / 2) != 1) {
break;
}
i += 1;
}
return power(i, (P - 1) >> k);
}
template<int P>
constexpr MInt<P> primitiveRoot = findPrimitiveRoot<P>();
template<>
constexpr MInt<998244353> primitiveRoot<998244353> {31};
template<int P>
constexpr void dft(std::vector<MInt<P>> &a) {
int n = a.size();
if (int(rev.size()) != n) {
int k = __builtin_ctz(n) - 1;
rev.resize(n);
for (int i = 0; i < n; i++) {
rev[i] = rev[i >> 1] >> 1 | (i & 1) << k;
}
}
for (int i = 0; i < n; i++) {
if (rev[i] < i) {
std::swap(a[i], a[rev[i]]);
}
}
if (roots<P>.size() < n) {
int k = __builtin_ctz(roots<P>.size());
roots<P>.resize(n);
while ((1 << k) < n) {
auto e = power(primitiveRoot<P>, 1 << (__builtin_ctz(P - 1) - k - 1));
for (int i = 1 << (k - 1); i < (1 << k); i++) {
roots<P>[2 * i] = roots<P>[i];
roots<P>[2 * i + 1] = roots<P>[i] * e;
}
k++;
}
}
for (int k = 1; k < n; k *= 2) {
for (int i = 0; i < n; i += 2 * k) {
for (int j = 0; j < k; j++) {
MInt<P> u = a[i + j];
MInt<P> v = a[i + j + k] * roots<P>[k + j];
a[i + j] = u + v;
a[i + j + k] = u - v;
}
}
}
}
template<int P>
constexpr void idft(std::vector<MInt<P>> &a) {
int n = a.size();
std::reverse(a.begin() + 1, a.end());
dft(a);
MInt<P> inv = (1 - P) / n;
for (int i = 0; i < n; i++) {
a[i] *= inv;
}
}
template<int P = 998244353>
struct Poly : public std::vector<MInt<P>> {
using Value = MInt<P>;
Poly() : std::vector<Value>() {}
explicit constexpr Poly(int n) : std::vector<Value>(n) {}
explicit constexpr Poly(const std::vector<Value> &a) : std::vector<Value>(a) {}
constexpr Poly(const std::initializer_list<Value> &a) : std::vector<Value>(a) {}
template<class InputIt, class = std::_RequireInputIter<InputIt>>
explicit constexpr Poly(InputIt first, InputIt last) : std::vector<Value>(first, last) {}
template<class F>
explicit constexpr Poly(int n, F f) : std::vector<Value>(n) {
for (int i = 0; i < n; i++) {
(*this)[i] = f(i);
}
}
constexpr Poly shift(int k) const {
if (k >= 0) {
auto b = *this;
b.insert(b.begin(), k, 0);
return b;
} else if (this->size() <= -k) {
return Poly();
} else {
return Poly(this->begin() + (-k), this->end());
}
}
constexpr Poly trunc(int k) const {
Poly f = *this;
f.resize(k);
return f;
}
constexpr friend Poly operator+(const Poly &a, const Poly &b) {
Poly res(std::max(a.size(), b.size()));
for (int i = 0; i < a.size(); i++) {
res[i] += a[i];
}
for (int i = 0; i < b.size(); i++) {
res[i] += b[i];
}
return res;
}
constexpr friend Poly operator-(const Poly &a, const Poly &b) {
Poly res(std::max(a.size(), b.size()));
for (int i = 0; i < a.size(); i++) {
res[i] += a[i];
}
for (int i = 0; i < b.size(); i++) {
res[i] -= b[i];
}
return res;
}
constexpr friend Poly operator-(const Poly &a) {
std::vector<Value> res(a.size());
for (int i = 0; i < int(res.size()); i++) {
res[i] = -a[i];
}
return Poly(res);
}
constexpr friend Poly operator*(Poly a, Poly b) {
if (a.size() == 0 || b.size() == 0) {
return Poly();
}
if (a.size() < b.size()) {
std::swap(a, b);
}
int n = 1, tot = a.size() + b.size() - 1;
while (n < tot) {
n *= 2;
}
if (((P - 1) & (n - 1)) != 0 || b.size() < 128) {
Poly c(a.size() + b.size() - 1);
for (int i = 0; i < a.size(); i++) {
for (int j = 0; j < b.size(); j++) {
c[i + j] += a[i] * b[j];
}
}
return c;
}
a.resize(n);
b.resize(n);
dft(a);
dft(b);
for (int i = 0; i < n; ++i) {
a[i] *= b[i];
}
idft(a);
a.resize(tot);
return a;
}
constexpr friend Poly operator*(Value a, Poly b) {
for (int i = 0; i < int(b.size()); i++) {
b[i] *= a;
}
return b;
}
constexpr friend Poly operator*(Poly a, Value b) {
for (int i = 0; i < int(a.size()); i++) {
a[i] *= b;
}
return a;
}
constexpr friend Poly operator/(Poly a, Value b) {
for (int i = 0; i < int(a.size()); i++) {
a[i] /= b;
}
return a;
}
constexpr Poly &operator+=(Poly b) {
return (*this) = (*this) + b;
}
constexpr Poly &operator-=(Poly b) {
return (*this) = (*this) - b;
}
constexpr Poly &operator*=(Poly b) {
return (*this) = (*this) * b;
}
constexpr Poly &operator*=(Value b) {
return (*this) = (*this) * b;
}
constexpr Poly &operator/=(Value b) {
return (*this) = (*this) / b;
}
constexpr Poly deriv() const {
if (this->empty()) {
return Poly();
}
Poly res(this->size() - 1);
for (int i = 0; i < this->size() - 1; ++i) {
res[i] = (i + 1) * (*this)[i + 1];
}
return res;
}
constexpr Poly integr() const {
Poly res(this->size() + 1);
for (int i = 0; i < this->size(); ++i) {
res[i + 1] = (*this)[i] / (i + 1);
}
return res;
}
constexpr Poly inv(int m) const {
Poly x{(*this)[0].inv()};
int k = 1;
while (k < m) {
k *= 2;
x = (x * (Poly{2} - trunc(k) * x)).trunc(k);
}
return x.trunc(m);
}
constexpr Poly log(int m) const {
return (deriv() * inv(m)).integr().trunc(m);
}
constexpr Poly exp(int m) const {
Poly x{1};
int k = 1;
while (k < m) {
k *= 2;
x = (x * (Poly{1} - x.log(k) + trunc(k))).trunc(k);
}
return x.trunc(m);
}
constexpr Poly pow(int k, int m) const {
int i = 0;
while (i < this->size() && (*this)[i] == 0) {
i++;
}
if (i == this->size() || 1LL * i * k >= m) {
return Poly(m);
}
Value v = (*this)[i];
auto f = shift(-i) * v.inv();
return (f.log(m - i * k) * k).exp(m - i * k).shift(i * k) * power(v, k);
}
constexpr Poly sqrt(int m) const {
Poly x{1};
int k = 1;
while (k < m) {
k *= 2;
x = (x + (trunc(k) * x.inv(k)).trunc(k)) * CInv<2, P>;
}
return x.trunc(m);
}
constexpr Poly mulT(Poly b) const {
if (b.size() == 0) {
return Poly();
}
int n = b.size();
std::reverse(b.begin(), b.end());
return ((*this) * b).shift(-(n - 1));
}
constexpr std::vector<Value> eval(std::vector<Value> x) const {
if (this->size() == 0) {
return std::vector<Value>(x.size(), 0);
}
const int n = std::max(x.size(), this->size());
std::vector<Poly> q(4 * n);
std::vector<Value> ans(x.size());
x.resize(n);
std::function<void(int, int, int)> build = [&](int p, int l, int r) {
if (r - l == 1) {
q[p] = Poly{1, -x[l]};
} else {
int m = (l + r) / 2;
build(2 * p, l, m);
build(2 * p + 1, m, r);
q[p] = q[2 * p] * q[2 * p + 1];
}
};
build(1, 0, n);
std::function<void(int, int, int, const Poly &)> work = [&](int p, int l, int r, const Poly &num) {
if (r - l == 1) {
if (l < int(ans.size())) {
ans[l] = num[0];
}
} else {
int m = (l + r) / 2;
work(2 * p, l, m, num.mulT(q[2 * p + 1]).resize(m - l));
work(2 * p + 1, m, r, num.mulT(q[2 * p]).resize(r - m));
}
};
work(1, 0, n, mulT(q[1].inv(n)));
return ans;
}
};
template<int P = 998244353>
Poly<P> berlekampMassey(const Poly<P> &s) {
Poly<P> c;
Poly<P> oldC;
int f = -1;
for (int i = 0; i < s.size(); i++) {
auto delta = s[i];
for (int j = 1; j <= c.size(); j++) {
delta -= c[j - 1] * s[i - j];
}
if (delta == 0) {
continue;
}
if (f == -1) {
c.resize(i + 1);
f = i;
} else {
auto d = oldC;
d *= -1;
d.insert(d.begin(), 1);
MInt<P> df1 = 0;
for (int j = 1; j <= d.size(); j++) {
df1 += d[j - 1] * s[f + 1 - j];
}
assert(df1 != 0);
auto coef = delta / df1;
d *= coef;
Poly<P> zeros(i - f - 1);
zeros.insert(zeros.end(), d.begin(), d.end());
d = zeros;
auto temp = c;
c += d;
if (i - temp.size() > f - oldC.size()) {
oldC = temp;
f = i;
}
}
}
c *= -1;
c.insert(c.begin(), 1);
return c;
}
template<int P = 998244353>
MInt<P> linearRecurrence(Poly<P> p, Poly<P> q, i64 n) {
int m = q.size() - 1;
while (n > 0) {
auto newq = q;
for (int i = 1; i <= m; i += 2) {
newq[i] *= -1;
}
auto newp = p * newq;
newq = q * newq;
for (int i = 0; i < m; i++) {
p[i] = newp[i * 2 + n % 2];
}
for (int i = 0; i <= m; i++) {
q[i] = newq[i * 2];
}
n /= 2;
}
return p[0] / q[0];
}
struct Comb {
int n;
std::vector<Z> _fac;
std::vector<Z> _invfac;
std::vector<Z> _inv;
Comb() : n{0}, _fac{1}, _invfac{1}, _inv{0} {}
Comb(int n) : Comb() {
init(n);
}
void init(int m) {
m = std::min(m, Z::getMod() - 1);
if (m <= n) return;
_fac.resize(m + 1);
_invfac.resize(m + 1);
_inv.resize(m + 1);
for (int i = n + 1; i <= m; i++) {
_fac[i] = _fac[i - 1] * i;
}
_invfac[m] = _fac[m].inv();
for (int i = m; i > n; i--) {
_invfac[i - 1] = _invfac[i] * i;
_inv[i] = _invfac[i] * _fac[i - 1];
}
n = m;
}
Z fac(int m) {
if (m > n) init(2 * m);
return _fac[m];
}
Z invfac(int m) {
if (m > n) init(2 * m);
return _invfac[m];
}
Z inv(int m) {
if (m > n) init(2 * m);
return _inv[m];
}
Z binom(int n, int m) {
if (n < m || m < 0) return 0;
return fac(n) * invfac(m) * invfac(n - m);
}
} comb;
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
constexpr int N = 1E5;
std::vector<Z> ans(N + 1);
std::vector<Z> dp(N + 1);
dp[0] = 1;
const double phi = (std::sqrt(5) + 1) / 2;
Poly B = Poly(N + 1, [&](int i) {
return i ? comb.invfac(i) : 0;
}).exp(N + 1);
for (int i = 0; i <= N; i++) {
B[i] *= comb.fac(i);
}
auto solve = [&](auto self, int l, int r, Poly<P> p) -> Poly<P> {
p.resize(r - l + 1);
if (r - l == 1) {
int x = int(phi * l) - l + 1;
ans[l] = p[0] * x + p[1];
return {1, x};
}
int m = (l + r) / 2;
auto u = self(self, l, m, p);
auto v = self(self, m, r, (p * u).shift(-(m - l)));
return u * v;
};
solve(solve, 1, N + 1, B);
int t;
std::cin >> t;
while (t--) {
int n;
std::cin >> n;
std::cout << ans[n] << "\n";
}
return 0;
}
这程序好像有点Bug,我给组数据试试?
详细
Test #1:
score: 100
Accepted
time: 1213ms
memory: 15752kb
input:
5 1 2 3 4 5
output:
2 7 27 141 919
result:
ok 5 lines
Test #2:
score: 0
Accepted
time: 1200ms
memory: 15744kb
input:
1 100000
output:
757797613
result:
ok single line: '757797613'
Test #3:
score: 0
Accepted
time: 1210ms
memory: 15760kb
input:
1000 22568 59901 32625 77159 20665 98646 44928 96592 11443 88137 53705 90354 41583 60817 91696 66427 88639 6813 93000 13395 94112 62534 71965 21812 32169 82345 98958 16560 82042 53679 34636 20843 58907 12943 49049 68406 95730 52351 31702 42849 75524 28355 87441 3977 19409 26840 73924 75239 13883 944...
output:
512283284 732335762 162105871 244853136 93946059 351609654 670047232 235011092 817841037 663728573 178057637 545578765 823475841 179802724 754488058 21427077 368516001 755976516 332817809 895573832 392503381 281753219 807154049 697506532 558401292 682309547 498678317 420405486 355816470 941769776 77...
result:
ok 1000 lines
Test #4:
score: 0
Accepted
time: 1218ms
memory: 15808kb
input:
1000 9036 1921 59088 29263 53830 77915 67459 59238 72694 29746 32438 7567 25267 26295 39332 67958 7573 70442 27833 2712 27455 71660 79485 61606 11385 52165 96451 52784 25154 17847 23378 64935 92207 633 33324 34080 62726 79312 36738 25534 61256 83134 45722 87124 92548 29778 90065 90708 14206 55949 11...
output:
887375738 185864495 684204616 940533366 214730721 427877381 537051774 241528143 914147368 257911534 646344438 595659017 689901164 259655462 989375542 601886315 831005775 362780664 447990250 770016429 392018535 361158668 339978840 93002231 731735636 791569625 280701332 89862683 787031441 263606571 79...
result:
ok 1000 lines
Test #5:
score: 0
Accepted
time: 1215ms
memory: 15652kb
input:
1000 18753 21822 39143 6714 68147 95863 39060 58705 95632 50357 4795 79518 59901 62662 99291 38704 84028 4237 48888 42300 9765 67389 84274 18166 71932 38845 83579 79872 21597 4398 99385 72717 89530 18304 91849 75851 41785 49049 62801 15609 86223 97168 20923 65866 138 34515 60476 68228 232 17730 6453...
output:
847853379 490142143 673596645 633463716 742615413 940771896 298370451 986022532 989886624 30063041 119774969 835085555 732335762 144917760 225716898 631456583 996772590 499376995 623307593 441444421 371565727 906287178 94408606 836398569 72091648 638263600 1668580 685924314 776387400 462260232 62128...
result:
ok 1000 lines
Test #6:
score: 0
Accepted
time: 1204ms
memory: 15656kb
input:
1000 9513 14175 55601 54182 68054 33647 72330 49716 77434 83984 61145 23443 29675 81854 56051 66132 21694 31969 52809 70725 96685 73465 73760 35841 62259 68786 50070 51114 28051 24156 59943 45754 36177 64459 50052 60986 50871 33936 48815 71534 67001 7951 14017 59979 3199 30048 71211 471 93958 74982 ...
output:
248228171 617478841 724297497 536828530 837112925 567900010 953779955 766853711 779353623 744687738 169545852 681986171 540328169 325854664 499398139 421957212 468535573 62035110 623804049 17215982 986909702 1381863 287855436 730040477 487382960 491162791 417105311 303894823 726777088 198921466 8115...
result:
ok 1000 lines
Test #7:
score: 0
Accepted
time: 1223ms
memory: 15852kb
input:
1000 595 308 350 695 485 649 265 445 93 486 884 805 317 960 567 921 456 791 871 18 55 529 431 69 959 738 915 375 45 663 999 33 289 277 806 298 154 197 12 326 824 30 346 722 76 361 293 818 719 957 787 809 661 507 751 212 962 631 569 536 561 128 603 112 939 878 890 58 754 664 408 438 637 10 463 544 62...
output:
352785038 829829846 39575634 126040755 619499866 381691595 345092526 742931755 847329892 173347173 644103387 291336371 812680979 970778935 286583786 221641110 87302280 432217395 946634817 72328049 290512351 311359326 291312676 41951599 182614473 988877478 64733444 582927217 28337171 662340124 699540...
result:
ok 1000 lines
Test #8:
score: 0
Accepted
time: 1216ms
memory: 15784kb
input:
5000 3108 479 4147 4591 3406 2597 804 1866 2588 483 4617 1053 1688 3205 1701 62 221 2882 2522 737 3390 674 1342 316 3408 3629 742 2702 3196 3925 1477 668 3481 2949 4364 3980 1640 2074 866 4618 1417 194 1974 3013 4861 3433 730 4910 3962 1310 2233 1956 1796 1544 1824 2481 2106 4387 3580 2009 1104 865 ...
output:
811145023 490241791 702731360 665593153 227447720 753826493 309609946 814480975 740657570 190657935 848212018 272405848 411866278 896971116 872110711 266006587 212101945 156873600 402193881 175850816 466147152 338801852 293987585 734927745 444768928 955370403 850280395 810632080 387790567 522296108 ...
result:
ok 5000 lines
Test #9:
score: 0
Accepted
time: 1225ms
memory: 15740kb
input:
10000 4506 1218 6756 2702 8980 3053 4978 5425 752 848 1366 9354 9736 5621 7171 8691 2329 9621 549 6984 1256 2150 8848 1206 4098 7273 5851 628 7691 9884 7692 9437 269 9422 6310 738 6704 3517 7253 5823 6252 994 3179 871 6821 1972 47 8803 1451 1352 8929 6025 4306 1215 8455 3232 9232 7015 7389 1949 8612...
output:
824678440 653433658 352726779 810632080 448760348 974135082 106267916 317116073 449569904 639850813 595301743 685365656 434898268 165533795 459795155 480195116 4712339 826100316 309080081 541007291 484784260 850485526 291559803 716616440 409459581 51505304 978154883 744447406 864704215 857749367 335...
result:
ok 10000 lines
Test #10:
score: 0
Accepted
time: 1223ms
memory: 15612kb
input:
50000 30139 9357 22016 23146 17573 40473 17256 44159 10840 33027 34006 25292 2522 26127 23709 6764 10182 1973 19232 40340 23057 6887 42462 28416 46337 37357 23724 44037 32611 12567 48215 6369 35321 16233 45712 2797 16235 28244 36019 11022 17669 49532 7218 40406 25222 16749 34921 2192 46664 18305 311...
output:
330862674 251042768 174291998 127471841 583159196 859026054 640820458 555388145 642223703 581692140 122867752 304841037 402193881 580968810 8043081 896581221 517973455 645552913 618894682 406913577 156838272 630874756 156497658 582281930 311207160 957873093 809717564 97471206 82484740 816302156 7650...
result:
ok 50000 lines
Test #11:
score: 0
Accepted
time: 1222ms
memory: 15848kb
input:
100000 72992 57376 9369 55254 56984 8840 53487 9703 41014 48 35475 65765 56966 13931 95666 28913 24522 84969 13694 10049 4736 27647 10450 60680 16243 76467 63114 56999 93751 70610 43297 65252 96484 53292 89010 31184 39959 62559 49272 9533 80919 6876 83742 58553 74286 50737 34694 90400 21366 40641 41...
output:
957623588 419032934 237992356 923701323 169124718 406856879 593232837 599881909 520793745 437877846 459606271 328788319 490258904 5531643 433844669 477483744 593243118 651540063 853407719 511541684 856211513 127790699 783554402 35283007 217337906 986073774 447233357 625823082 650479956 156125060 662...
result:
ok 100000 lines
Test #12:
score: 0
Accepted
time: 1228ms
memory: 15896kb
input:
100000 93620 97547 99805 98366 90172 91659 93266 91936 98841 99388 92577 96281 95406 93126 93992 91794 92945 93167 94645 96611 93371 99002 98277 91240 94911 97886 92211 99992 95631 92571 90014 94321 98198 93857 97577 93501 94179 91790 94437 99526 95584 95273 98507 95698 99486 90991 92069 91655 95729...
output:
450853432 426002620 650006020 421489030 106862387 329032699 900706232 602846325 868949171 748526360 91132452 675572907 304908455 507361493 557032887 925867642 438112603 268890232 129209530 210219237 772617916 448424385 111636545 488121680 861240714 241815158 13253286 589892457 804265562 242801707 63...
result:
ok 100000 lines