QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#400820#8556. Somewhere Over the Rainbowhos_lyricWA 18ms5296kbC++145.4kb2024-04-27 16:36:492024-04-27 16:36:50

Judging History

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

  • [2024-04-27 16:36:50]
  • 评测
  • 测评结果:WA
  • 用时:18ms
  • 内存:5296kb
  • [2024-04-27 16:36:49]
  • 提交

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 = 998244353;
using Mint = ModInt<MO>;

/*
  b[i] := a[i] + i(i-1)/2
  2 a[i] > a[i-1] + a[i+1]
  2 b[i] >= b[i-1] + b[i+1]
*/

using Pt = pair<Int, Int>;
bool cw(const Pt &a, const Pt &b, const Pt &c) {
  return ((__int128)(b.first - a.first) * (c.second - a.second) < (__int128)(b.second - a.second) * (c.first - a.first));
}

// \sum[0<=x<n] (a + d x)
Mint calc(Int n, Mint a, Mint d) {
  Mint ret = 0;
  ret += a * n;
  ret += d * (Mint(n) * Mint(n - 1) / 2);
  return ret;
}

int N;
Int M;
vector<Int> X, Y;

int main() {
  for (; ~scanf("%d%lld", &N, &M); ) {
    X.resize(N + 2);
    Y.resize(N + 2);
    X[0] = 0;
    Y[0] = 0;
    for (int i = 1; i <= N; ++i) {
      scanf("%lld%lld", &X[i], &Y[i]);
    }
    X[N + 1] = M;
    Y[N + 1] = 0;
    
    for (int i = 0; i <= N + 1; ++i) {
      Y[i] += X[i] * (X[i] - 1) / 2;
    }
    
    vector<Pt> ps;
    for (int i = 0; i <= N + 1; ++i) {
      const Pt p(X[i], Y[i]);
      for (; ps.size() >= 2 && !cw(ps.end()[-2], ps.end()[-1], p); ps.pop_back()) {}
      ps.push_back(p);
    }
// cerr<<"ps = "<<ps<<endl;
    
    Mint ans = 0;
    for (int j = 0; j < (int)ps.size() - 1; ++j) {
      const Pt &p0 = ps[j];
      const Pt &p1 = ps[j + 1];
      const Int dx = p1.first - p0.first;
      const Int dy = p1.second - p0.second;
      Int r = dy % dx;
      if (r < 0) r += dx;
      const Int q = (dy - r) / dx;
// cerr<<p0<<" "<<p1<<" "<<q<<" "<<r<<endl;
      // q+1, ..., q+1, q, ..., q
      ans += calc(r, p0.second, q + 1);
      ans += calc(dx - r, p0.second + r * (q + 1), q);
    }
    // \sum[0<=x<M] x(x-1)/2
    ans -= Mint(M) * Mint(M - 1) * Mint(M - 2) / 6;
    printf("%u\n", ans.x);
  }
  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3808kb

input:

3 6
1 100
4 42
5 22

output:

310

result:

ok 1 number(s): "310"

Test #2:

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

input:

0 5

output:

10

result:

ok 1 number(s): "10"

Test #3:

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

input:

20 50
4 11
7 4
8 20
13 9
14 29
15 26
16 19
18 18
29 46
30 7
34 37
35 16
38 14
39 47
40 18
42 30
43 6
44 23
47 13
48 4

output:

10725

result:

ok 1 number(s): "10725"

Test #4:

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

input:

40 100
3 20
4 67
5 62
7 75
9 49
11 14
14 31
18 11
19 5
24 98
25 100
28 35
30 19
31 20
32 71
37 29
38 5
40 94
45 95
46 65
51 2
52 52
53 61
54 77
57 50
59 69
61 30
65 50
67 4
68 56
73 99
75 15
76 47
78 52
79 72
83 91
88 44
89 3
91 55
94 2

output:

84575

result:

ok 1 number(s): "84575"

Test #5:

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

input:

60 150
1 119
4 135
5 75
9 13
11 72
15 34
17 130
21 12
26 107
30 133
33 18
34 135
36 78
37 95
38 26
42 24
44 25
51 49
52 73
54 40
55 100
56 67
61 128
62 87
74 131
75 103
77 84
78 37
81 51
82 83
83 89
84 58
89 117
93 148
94 127
95 118
96 103
97 49
98 28
99 83
102 65
105 97
107 103
109 9
113 40
116 107...

output:

286360

result:

ok 1 number(s): "286360"

Test #6:

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

input:

80 200
3 165
5 49
10 42
11 7
12 115
13 78
14 52
15 102
17 132
18 181
19 36
21 59
23 139
24 8
25 54
26 181
29 178
33 120
37 163
38 90
41 182
44 133
48 171
50 60
52 74
53 174
58 156
61 65
64 156
66 174
67 24
70 64
73 57
77 179
80 5
84 38
86 152
90 153
94 137
96 24
99 59
101 180
103 156
109 29
111 55
1...

output:

674709

result:

ok 1 number(s): "674709"

Test #7:

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

input:

100 250
1 99
2 176
4 122
5 16
6 138
7 59
9 134
10 185
11 249
12 245
13 148
14 9
15 74
20 190
23 203
24 239
31 41
32 202
33 155
37 26
40 170
43 84
45 144
46 59
47 169
54 184
56 37
57 106
58 38
60 219
63 40
66 245
68 106
70 97
72 127
74 146
75 1
76 173
77 179
79 205
81 72
83 90
85 17
88 227
90 163
92 ...

output:

1309875

result:

ok 1 number(s): "1309875"

Test #8:

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

input:

120 300
5 37
6 246
13 248
14 152
15 250
16 221
17 201
20 171
21 73
23 97
24 163
26 168
28 228
29 57
31 193
32 226
34 7
35 81
39 78
41 120
42 290
44 228
47 192
49 269
50 86
52 222
54 91
55 37
59 94
61 24
62 200
65 221
68 119
72 230
75 20
76 160
77 288
81 32
84 84
85 19
88 22
94 240
98 135
99 284
102 ...

output:

2261225

result:

ok 1 number(s): "2261225"

Test #9:

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

input:

140 350
1 82
3 115
4 59
7 212
9 307
10 149
18 189
19 43
21 77
22 264
25 177
29 117
31 49
32 181
33 231
34 45
35 152
41 314
42 258
43 276
44 33
45 114
48 119
49 323
59 100
60 26
61 40
63 304
64 343
65 277
67 138
74 234
78 188
79 59
80 213
81 257
83 197
84 76
94 286
96 72
97 225
98 14
101 154
102 202
...

output:

3588200

result:

ok 1 number(s): "3588200"

Test #10:

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

input:

160 400
3 76
4 29
7 25
16 73
18 76
19 368
21 311
23 111
25 298
26 51
28 396
29 11
31 213
32 284
35 395
36 214
37 256
40 216
41 178
44 101
46 382
47 286
49 64
52 184
56 113
57 247
59 308
64 393
66 392
67 46
68 256
69 230
76 153
78 263
80 396
84 9
87 3
93 373
94 35
95 48
97 305
98 310
105 2
106 94
109...

output:

5353300

result:

ok 1 number(s): "5353300"

Test #11:

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

input:

180 450
6 384
9 255
10 135
11 389
12 355
13 170
14 87
15 355
16 295
17 397
19 244
20 238
22 61
28 346
31 411
32 315
34 220
36 133
38 47
41 21
42 99
43 354
44 212
45 368
50 85
52 99
55 147
58 119
59 24
66 349
67 103
69 77
73 97
76 339
77 417
80 175
82 343
88 265
90 184
91 225
92 269
93 301
94 35
95 2...

output:

7619025

result:

ok 1 number(s): "7619025"

Test #12:

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

input:

200 500
2 302
4 37
14 133
16 148
18 138
19 61
21 206
24 361
25 417
26 171
27 70
29 314
30 43
31 231
34 341
37 373
39 144
43 371
44 239
46 494
49 159
51 18
53 163
56 167
57 446
60 215
62 381
63 116
64 37
67 382
69 375
71 331
77 125
79 432
81 256
83 130
84 333
86 264
89 31
91 27
93 340
94 366
95 315
9...

output:

10447875

result:

ok 1 number(s): "10447875"

Test #13:

score: -100
Wrong Answer
time: 18ms
memory: 5296kb

input:

123748 1237481
10 3247552102
14 4546572905
15 4871328114
18 5845593708
29 9417900801
31 10067411175
34 11041676717
37 12015942260
50 16237759481
58 18835800760
67 21758597131
72 23382372855
73 23707127994
78 25330903696
86 27928944751
90 29227965263
129 41893414353
132 42867679610
142 46115230387
15...

output:

710394229

result:

wrong answer 1st numbers differ - expected: '710395341', found: '710394229'