QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#710946#3849. Cactus cuttingahsoltanAC ✓2202ms24460kbC++205.3kb2024-11-04 23:23:342024-11-04 23:23:38

Judging History

This is the latest submission verdict.

  • [2024-11-04 23:23:38]
  • Judged
  • Verdict: AC
  • Time: 2202ms
  • Memory: 24460kb
  • [2024-11-04 23:23:34]
  • Submitted

answer

#include <bits/stdc++.h>
using namespace std;

#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define all(x) begin(x), end(x)
#define sz(x) int((x).size())
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;

#ifdef LOCAL
auto operator<<(auto& o, auto x) -> decltype(x.first, o);
auto operator<<(auto& o, auto x) -> decltype(x.end(), o) {
  o << "{";
  for (int i = 0; auto y : x) o << ", " + !i++ * 2 << y;
  return o << "}"; }
auto operator<<(auto& o, auto x) -> decltype(x.first, o) {
  return o << "(" << x.first << ", " << x.second << ")"; }
void __print(auto... x) { ((cerr << x << " "), ...) << endl; }
#define debug(x...) __print("[" #x "]:", x)
#else
#define debug(...) 2137
#endif

template<int M, int R>
struct Mod {
  static const int MOD = M, ROOT = R;
  int x;
  Mod(ll y = 0) : x(y % M) { x += (x < 0) * M; }
  Mod& operator+=(Mod o) {
    if ((x += o.x) >= M) x -= M;
    return *this; }
  Mod& operator-=(Mod o) {
    if ((x -= o.x) < 0) x += M;
    return *this; }
  Mod& operator*=(Mod o) {
    x = 1ll * x * o.x % M;
    return *this; }
  Mod& operator/=(Mod o) { return *this *= o.inv(); }
  friend Mod operator+(Mod a, Mod b) { return a += b; }
  friend Mod operator-(Mod a, Mod b) { return a -= b; }
  friend Mod operator*(Mod a, Mod b) { return a *= b; }
  friend Mod operator/(Mod a, Mod b) { return a /= b; }
  auto operator<=>(const Mod&) const = default;
  Mod pow(ll n) const {
    Mod a = x, b = 1;
    for (; n; n /= 2, a *= a) if (n & 1) b *= a;
    return b;
  }
  Mod inv() const { assert(x != 0); return pow(M - 2); }
};
using mint = Mod<1000003, -1>;

template<class T>
void ntt(vector<T>& a, bool inv) {
  int n = sz(a); vector<T> b(n);
  for (int i = n / 2; i; i /= 2, swap(a, b)) {
    T w = T(T::ROOT).pow((T::MOD - 1) / n * i), m = 1;
    for (int j = 0; j < n; j += 2 * i, m *= w) rep(k, 0, i) {
      T u = a[j + k], v = a[j + k + i] * m;
      b[j / 2 + k] = u + v, b[j / 2 + k + n / 2] = u - v;
    }
  }
  if (inv) {
    reverse(1 + all(a));
    T z = T(n).inv(); rep(i, 0, n) a[i] *= z;
  }
}
template<class T>
vector<T> conv(vector<T> a, vector<T> b) {
  int s = sz(a) + sz(b) - 1, n = 1 << __lg(2 * s - 1);
  a.resize(n); ntt(a, 0); b.resize(n); ntt(b, 0);
  rep(i, 0, n) a[i] *= b[i];
  ntt(a, 1); a.resize(s);
  return a;
}
template<class T>
vector<T> mconv(const auto& x, const auto& y) {
  auto con = [&](const auto& v) {
    vector<T> w(sz(v)); rep(i, 0, sz(v)) w[i] = v[i].x;
    return w; };
  return conv(con(x), con(y));
}
template<class T>
vector<T> conv3(const vector<T>& a, const vector<T>& b) {
  using m0 = Mod<754974721, 11>; auto c0 = mconv<m0>(a, b);
  using m1 = Mod<167772161, 3>;  auto c1 = mconv<m1>(a, b);
  using m2 = Mod<469762049, 3>;  auto c2 = mconv<m2>(a, b);
  int n = sz(c0); vector<T> d(n); m1 r01 = m1(m0::MOD).inv();
  m2 r02 = m2(m0::MOD).inv(), r12 = m2(m1::MOD).inv();
  rep(i, 0, n) {
    int x = c0[i].x, y = ((c1[i] - x) * r01).x,
        z = (((c2[i] - x) * r02 - y) * r12).x;
    d[i] = (T(z) * m1::MOD + y) * m0::MOD + x;
  }
  return d;
}

