QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#883515#9642. 冲刺hos_lyric#45 853ms239092kbC++149.3kb2025-02-05 16:43:172025-02-05 16:43:17

Judging History

This is the latest submission verdict.

  • [2025-02-05 16:43:17]
  • Judged
  • Verdict: 45
  • Time: 853ms
  • Memory: 239092kb
  • [2025-02-05 16:43:17]
  • Submitted

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")

////////////////////////////////////////////////////////////////////////////////
template <unsigned M_> struct ModInt {
  static constexpr unsigned M = M_;
  unsigned x;
  constexpr ModInt() : x(0U) {}
  constexpr ModInt(unsigned x_) : x(x_ % M) {}
  constexpr ModInt(unsigned long long x_) : x(x_ % M) {}
  constexpr ModInt(int x_) : x(((x_ %= static_cast<int>(M)) < 0) ? (x_ + static_cast<int>(M)) : x_) {}
  constexpr 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) { x = (static_cast<unsigned long long>(x) * a.x) % 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; }
};
////////////////////////////////////////////////////////////////////////////////

constexpr unsigned MO = 1004535809;
using Mint = ModInt<MO>;

constexpr int LIM_INV = 10'100'000;
Mint inv[LIM_INV], fac[LIM_INV], invFac[LIM_INV];

void prepare() {
  inv[1] = 1;
  for (int i = 2; i < LIM_INV; ++i) {
    inv[i] = -((Mint::M / i) * inv[Mint::M % i]);
  }
  fac[0] = invFac[0] = 1;
  for (int i = 1; i < LIM_INV; ++i) {
    fac[i] = fac[i - 1] * i;
    invFac[i] = invFac[i - 1] * inv[i];
  }
}
Mint binom(Int n, Int k) {
  if (n < 0) {
    if (k >= 0) {
      return ((k & 1) ? -1 : +1) * binom(-n + k - 1, k);
    } else if (n - k >= 0) {
      return (((n - k) & 1) ? -1 : +1) * binom(-k - 1, n - k);
    } else {
      return 0;
    }
  } else {
    if (0 <= k && k <= n) {
      assert(n < LIM_INV);
      return fac[n] * invFac[k] * invFac[n - k];
    } else {
      return 0;
    }
  }
}

////////////////////////////////////////////////////////////////////////////////


// [z^m] ((1-P)z/(1-Pz))^(a+b-1) (1 + z/(1-Pz))
Mint brute(Mint P, Mint Q, int u, int v) {
  if (u == 0 && v == 0) return 1;
  Mint ret = 0;
  for (int m = 0; m <= u && m <= v; ++m) {
    const int a = u - m, b = v - m;
    if (a+b <= 0) break;
    auto at = [&](int n) -> Mint {
      return (n >= a+b-1) ? (binom(n, a+b-1) * P.pow(n - (a+b-1))) : 0;
    };
    ret += binom(a+b, a) * Q.pow(a) * (1-Q).pow(b) * (1-P).pow(a+b-1) * (at(m) + (1-P) * at(m-1));
  }
  return ret;
}

/*
  d := v - u >= 0
  +(1,0) i times
  +(0,1) (d+i) times
  count by z^u
  
  \sum[i>=[d=0]] binom(d+2i,i) Q^i (1-Q)^(d+i) z^i ((1-P)z/(1-Pz))^(d+2i-1) (1 + z/(1-Pz))
  = (1-Q)^d ((1-P)z/(1-Pz))^(d-1) (1 + z/(1-Pz)) \sum[i>=0] (binom(d+2i,i) (Q (1-Q) z ((1-P)z/(1-Pz))^2)^i - [d=0])
      C(t) := (1 - sqrt(1-4x)) / 2x
      F := Q (1-Q) z ((1-P)z/(1-Pz))^2
  = (1-Q)^d ((1-P)z/(1-Pz))^(d-1) (1 + z/(1-Pz)) ((1-4F)^(-1/2) C(F)^d - [d=0])
*/
Mint easy(Mint P, Mint Q, int u, int v) {
  if (u > v) return easy(P, 1-Q, v, u);
  if (u == 0 && v == 0) return 1;
  if (Q == 0) {
    // [z^u] ((1-P)z/(1-Pz))^(d-1) (1 + z/(1-Pz))
    const int d = v - u;
    auto at = [&](int n) -> Mint {
      return (n >= d-1) ? (binom(n, d-1) * P.pow(n - (d-1))) : 0;
    };
    return (1-P).pow(d-1) * (at(u) + (1-P) * at(u-1));
  }
  if (Q == 1) {
    return 0;
  }
  if (P == 0) {
    if ((u + v) % 3 == 0) {
      const int n = (u + v) / 3;
      const int a = u - n, b = v - n;
      return (a >= 0 && b >= 0) ? (binom(a+b, a) * Q.pow(a) * (1-Q).pow(b)) : 0;
    } else if ((u + v) % 3 == 1) {
      Mint ret = 0;
      if (u >= 1) ret += easy(P, Q, u - 1, v) * Q;
      if (v >= 1) ret += easy(P, Q, u, v - 1) * (1-Q);
      return ret;
    } else {
      return 0;
    }
  }
  assert(false);
}

