QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#297499#5826. 错排Xiaohuba30 463ms20176kbC++237.9kb2024-01-04 16:41:482024-01-04 16:41:49

Judging History

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

  • [2024-01-04 16:41:49]
  • 评测
  • 测评结果:30
  • 用时:463ms
  • 内存:20176kb
  • [2024-01-04 16:41:48]
  • 提交

answer

// clang-format off
#include <bits/stdc++.h>

using namespace std;

#if __cplusplus < 201400
  #warning "Please use c++14 or higher."
  #define INLINE_V
  #define REGISTER_V register
  #define CPP14CONSTEXPR
  #define gcd __gcd
  #define CPP14ENABLE_IF
#elif __cplusplus < 201700
  #define INLINE_V
  #define REGISTER_V
  #define CPP14CONSTEXPR constexpr
  #define gcd __gcd
  #define CPP14ENABLE_IF ,enable_if_t<_is_integer<T>, int> = 0
#else
  #define INLINE_V inline
  #define REGISTER_V
  #define CPP14CONSTEXPR constexpr
  #define CPP14ENABLE_IF ,enable_if_t<_is_integer<T>, int> = 0
#endif

#if !defined(_WIN32) && !defined(LOCK_GETCHAR)
  #define getchar getchar_unlocked
#endif

#define il inline
#define mkp make_pair
#define fi first
#define se second
#define For(i,j,k) for(REGISTER_V int i=(j);i<=(k);++i) // NOLINT
#define ForDown(i,j,k) for(REGISTER_V int i=(j);i>=(k);--i) // NOLINT
#define pb push_back
#define eb emplace_back
#ifndef ONLINE_JUDGE
#define FileIO(filename) freopen(filename".in","r",stdin);freopen(filename".out","w",stdout)
#else
#define FileIO(filename)
#endif

using ll = long long;
// using lll = __int128_t;
using uint = unsigned int;
using ull = unsigned long long;
// using ulll = __uint128_t;
using db = double;
using ldb = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

#if __cplusplus >= 201400
  template <class T> INLINE_V constexpr bool _is_integer = numeric_limits<T>::is_integer;
  // template <> INLINE_V constexpr bool _is_integer<__int128> = true;
  // template <> INLINE_V constexpr bool _is_integer<__uint128_t> = true;
  template <> INLINE_V constexpr bool _is_integer<bool> = false;
  template <> INLINE_V constexpr bool _is_integer<char> = false;
  template <class T CPP14ENABLE_IF>
    INLINE_V constexpr T INF = numeric_limits<T>::max() >> 1;
#endif

template<typename T> constexpr il T sq(const T & x){return x*x;}
template<typename T> CPP14CONSTEXPR il void cmin(T & x, const T &y){x=min(x,y);}
template<typename T> CPP14CONSTEXPR il void cmax(T & x, const T &y){x=max(x,y);}
template<typename T> CPP14CONSTEXPR il T qpow(T x, ull y, T mod){T ans=1;x%=mod;while(y){if(y&1)(ans*=x)%=mod;(x*=x)%=mod;y>>=1;}return ans;}
template<typename T> CPP14CONSTEXPR il T qpow(T x, ull y){T ans=1;while(y){if(y&1)ans*=x;x*=x;y>>=1;}return ans;}
template<typename T CPP14ENABLE_IF> il void read(T &x){ x=0;int f=1;int c=getchar();while(!isdigit(c)){if(c=='-')f=-1;c=getchar();}while(isdigit(c)){x=x*10+c-'0';c=getchar();}x*=f;}
template<typename T, typename ... Args> il void read(T &x, Args &... y){ read(x);read(y...); }

// File head end
// clang-format on