vector<mint> _f = {1}, _fi = {1};
void _init(int n) {
  int k = ssize(_f);
  if (n < k) return;
  n *= 2;
  _f.resize(n + 1);
  _fi.resize(n + 1);
  for (int i = k; i <= n; i++) _f[i] = _f[i - 1] * i;
  _fi[n] = _f[n].inv();
  for (int i = n; i > k; i--) _fi[i - 1] = _fi[i] * i;
}
mint fact(int n) {
  _init(n);
  return _f[n];
}
mint inv_fact(int n) {
  _init(n);
  return _fi[n];
}
mint choose(int n, int k) {
  if (n < k || k < 0) return 0;
  _init(n);
  return _f[n] * _fi[k] * _fi[n - k];
}

const int N = 100100;
int n, m;
int in[N], out[N], t;
vi adj[N];
mint dp[N][2][2];
int k[N];
mint pr[N];

vector<mint> rec(auto& x, int l, int r) {
  if (l + 1 == r) return x[l];
  int mid = (l + r) / 2;
  return conv3(rec(x, l, mid), rec(x, mid, r));
}

void dfs(int u, int p) {
  in[u] = t++;
  vector<vector<mint>> w;
  w.push_back({1});
  int kto = -1;
  for (int v : adj[u]) {
    if (v == p) continue;
    if (in[v] == -1) {
      dfs(v, u);
      if (k[v] != -1 && k[v] != u) {
          kto = v;
        } else {
          auto& x = w.emplace_back(3);
          rep(i, 0, 2) rep(j, 0, 2) x[i + j] += dp[v][i][j];
      }
    } else if (in[v] < in[u]) {
      k[u] = v;
    }
  }
  debug(u, kto);
  if (kto != -1) {
    k[u] = k[kto];
    rep(s, 0, 2) {
      auto& x = w.emplace_back(3);
      rep(i, 0, 2) x[i] += dp[kto][i][s];
      auto q = rec(w, 0, sz(w));
      rep(i, 0, sz(q)) {
        if (i % 2 == 0) {
          dp[u][1][s] += q[i] * pr[i];
        } else {
          dp[u][0][s] += q[i] * pr[i - 1] * i;
        }
      }
      w.pop_back();
    }
  } else {
    auto q = rec(w, 0, sz(w));
    rep(i, 0, sz(q)) rep(j, 0, 2) rep(b, 0, 2) {
      if (b && k[u] == -1) continue;
      if ((i + !j + (!b && k[u] != -1)) % 2) continue;
      dp[u][j][b] += q[i] * pr[i + !j + (!b && k[u] != -1)];
    }
  }
  rep(i, 0, 2) rep(j, 0, 2) debug(u, i, j, dp[u][i][j].x);
  debug(u, k[u]);
}

int main() {
  cin.tie(0)->sync_with_stdio(0);
  cin >> n >> m;
  rep(i, 0, m) {
    int u, v;
    cin >> u >> v;
    u--; v--;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  rep(i, 0, n) in[i] = k[i] = -1;
  pr[0] = 1;
  rep(i, 1, n + 1) {
    if (i % 2 == 0) {
      pr[i] = (i - 1) * pr[i - 2];
    }
  }
  dfs(0, -1);
  //rep(i, 0, n) rep(j, 0, 2) rep(k, 0, 2) debug(i, j, k, dp[i][j][k].x);
  cout << dp[0][1][0].x << '\n';
}

詳細信息

Test #1:

score: 100
Accepted
time: 546ms
memory: 13564kb

input:

75000 99998
1 2
1 3
1 6
1 13
1 14
1 17
1 19
1 54
1 62
1 64
1 25003
1 50002
1 25004
1 50003
1 25005
1 50004
1 25006
1 50005
1 25007
1 50006
1 25008
1 50007
1 25009
1 50008
1 25010
1 50009
1 25011
1 50010
1 25012
1 50011
1 25013
1 50012
1 25014
1 50013
1 25015
1 50014
1 25016
1 50015
1 25017
1 50016
1...

output:

224026

result:

ok single line: '224026'

Test #2:

score: 0
Accepted
time: 551ms
memory: 13324kb

input:

75000 99998
1 2
1 3
1 10
1 18
1 63
1 514
1 3552
1 4093
1 6245
1 22678
1 25003
1 50002
1 25004
1 50003
1 25005
1 50004
1 25006
1 50005
1 25007
1 50006
1 25008
1 50007
1 25009
1 50008
1 25010
1 50009
1 25011
1 50010
1 25012
1 50011
1 25013
1 50012
1 25014
1 50013
1 25015
1 50014
1 25016
1 50015
1 2501...

output:

429308

result:

ok single line: '429308'

Test #3:

score: 0
Accepted
time: 547ms
memory: 13144kb

input:

75000 99998
1 2
1 3
1 58
1 66
1 77
1 210
1 770
1 844
1 25003
1 50002
1 25004
1 50003
1 25005
1 50004
1 25006
1 50005
1 25007
1 50006
1 25008
1 50007
1 25009
1 50008
1 25010
1 50009
1 25011
1 50010
1 25012
1 50011
1 25013
1 50012
1 25014
1 50013
1 25015
1 50014
1 25016
1 50015
1 25017
1 50016
1 25018...

output:

355105

result:

ok single line: '355105'

Test #4:

score: 0
Accepted
time: 551ms
memory: 13224kb

input:

75000 99998
1 2
1 6
1 89
1 137
1 169
1 2929
1 6649
1 24127
1 25003
1 50002
1 25004
1 50003
1 25005
1 50004
1 25006
1 50005
1 25007
1 50006
1 25008
1 50007
1 25009
1 50008
1 25010
1 50009
1 25011
1 50010
1 25012
1 50011
1 25013
1 50012
1 25014
1 50013
1 25015
1 50014
1 25016
1 50015
1 25017
1 50016
1...

output:

729262

result:

ok single line: '729262'

Test #5:

score: 0
Accepted
time: 524ms
memory: 12592kb

input:

75000 91998
1 2
1 7
1 8
1 11
1 77
1 107
1 118
1 332
1 804
1 14455
1 22866
1 34680
1 41003
1 58002
1 41004
1 58003
1 41005
1 58004
1 41006
1 58005
1 41007
1 58006
1 41008
1 58007
1 41009
1 58008
1 41010
1 58009
1 41011
1 58010
1 41012
1 58011
1 41013
1 58012
1 41014
1 58013
1 41015
1 58014
1 41016
1 ...

output:

916981

result:

ok single line: '916981'

Test #6:

score: 0
Accepted
time: 521ms
memory: 12724kb

input:

75000 91998
1 2
1 3
1 4
1 8
1 27
1 3972
1 4196
1 6344
1 8151
1 17056
1 41003
1 58002
1 41004
1 58003
1 41005
1 58004
1 41006
1 58005
1 41007
1 58006
1 41008
1 58007
1 41009
1 58008
1 41010
1 58009
1 41011
1 58010
1 41012
1 58011
1 41013
1 58012
1 41014
1 58013
1 41015
1 58014
1 41016
1 58015
1 41017...

output:

537508

result:

ok single line: '537508'

Test #7:

score: 0
Accepted
time: 525ms
memory: 12840kb

input:

75000 91998
1 2
1 4
1 7
1 16
1 60
1 72
1 77
1 87
1 448
1 3609
1 4997
1 10698
1 21177
1 41003
1 58002
1 41004
1 58003
1 41005
1 58004
1 41006
1 58005
1 41007
1 58006
1 41008
1 58007
1 41009
1 58008
1 41010
1 58009
1 41011
1 58010
1 41012
1 58011
1 41013
1 58012
1 41014
1 58013
1 41015
1 58014
1 41016...

output:

549167

result:

ok single line: '549167'

Test #8:

score: 0
Accepted
time: 514ms
memory: 12564kb

input:

75000 91998
1 2
1 3
1 19
1 55
1 133
1 229
1 263
1 277
1 352
1 729
1 41003
1 58002
1 41004
1 58003
1 41005
1 58004
1 41006
1 58005
1 41007
1 58006
1 41008
1 58007
1 41009
1 58008
1 41010
1 58009
1 41011
1 58010
1 41012
1 58011
1 41013
1 58012
1 41014
1 58013
1 41015
1 58014
1 41016
1 58015
1 41017
1 ...

output:

411498

result:

ok single line: '411498'

Test #9:

score: 0
Accepted
time: 567ms
memory: 10600kb

input:

75000 84355
1 60160
1 36004
1 2727
2 37452
3 48319
3 2480
4 58521
4 17803
4 43753
5 72576
5 29500
6 66502
6 8864
6 70643
7 52161
7 57465
8 41017
9 16472
9 32077
10 216
10 67333
10 6377
11 32592
11 15405
12 32930
12 72311
13 22378
13 19539
14 73204
14 47834
14 6326
15 65328
15 29270
16 62556
17 29754...

output:

0

result:

ok single line: '0'

Test #10:

score: 0
Accepted
time: 557ms
memory: 10812kb

input:

75000 84443
1 16256
1 65404
1 53817
2 15011
3 46150
3 58264
3 64631
4 63600
4 42517
5 35410
5 2918
6 43679
6 31347
7 313
7 26818
8 19803
8 28022
9 33081
9 58179
9 49534
10 28057
10 38811
10 65628
10 7342
11 8803
11 13929
12 41494
12 35263
13 32331
13 20071
13 45194
14 22131
14 51956
15 55884
15 2706...

output:

0

result:

ok single line: '0'

Test #11:

score: 0
Accepted
time: 560ms
memory: 10604kb

input:

75000 84361
1 14054
1 45822
1 5525
2 48240
2 55109
3 49958
3 43979
3 57569
3 13554
3 68084
4 38798
5 32827
5 3992
6 74366
6 62015
6 71485
7 8019
8 50760
8 19013
9 52224
10 38701
10 8292
11 10571
11 69807
11 68176
11 45014
11 38122
12 11627
12 63666
12 63447
13 72983
14 697
14 18585
14 11520
15 67317...

output:

0

result:

ok single line: '0'

Test #12:

score: 0
Accepted
time: 558ms
memory: 10644kb

input:

75000 84446
1 12163
1 10711
2 49923
2 44701
2 8609
3 53520
3 43344
3 38631
4 51228
4 73023
4 29607
4 47035
5 59192
5 7614
5 6391
6 32925
6 7726
7 25514
7 9391
8 44630
9 68916
9 7576
9 67445
10 28334
10 27979
11 64824
12 40803
13 47302
13 21264
14 49825
14 54823
14 61784
15 2695
15 22559
16 50048
16 ...

output:

708959

result:

ok single line: '708959'

Test #13:

score: 0
Accepted
time: 573ms
memory: 11060kb

input:

80000 89652
1 66308
1 10222
2 26590
2 71306
3 66796
3 29384
4 70837
5 40447
5 47609
6 34764
7 11408
7 14944
8 314
8 74773
8 41874
8 71852
9 17383
9 67743
9 5347
10 19430
10 56609
11 417
12 76333
12 60115
13 18696
14 57507
14 74267
15 65479
15 38262
15 43748
15 33902
16 68798
16 14903
17 25416
17 514...

output:

554931

result:

ok single line: '554931'

Test #14:

score: 0
Accepted
time: 573ms
memory: 10868kb

input:

80000 89479
1 20098
1 29446
2 53025
3 77498
3 42267
3 48225
4 59800
5 49989
5 75588
6 46532
6 3921
6 65370
6 43573
7 24293
7 72322
7 41443
7 50945
8 79801
8 6185
8 14588
9 63479
9 43283
10 4729
10 72803
10 75177
11 74978
11 28556
11 8400
11 35850
12 2045
13 57480
13 25090
13 17356
14 75090
15 48767
...

output:

0

result:

ok single line: '0'

Test #15:

score: 0
Accepted
time: 571ms
memory: 10808kb

input:

80000 89541
1 37420
1 21167
2 27013
3 16643
3 7351
3 75810
4 72423
5 33283
5 24152
6 67473
6 35419
7 51075
8 64510
8 56113
9 25479
9 49232
10 79970
10 5793
11 72852
11 18034
11 53317
11 71175
12 74230
12 23826
13 30588
13 4880
13 13481
13 59718
13 48623
14 37346
15 1053
16 50470
17 78276
17 55335
17...

output:

0

result:

ok single line: '0'

Test #16:

score: 0
Accepted
time: 575ms
memory: 10804kb

input:

80000 89486
1 27427
1 14790
2 41804
2 6603
2 10074
2 26642
2 44375
3 11606
4 10048
4 48838
5 23219
6 53875
7 32447
7 10347
8 12387
8 19203
9 18548
9 64409
10 79601
11 48861
11 34818
12 50037
12 8561
12 46194
13 3983
14 5421
15 24834
15 43143
16 75566
17 26566
17 64114
17 70281
18 18296
18 43833
19 3...

output:

490881

result:

ok single line: '490881'

Test #17:

score: 0
Accepted
time: 547ms
memory: 11424kb

input:

90000 97174
1 7614
1 43646
2 71272
2 67043
3 53652
3 75364
4 77652
4 33533
5 2762
5 69727
6 33066
6 74704
6 32981
7 7827
8 32603
9 46684
9 23465
10 730
10 6472
10 8374
10 47655
11 49036
12 51500
12 2614
13 4012
13 15522
13 43567
14 52946
15 34403
15 83392
15 52725
16 44333
16 22639
16 748
17 18394
1...

output:

588018

result:

ok single line: '588018'

Test #18:

score: 0
Accepted
time: 546ms
memory: 11476kb

input:

90000 97212
1 33588
1 43881
2 32458
2 48845
3 10607
3 14583
3 61327
4 81356
5 85065
5 18512
5 9169
6 89640
6 70595
7 53905
7 32741
8 40142
9 35301
10 62004
11 30952
11 33180
12 61023
13 88711
13 36424
13 52374
14 23059
15 9374
16 68813
16 49264
16 8226
17 24553
17 69481
17 86966
18 1358
18 80756
18 ...

output:

729514

result:

ok single line: '729514'

Test #19:

score: 0
Accepted
time: 544ms
memory: 11500kb

input:

90000 97119
1 27709
1 75782
2 49423
2 43710
2 38280
3 79902
3 16177
4 17534
4 4971
4 52981
4 85259
5 61410
6 47670
6 24515
7 32817
7 7674
8 19146
9 2135
9 33461
9 77724
9 79066
9 66561
10 49909
11 65637
12 17342
12 24782
13 61518
13 47315
13 9454
13 6223
14 53351
14 21336
14 62013
15 34765
15 75067
...

output:

0

result:

ok single line: '0'

Test #20:

score: 0
Accepted
time: 542ms
memory: 11764kb

input:

90000 97163
1 31427
1 70685
2 36010
2 48358
3 59856
3 18107
3 51742
4 14392
5 83470
5 51954
6 26664
6 62806
6 48105
6 61407
7 12462
8 5611
9 70485
10 17935
11 18902
11 22543
12 1168
12 43259
12 34919
12 70473
13 11396
13 8129
14 24370
14 81633
15 62316
16 31708
16 5261
16 73888
17 35435
17 47626
18 ...

output:

0

result:

ok single line: '0'

Test #21:

score: 0
Accepted
time: 1121ms
memory: 24460kb

input:

99999 99998
97945 69840
97945 99591
97945 23621
97945 96306
97945 49596
97945 45916
97945 90799
97945 95403
97945 93990
97945 98394
97945 41714
97945 61236
97945 1701
97945 37124
97945 16020
97945 19127
97945 59494
97945 64104
97945 63062
97945 37016
97945 97047
97945 50811
97945 35552
97945 23083
9...

output:

539460

result:

ok single line: '539460'

Test #22:

score: 0
Accepted
time: 2202ms
memory: 23876kb

input:

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

output:

627786

result:

ok single line: '627786'

Test #23:

score: 0
Accepted
time: 731ms
memory: 13288kb

input:

75001 100000
1 3
1 2
3 2
3 4
1 6
1 5
6 5
6 7
1 9
1 8
9 8
9 10
1 12
1 11
12 11
12 13
1 15
1 14
15 14
15 16
1 18
1 17
18 17
18 19
1 21
1 20
21 20
21 22
1 24
1 23
24 23
24 25
1 27
1 26
27 26
27 28
1 30
1 29
30 29
30 31
1 33
1 32
33 32
33 34
1 36
1 35
36 35
36 37
1 39
1 38
39 38
39 40
1 42
1 41
42 41
42...

output:

132779

result:

ok single line: '132779'