/*
  F := Q (1-Q) z ((1-P)z/(1-Pz))^2
  (1-4F)^(-1/2) = (1-Pz) ((1-Pz)^2 - 4 Q (1-Q) (1-P)^2 z^3)^(-1/2)
*/
vector<Mint> calcInvSqrt(Mint P, Mint Q, int len) {
  const Mint fs[4] = {1, -2*P, P*P, -4 * Q * (1-Q) * (1-P)*(1-P)};
  vector<Mint> gs(len, 0);
  gs[0] = 1;
  for (int i = 1; i < len; ++i) {
    for (int j = 1; j <= i && j <= 3; ++j) gs[i] += ((-inv[2] + 1) * j - i) * fs[j] * gs[i - j];
    gs[i] *= inv[i];
  }
  for (int i = len; --i >= 1; ) gs[i] -= P * gs[i - 1];
  return gs;
}

/*
  d = 0
  ((1-P)z/(1-Pz))^(-1) (1 + z/(1-Pz)) ((1-4F)^(-1/2) - 1)
  = ((1 + (1-P)z) / (1-P)z) ((1-4F)^(-1/2) - 1)
*/
vector<Mint> calcDiag(Mint P, Mint Q, int len) {
  auto hs = calcInvSqrt(P, Q, len + 1);
// cerr<<"hs = "<<hs<<endl;
  assert(hs[0] == 1);
  hs.erase(hs.begin());
  const Mint val = (1-P).inv();
  for (int i = 0; i < len; ++i) hs[i] *= val;
  for (int i = len; --i >= 1; ) hs[i] += (1-P) * hs[i - 1];
  hs[0] = 1;
  return hs;
}

/*
  d = 1
  (1-Q) (1 + z/(1-Pz)) (1-4F)^(-1/2) C(F)
  = (1-Q) ((1 + (1-P)z) / (1-Pz)) ((1-4F)^(-1/2) - 1) (2F)^-1
*/
vector<Mint> calcDiag1(Mint P, Mint Q, int len) {
  if (Q == 0 || Q == 1) return {};
  auto hs = calcInvSqrt(P, Q, len + 4);
  assert(hs[0] == 1);
  assert(hs[1] == 0);
  assert(hs[2] == 0);
  hs.erase(hs.begin(), hs.begin() + 3);
  const Mint val = (1-Q) * (2 * Q * (1-Q) * (1-P)*(1-P)).inv();
  for (int i = 0; i < len; ++i) hs[i] *= val;
  for (int i = len; --i >= 1; ) hs[i] += (1-P) * hs[i - 1];
  for (int i = len; --i >= 1; ) hs[i] -= P * hs[i - 1];
  return hs;
}

/*
  (1-Q)^d (1-P)^(d-1)  z^(d-1) (1 + (1-P)z)  (1/(1-Pz))^d  (1-4F)^(-1/2) C(F)^d
  
  1-4F =: G/(1-Pz)^2
  H := (1-4F)^(-1/2)
  
  (1-4F)^(-1/2) C(F)^d
  = \sum[0<=k<=d] binom(d,k) (-1/2)^k sqrt(1-4F)^(k-1) / F^k
  = \sum[0<=k<=d] binom(d,k) (-1/2)^k (Q(1-Q)(1-P)^2)^-k (
        [k:even] H G^(k/2) (1-Pz)^k z^(-3k)
      + [k:odd ] G^((k-1)/2) (1-Pz)^(k+1) z^(-3k)
    )
*/