namespace Modint {
template <const uint Mod> class Modint {
  uint _val;
  il static unordered_map<uint, uint> _inv;

public:
  Modint() : _val(0) {}
  constexpr Modint(ll val) : _val((val % Mod + Mod) % Mod) {}
  [[nodiscard]] il uint value() const { return _val; }
  [[nodiscard]] il uint safe_value() const { return (_val + Mod) % Mod; }
  [[nodiscard]] il uint inv() const {
    return _inv.count(_val) ? _inv[_val]
                            : _inv[_val] = qpow(*this, Mod - 2)._val;
  }
  Modint operator-() const { return (Mod - this->safe_value()) % Mod; }
  void operator*=(const Modint &rhs) { _val = ((ull)_val * rhs._val) % Mod; }
  void operator/=(const Modint &rhs) { _val = ((ull)_val * rhs.inv()) % Mod; }
  void operator+=(const Modint &rhs) {
    int(_val += rhs._val - (Mod << 1)) < 0 ? _val += (Mod << 1) : (ull){};
  }
  void operator-=(const Modint &rhs) {
    int(_val -= rhs._val) < 0 ? _val += (Mod << 1) : (ull){};
  }
#define setOper(x)                                                             \
  [[nodiscard]] Modint operator x(const Modint &rhs) const {                   \
    auto lhs = *this;                                                          \
    lhs x## = rhs;                                                             \
    return lhs;                                                                \
  }
  setOper(+) setOper(-) setOper(*) setOper(/)
#undef setOper
};
template <const ll Mod> istream &operator>>(istream &istr, Modint<Mod> &x) {
  ll _temp;
  istr >> _temp, x = _temp;
  return istr;
}
template <const ll Mod> ostream &operator<<(ostream &ostr, Modint<Mod> x) {
  ostr << x.value();
  return ostr;
}
} // namespace Modint

using Z = Modint::Modint<998244353>;
constexpr Z operator""_Z(ull x);

class Poly : public vector<Z> {
  il static constexpr Z G = 3;
  il static constexpr uint MAXN = 1u << 21;

  il static vector<uint> _Rev;
  il static uint cur_len = 0;
  il static bool gn_table_ok = 0;
  il static Z GN[MAXN];
  il static void init_rev(uint len) {
    cur_len = len;
    _Rev.resize(len);
    For(i, 0, len - 1) {
      _Rev[i] = _Rev[i >> 1] >> 1;
      if (i & 1)
        _Rev[i] |= len >> 1;
    }
  }
  il static void init_gn_table() {
    GN[21] = qpow(G, 998244352 / (1 << 21));
    for (uint l = MAXN >> 1, i = 20; l >= 2; l >>= 1, i--) {
      GN[i] = sq(GN[i + 1]);
    }
    gn_table_ok = 1;
  }

  il void NTT(uint len, bool tp, Poly &res) const {
    res = *this;
    res.resize(len);
    if (len != cur_len)
      init_rev(len);
    if (!gn_table_ok)
      init_gn_table();
    for (uint i = 0; i < len; i++)
      if (i < _Rev[i])
        ::swap(res[i], res[_Rev[i]]);
    for (uint l = 2, lg = 1; l <= len; l <<= 1, lg++) {
      uint m = l >> 1;
      Z gn = GN[lg];
      for (uint i = 0; i < len; i += l) {
        Z g = 1;
        for (uint j = 0; j < m; j++, g *= gn) {
          Z tmp = res[i + j + m] * g;
          res[i + j + m] = res[i + j] - tmp;
          res[i + j] += tmp;
        }
      }
    }
    if (!tp) {
      reverse(res.begin() + 1, res.begin() + len);
      auto inv = Z(len).inv();
      For(i, 0, len - 1) res[i] *= inv;
    }
  }

public:
  Poly() = default;
  Poly(int sz) { this->clear(), this->resize(sz); }
  void operator+=(const Poly &rhs) {
    int n_sz = max(this->size(), rhs.size()), rhs_sz = rhs.size(); // NOLINT
    this->resize(n_sz);
    For(i, 0, rhs_sz - 1) this->operator[](i) += rhs[i];
  }
  void operator-=(const Poly &rhs) {
    int n_sz = max(this->size(), rhs.size()), rhs_sz = rhs.size(); // NOLINT
    this->resize(n_sz);
    For(i, 0, rhs_sz - 1) operator[](i) -= rhs[i];
  }
  void operator*=(const Poly &rhs) {
    int N = 1, NN = this->size() + rhs.size() - 1; // NOLINT
    while (N < NN)
      N <<= 1;
    Poly x, y, ans{N};
    this->NTT(N, 1, x), rhs.NTT(N, 1, y);
    For(i, 0, N - 1) ans[i] = x[i] * y[i];
    ans.NTT(N, 0, *this);
    resize(NN);
  }
#define setOper(x)                                                             \
  [[nodiscard]] Poly operator x(const Poly &rhs) const {                       \
    auto lhs = *this;                                                          \
    lhs x## = rhs;                                                             \
    return lhs;                                                                \
  }
  setOper(+) setOper(-) setOper(*)
#undef setOper
};

namespace {
constexpr ll MAXN = 2e5 + 5;
int T, n, m;
Z f[MAXN], finv[MAXN];
il Z C(ll x, ll y) {
  if (x < 0 || y < 0 || x < y)
    return 0;
  return f[x] * finv[y] * finv[x - y];
}
il Z A(ll x, ll y) {
  if (x < 0 || y < 0 || x < y)
    return 0;
  return f[x] * finv[x - y];
}

il void solver_main() {
  n = MAXN - 5, f[0] = 1;
  For(i, 1, n) f[i] = f[i - 1] * i;
  finv[n] = f[n].inv();
  ForDown(i, n, 1) finv[i - 1] = finv[i] * i;

  read(T);
  while (T--) {
    read(n, m);
    Poly g(m + 1), h(n - m + 1);
    For(i, 0, m) g[i] = C(m, i) * A(m, i);
    For(i, 0, n - m) h[i] = C(n - m, i);
    // cerr << "OK\n";
    auto res = g * h;
    Z ans{};
    For(i, 0, n) { ans += res[i] * f[n - i] * ((i & 1) ? 998244352 : 1); }
    cout << ans.safe_value() << endl;
  }
}
} // namespace

signed main() { return solver_main(), 0; }

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 1
Accepted

Test #1:

score: 1
Accepted
time: 0ms
memory: 13256kb

input:

0

output:


result:

ok 0 number(s): ""

Subtask #2:

score: 9
Accepted

Test #2:

score: 9
Accepted
time: 0ms
memory: 13328kb

input:

10
8 6
5 1
4 2
6 3
8 1
3 1
6 2
3 1
4 1
6 2

output:

0
44
4
36
14833
2
168
2
9
168

result:

ok 10 numbers

Test #3:

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

input:

10
8 1
8 4
6 3
8 2
8 3
6 3
6 1
7 3
2 1
8 3

output:

14833
576
36
10860
4680
36
265
432
1
4680

result:

ok 10 numbers

Test #4:

score: 0
Accepted
time: 2ms
memory: 13260kb

input:

10
7 5
3 1
8 3
7 3
8 1
4 1
5 2
6 3
7 1
7 3

output:

0
2
4680
432
14833
9
24
36
1854
432

result:

ok 10 numbers

Test #5:

score: 0
Accepted
time: 0ms
memory: 13256kb

input:

10
7 2
8 4
6 1
5 1
8 2
6 3
4 2
8 3
3 1
8 1

output:

1280
576
265
44
10860
36
4
4680
2
14833

result:

ok 10 numbers

Test #6:

score: 0
Accepted
time: 0ms
memory: 13208kb

input:

10
6 6
3 1
8 3
2 1
7 1
3 1
6 2
8 4
7 3
7 0

output:

0
2
4680
1
1854
2
168
576
432
1854

result:

ok 10 numbers

Subtask #3:

score: 0
Time Limit Exceeded

Test #7:

score: 0
Time Limit Exceeded

input:

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

output:

0
1
2
9
44
265
1854
14833
133496
1334961
14684570
176214841
294304226
127281753
910981941
600290115
222488424
11814221
224470198
496426549
442513998
751108780
305347938
340640042
530046225
804025262
745550660
910531421
451058030
554564312
221339670
95158970
145512950
954462889
464137465
737039093
31...

result:


Subtask #4:

score: 0
Time Limit Exceeded

Test #8:

score: 0
Time Limit Exceeded

input:

200000
4303 1473
1276 72
967 234
3619 984
1316 384
2679 50
4426 1744
3782 1179
4919 4
805 63
3933 158
1574 528
1277 435
3826 915
2739 68
2286 349
3017 527
3036 476
4280 1764
1504 686
4584 917
1379 145
4764 2178
1881 45
4808 1565
3663 165
4730 2209
2258 103
4181 1687
1636 770
4339 1173
2355 777
3201 ...

output:

855518783
202627962
284771116
596280162
111952425
28114068
922980998
483503998
478475869
42227903
210453242
82826277
349706660
478397018
588903665
672339856
911511930
783922264
224272260
199537336
659467844
383745708
953695418
668329703
880293299
649430530
916687905
550953325
295023552
141584429
871...

result:


Subtask #5:

score: 20
Accepted

Dependency #1:

100%
Accepted

Dependency #2:

100%
Accepted

Test #13:

score: 20
Accepted
time: 284ms
memory: 19580kb

input:

10
94764 1149
111140 21372
59140 20928
73376 27175
59837 4344
160865 25705
44518 10326
145794 64106
147628 12887
103719 39458

output:

139176963
393241499
258873190
39229362
870875380
975228452
243360193
751148936
95574458
297629235

result:

ok 10 numbers

Test #14:

score: 0
Accepted
time: 354ms
memory: 19772kb

input:

10
158002 80444
9451 2903
173427 12416
137154 16538
166581 24311
127365 41216
190696 67064
103832 40293
108767 52320
109966 50541

output:

0
702735124
750025710
841222658
375040035
583566228
649803213
746573713
113561055
257165994

result:

ok 10 numbers

Test #15:

score: 0
Accepted
time: 358ms
memory: 20176kb

input:

10
116222 34542
164940 69028
129899 24339
178863 5716
55643 22577
198734 88929
133306 14275
81416 35799
63999 8310
161489 76991

output:

290743004
378341801
539453217
748305238
182916741
547729744
354427924
684683519
325465086
252320707

result:

ok 10 numbers

Test #16:

score: 0
Accepted
time: 353ms
memory: 19572kb

input:

10
195516 163316
189541 26309
135594 44847
135877 65724
130088 25449
43733 28
99690 16935
64787 30191
71441 7633
149688 65415

output:

0
913189292
577808221
197261625
42734325
96320751
700077354
534988269
607854165
915606441

result:

ok 10 numbers

Test #17:

score: 0
Accepted
time: 327ms
memory: 19884kb

input:

10
163009 107578
44973 3654
143223 16359
94260 46849
30253 4092
157914 12798
96468 10458
69392 15738
175738 45794
88177 40742

output:

0
3532506
560207666
853291572
71222878
859859971
982136558
976170384
963781875
378642896

result:

ok 10 numbers

Test #18:

score: 0
Accepted
time: 382ms
memory: 20108kb

input:

10
110588 39549
80716 17407
111961 32201
141172 69526
113156 9733
197619 33476
175704 25422
193136 84984
121758 34704
186415 47390

output:

365752740
457468561
833768060
154200192
143999958
497510358
149082994
737572231
779740562
243498597

result:

ok 10 numbers

Test #19:

score: 0
Accepted
time: 460ms
memory: 19968kb

input:

10
135555 22650
164492 12323
142156 56953
147138 39540
198672 13148
113977 32999
140219 50353
70617 28677
163824 34306
189792 79678

output:

607236911
240877911
474627738
535731583
760041723
433248274
681491153
764919295
435671865
90602437

result:

ok 10 numbers

Test #20:

score: 0
Accepted
time: 463ms
memory: 19860kb

input:

10
170490 75332
171298 50434
192270 14874
169646 26194
148215 47449
184554 54250
89981 14289
155225 40485
117440 37074
176617 25601

output:

674081939
124446215
364039988
511947341
952033804
203192142
723185949
644885639
266757734
667462943

result:

ok 10 numbers

Subtask #6:

score: 0
Skipped

Dependency #1:

100%
Accepted

Dependency #2:

100%
Accepted

Dependency #3:

0%