int main() {
  prepare();
  
  int N;
  Mint P, Q;
  for (; ~scanf("%d%u%u", &N, &P.x, &Q.x); ) {
    vector<int> U(N), V(N);
    for (int n = 0; n < N; ++n) {
      scanf("%d%d", &U[n], &V[n]);
    }
    
    int len = 0;
    for (int n = 0; n < N; ++n) {
      chmax(len, U[n]);
      chmax(len, V[n]);
    }
    len += 10;
    
    const auto diag = calcDiag(P, Q, len);
// cerr<<"diag = "<<diag<<endl;
// for(int i=0;i<len;++i)assert(diag[i]==brute(P,Q,i,i));
    const auto diag1U = calcDiag1(P, Q, len);
    const auto diag1V = calcDiag1(P, 1-Q, len);
    
    auto solve = [&](int u, int v) -> Mint {
      if (P == 0 || Q == 0 || Q == 1) return easy(P, Q, u, v);
      const int d = v - u;
      if (d == 0) return diag[u];
      if (d == 1) return diag1U[u];
      if (d == -1) return diag1V[v];
      return brute(P, Q, u, v);
    };
    Mint ans = 0;
    for (int n = 0; n < N; ++n) ans += solve(U[n], V[n]);
    printf("%u\n", ans.x);
  }
  return 0;
}

详细

Subtask #1:

score: 5
Accepted

Test #1:

score: 5
Accepted
time: 166ms
memory: 122268kb

input:

4872 301135505 501969174
62 292
76 226
20 67
11 117
145 33
28 37
43 16
26 147
148 257
282 249
275 89
129 30
100 253
110 206
100 230
2 103
71 260
154 164
14 183
185 184
227 25
166 164
187 144
181 62
176 139
121 253
252 181
87 145
255 63
66 2
178 209
161 30
112 49
71 178
264 281
81 190
124 216
231 91
...

output:

354616342

result:

ok single line: '354616342'

Test #2:

score: 5
Accepted
time: 171ms
memory: 122136kb

input:

4868 135320924 895002559
284 240
284 300
259 42
233 262
209 165
50 294
80 300
300 110
288 4
6 145
65 55
3 167
6 160
120 150
104 280
227 253
105 33
203 262
294 263
295 187
91 255
300 99
188 86
294 90
230 117
96 33
86 255
192 260
86 180
197 13
71 0
65 300
201 21
97 253
168 157
292 64
293 72
41 189
24 ...

output:

871825364

result:

ok single line: '871825364'

Test #3:

score: 5
Accepted
time: 166ms
memory: 122268kb

input:

4859 596446395 415664961
2 2
151 224
107 217
257 82
260 287
63 174
115 93
185 58
58 75
142 289
11 164
149 248
217 186
124 290
70 257
233 107
149 195
213 128
283 258
194 80
110 284
30 173
228 11
296 134
67 85
229 255
127 39
168 257
282 90
52 290
182 114
259 248
236 133
273 81
226 174
51 153
175 58
27...

output:

536513995

result:

ok single line: '536513995'

Subtask #2:

score: 0
Time Limit Exceeded

Test #4:

score: 0
Time Limit Exceeded

input:

5000 732289608 658888527
232 5000
2921 5000
5000 3706
5000 4148
5000 2105
1771 5000
3970 5000
3729 5000
1614 5000
5000 151
5000 3553
5000 3405
5000 2869
5000 835
2673 5000
3238 5000
2853 5000
5000 999
5000 3
420 5000
5000 3025
5000 6
830 5000
723 5000
5000 851
5000 1210
1844 5000
5000 4389
2203 5000...

output:


result:


Subtask #3:

score: 5
Accepted

Test #8:

score: 5
Accepted
time: 845ms
memory: 239048kb

input:

5000 0 454262500
3448016 3452435
2687053 2686221
8169887 8172208
9741693 9745191
2134360 2132632
2223989 2222297
6946537 6946296
2695577 2695329
1450857 1451876
8453241 8451200
9107846 9110910
8988968 8987572
9169892 9169540
7695546 7690794
5077074 5072683
7532989 7530319
7944119 7943232
2293369 229...

output:

138390816

result:

ok single line: '138390816'

Test #9:

score: 5
Accepted
time: 847ms
memory: 238936kb

input:

5000 0 197940563
163633 159658
719510 720734
5037104 5036502
8688315 8690135
3351747 3354084
5798281 5794946
6424717 6429537
5861254 5863610
608280 613091
6383637 6385534
604115 604050
7594548 7591747
9744453 9746034
8498980 8499566
4552034 4547746
6470487 6470247
4965628 4965391
4626287 4629397
924...

output:

12953277

result:

ok single line: '12953277'

Test #10:

score: 5
Accepted
time: 849ms
memory: 239092kb

input:

5000 0 792777687
9990775 9987449
9990793 9995124
9994277 9998218
9994537 9992587
9994936 9999367
9994417 9997299
9994341 9991910
9990605 9990976
9993356 9991105
9990649 9989123
9992634 9991999
9993403 9991953
9990844 9992544
9990838 9986624
9991823 9989920
9993872 9994934
9994306 9989379
9994621 999...

output:

352309080

result:

ok single line: '352309080'

Test #11:

score: 5
Accepted
time: 843ms
memory: 239084kb

input:

4998 0 742045327
9771639 9776635
9329681 9334673
9925908 9930906
9882915 9887911
9052925 9057916
9414381 9419379
9825440 9830432
9006220 9011215
9182712 9187706
9399767 9404761
9298823 9303823
9164839 9169836
9617019 9622012
9039297 9044297
9761372 9766369
9802576 9807575
9758643 9763634
9581696 958...

output:

233419299

result:

ok single line: '233419299'

Subtask #4:

score: 5
Accepted

Test #12:

score: 5
Accepted
time: 344ms
memory: 160964kb

input:

5000 102882260 0
6819081 6816853
5149145 5145899
9892659 9895176
8631345 8627935
349191 353992
7035406 7036453
5912292 5916306
9657928 9662549
6200108 6198289
2101773 2106094
9309396 9307219
7369903 7374376
9787731 9790309
9108307 9103593
3772703 3768160
976239 977233
3825147 3825573
7626595 7626660...

output:

1002452599

result:

ok single line: '1002452599'

Test #13:

score: 5
Accepted
time: 341ms
memory: 160968kb

input:

5000 555602683 0
2276456 2279872
444955 447940
2811067 2814002
8547026 8545882
7689196 7687362
455321 458118
8354815 8351991
711149 714735
2873655 2876981
8278974 8281196
9070916 9074678
8636306 8631746
941904 938930
3037633 3037860
2507283 2507558
9813566 9811611
2448024 2451969
3676139 3680770
355...

output:

178037273

result:

ok single line: '178037273'

Test #14:

score: 5
Accepted
time: 344ms
memory: 160964kb

input:

5000 908333657 0
9994166 9996809
9991489 9994755
9993341 9994108
9991372 9992866
9994840 9990082
9994027 9989630
9993654 9993948
9992205 9989909
9994749 9995572
9992437 9993410
9991970 9987187
9990739 9992390
9994923 9997120
9993800 9992453
9991802 9996027
9990978 9995031
9993903 9992211
9990417 998...

output:

592206530

result:

ok single line: '592206530'

Test #15:

score: 5
Accepted
time: 341ms
memory: 160908kb

input:

5000 567725802 0
9389957 9394948
9142847 9147844
9206678 9211676
9679253 9684252
9375914 9380910
9989453 9994448
9883411 9888409
9557299 9562295
9010479 9015473
9175767 9180764
9508606 9513605
9794399 9799399
9249788 9254785
9254166 9259165
9662897 9667888
9090407 9095399
9466618 9471615
9458979 946...

output:

999241460

result:

ok single line: '999241460'

Subtask #5:

score: 5
Accepted

Test #16:

score: 5
Accepted
time: 154ms
memory: 127516kb

input:

4976 574513706 284978964
52150 52150
482959 482959
189922 189922
459237 459237
168102 168102
367208 367208
291040 291040
272705 272705
3179 3179
179983 179983
307014 307014
66641 66641
384965 384965
263794 263794
61297 61297
33430 33430
376221 376221
69803 69803
328604 328604
208254 208254
441178 44...

output:

685390943

result:

ok single line: '685390943'

Test #17:

score: 5
Accepted
time: 151ms
memory: 127516kb

input:

4977 738732183 48211757
224643 224643
231072 231072
350520 350520
353788 353788
177601 177601
266218 266218
54772 54772
441204 441204
126298 126298
210653 210653
406849 406849
89882 89882
399624 399624
6984 6984
230890 230890
449088 449088
206247 206247
286000 286000
177809 177809
351108 351108
3601...

output:

834673621

result:

ok single line: '834673621'

Test #18:

score: 5
Accepted
time: 142ms
memory: 127768kb

input:

5000 82619521 288046394
499070 499070
499507 499507
499421 499421
499996 499996
497341 497341
499880 499880
498466 498466
497215 497215
497717 497717
499041 499041
499450 499450
498373 498373
495077 495077
498387 498387
499689 499689
497281 497281
497675 497675
497312 497312
498371 498371
499832 499...

output:

256096624

result:

ok single line: '256096624'

Subtask #6:

score: 15
Accepted

Test #19:

score: 15
Accepted
time: 838ms
memory: 238888kb

input:

5000 235325377 512694546
5854662 5854662
315229 315229
3137556 3137556
7608068 7608068
9583921 9583921
4958765 4958765
3109547 3109547
3431604 3431604
3400130 3400130
5073586 5073586
2742677 2742677
5111263 5111263
5485216 5485216
7602789 7602789
1522223 1522223
2264844 2264844
8283226 8283226
80915...

output:

374571076

result:

ok single line: '374571076'

Test #20:

score: 15
Accepted
time: 841ms
memory: 239040kb

input:

4999 577526640 536592123
8982831 8982831
8658108 8658108
3051124 3051124
6249675 6249675
736542 736542
1907066 1907066
7853081 7853081
4392691 4392691
6371987 6371987
7681658 7681658
367808 367808
4584307 4584307
6635109 6635109
1920456 1920456
9443645 9443645
7198022 7198022
4573392 4573392
8832798...

output:

858926241

result:

ok single line: '858926241'

Test #21:

score: 15
Accepted
time: 853ms
memory: 238964kb

input:

5000 967858359 21174968
9997118 9997118
9998285 9998285
9996180 9996180
9999627 9999627
9996927 9996927
9999741 9999741
9997786 9997786
9997212 9997212
9996650 9996650
9996632 9996632
9996692 9996692
9999548 9999548
9998795 9998795
9998904 9998904
9997909 9997909
9998051 9998051
9995365 9995365
9995...

output:

236292211

result:

ok single line: '236292211'

Subtask #7:

score: 10
Accepted

Test #22:

score: 10
Accepted
time: 850ms
memory: 239040kb

input:

5000 736097577 890228464
1825247 1825246
4650840 4650840
2234311 2234312
7343146 7343145
3787634 3787634
2656735 2656734
1463014 1463013
3322493 3322494
8952134 8952133
9554797 9554798
9938426 9938425
7324212 7324212
9852188 9852188
4135136 4135135
2970038 2970037
216167 216166
5330601 5330602
72371...

output:

60773436

result:

ok single line: '60773436'

Test #23:

score: 10
Accepted
time: 851ms
memory: 239056kb

input:

4999 71938875 980975552
9165546 9165547
6658015 6658014
715219 715219
2770358 2770357
6724087 6724088
190936 190937
762142 762143
7697875 7697876
2560417 2560416
244298 244297
5390425 5390425
2948144 2948144
5979604 5979605
7544060 7544060
7640731 7640731
9811715 9811715
8451395 8451396
3957616 3957...

output:

553974017

result:

ok single line: '553974017'

Test #24:

score: 10
Accepted
time: 849ms
memory: 239092kb

input:

5000 718317306 176345776
9998853 9998852
9997215 9997216
9998526 9998526
9997709 9997708
9995955 9995955
9995446 9995447
9997855 9997855
9995342 9995342
9999947 9999946
9997976 9997976
9999200 9999199
9998210 9998211
9998941 9998941
9996613 9996613
9996078 9996077
9995751 9995752
9999173 9999172
999...

output:

947034016

result:

ok single line: '947034016'

Subtask #8:

score: 0
Time Limit Exceeded

Test #25:

score: 0
Time Limit Exceeded

input:

5000 118468585 976109781
377801 381122
359413 364103
226131 224910
163598 165233
16964 20842
113098 111221
129456 130508
478339 478755
182926 179190
143979 142184
388264 390144
365026 366013
16759 19223
359155 361291
62049 57288
66890 63415
425143 423399
382820 384122
488899 485326
218034 220421
554...

output:


result:


Subtask #9:

score: 0
Time Limit Exceeded

Test #29:

score: 0
Time Limit Exceeded

input:

3000 177229351 510136745
179259 179695
2298716 2302777
4922758 4926164
3300268 3302823
1818638 1817659
4429564 4426586
3915685 3911287
2997695 2999804
459298 460850
52829 57660
4442042 4438650
1148083 1150580
4093935 4089443
2032081 2031185
1910400 1912395
3852021 3853590
1073219 1068243
1986701 198...

output:


result:


Subtask #10:

score: 0
Time Limit Exceeded

Test #33:

score: 0
Time Limit Exceeded

input:

5000 73170491 768796521
670558 670089
2958025 2960803
5414101 5416526
4699271 4699813
4561016 4565915
7035233 7030323
1378672 1378584
4964463 4966800
7242131 7240789
8247079 8245607
9279015 9283822
6727462 6723335
438898 443570
8300373 8299256
6811203 6810475
3356946 3358886
3697156 3698634
2948112 ...

output:


result: