QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#163072#7111. Press the Buttonhos_lyricAC ✓1ms3940kbC++144.6kb2023-09-03 20:02:152023-09-03 20:02:16

Judging History

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

  • [2023-09-03 20:02:16]
  • 评测
  • 测评结果:AC
  • 用时:1ms
  • 内存:3940kb
  • [2023-09-03 20:02:15]
  • 提交

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 <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; }


template <class U, class T> T pathUnder(U m, U a, U b, U n, T e, T x, T y) {
  U c = (a * n + b) / m;
  T pre = e, suf = e;
  for (; ; ) {
    const U p = a / m; a %= m; x = x * y.pow(p);
    const U q = b / m; b %= m; pre = pre * y.pow(q);
    c -= (p * n + q);
    if (c == 0) return pre * x.pow(n) * suf;
    const U d = (m * c - b - 1) / a + 1;
    suf = y * x.pow(n - d) * suf;
    b = m - b - 1 + a; swap(m, a); n = c - 1; c = d; swap(x, y);
  }
}

struct Data {
  Int val, sum0, sum1;
  Data() : val(0), sum0(0), sum1(0) {}
  Data(Int val_, Int sum0_, Int sum1_) : val(val_), sum0(sum0_), sum1(sum1_) {}
  friend Data operator*(const Data &a, const Data &b) {
    return Data(a.val + b.val, a.sum0 + b.sum0, a.sum1 + b.sum1 + a.val * b.sum0);
  }
  Data pow(Int e) const {
    assert(e >= 0);
    return Data(e * val, e * sum0, e * sum1 + (e * (e - 1) / 2) * val * sum0);
  }
};

Int sumFloors(Int M, Int A, Int B, Int N) {
  return pathUnder<unsigned long long>(M, A, B, N,
        Data(), Data(0, 1, 0), Data(1, 0, 0)).sum1;
}


Int brute(Int A, Int B, Int C, Int D, Int V, Int T) {
  Int led = 0, counter = 0, last = 0;
  for (Int t = 0; t <= T; ++t) {
    auto push = [&]() -> void {
      if (t - last > V) {
        led = 0;
      }
      if (!led) {
        led = 1;
      } else {
        ++counter;
      }
      last = t;
    };
    if (t % A == 0) for (Int b = 0; b < B; ++b) push();
    if (t % C == 0) for (Int d = 0; d < D; ++d) push();
  }
  return counter;
}

Int solve(Int A, Int B, Int C, Int D, Int V, Int T) {
  const Int X = T / A;
  const Int Y = T / C;
  Int ans = 0;
  if (A <= V || C <= V) {
    ans += (X + 1) * B;
    ans += (Y + 1) * D;
    ans -= 1;
  } else {
    ans += (X + 1) * (B - 1);
    ans += (Y + 1) * (D - 1);
    ans += (sumFloors(A, C, A, Y + 1) - sumFloors(A, C, A - V - 1, Y + 1));
    ans += (sumFloors(C, A, C, X + 1) - sumFloors(C, A, C - V - 1, X + 1));
    ans -= (T / (A / __gcd(A, C) * C) + 1);
  }
  return ans;
}

void stress() {
  constexpr Int LIM = 20;
#define rep(i) for (int i = 1; i <= LIM; ++i)
  rep(A) rep(B) rep(C) rep(D) rep(V) rep(T) {
    const Int brt = brute(A, B, C, D, V, T);
    const Int slv = solve(A, B, C, D, V, T);
    if (brt != slv) {
      cerr << A << " " << B << " " << C << " " << D << " " << V << " " << T << ": " << brt << " " << slv << endl;
    }
    assert(brt == slv);
  }
#undef rep
}

// [0, 2^32)
unsigned xrand() {
  static unsigned x = 314159265, y = 358979323, z = 846264338, w = 327950288;
  unsigned t = x ^ x << 11; x = y; y = z; z = w; return w = w ^ w >> 19 ^ t ^ t >> 8;
}
void stressLarge() {
  for (int t = 0; t < 1'000'000; ++t) {
    const Int A = 1 + xrand() % 1'000'000;
    const Int B = 1 + xrand() % 1'000'000;
    const Int C = 1 + xrand() % 1'000'000;
    const Int D = 1 + xrand() % 1'000'000;
    const Int V = 1 + 1ULL * xrand() * xrand() % 1'000'000'000'000LL;
    const Int T = 1 + 1ULL * xrand() * xrand() % 1'000'000'000'000LL;
    const Int slv = solve(A, B, C, D, V, T);
    printf("%lld\n", slv);
  }
}

int main() {
#ifdef LOCAL
  // stress();
  // stressLarge();
#endif
  
  for (int numCases; ~scanf("%d", &numCases); ) { for (int caseId = 1; caseId <= numCases; ++caseId) {
    Int A, B, C, D, V, T;
    scanf("%lld%lld%lld%lld%lld%lld", &A, &B, &C, &D, &V, &T);
    
    const Int ans = solve(A, B, C, D, V, T);
    printf("%lld\n", ans);
  }
#ifndef LOCAL
  break;
#endif
  }
  return 0;
}

这程序好像有点Bug,我给组数据试试?

详细

Test #1:

score: 100
Accepted
time: 1ms
memory: 3920kb

input:

2
8 2 5 1 2 18
10 2 5 1 2 10

output:

6
4

result:

ok 2 number(s): "6 4"

Test #2:

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

input:

1000
8 6 2 6 3 17
1 6 1 1 1 30
5 4 8 8 1 31
7 6 10 3 6 12
9 1 4 4 3 38
3 3 5 8 1 8
9 1 5 2 3 18
6 10 10 8 2 40
9 6 9 10 3 9
2 5 1 10 10 39
7 7 1 2 4 19
8 10 8 6 7 36
2 9 1 1 7 17
1 2 3 5 6 14
8 8 8 7 1 46
6 9 3 9 4 6
10 8 1 7 10 18
7 1 7 10 3 50
1 10 2 1 5 1
5 8 4 9 7 44
9 2 5 4 7 42
9 1 2 1 1 20
5 ...

output:

71
216
52
16
38
22
7
102
30
499
60
75
98
54
84
44
148
80
20
179
45
4
463
139
56
30
45
127
204
121
42
69
38
98
63
121
25
142
17
75
24
175
114
40
32
11
29
85
35
7
66
49
492
49
49
14
17
53
431
161
94
27
21
135
71
92
33
290
57
300
18
89
155
55
10
219
203
390
28
50
67
213
26
18
27
19
128
101
118
62
46
15...

result:

ok 1000 numbers

Test #3:

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

input:

100
9 9 3 1 1 2
5 5 7 7 7 50
2 1 8 10 10 45
3 4 5 7 7 38
1 6 9 5 2 13
5 6 7 9 1 29
10 1 4 3 3 19
6 10 10 8 7 3
9 3 10 3 3 14
9 7 1 7 8 38
3 78 5 43 5 958
4 98 3 42 10 7
3 16 9 71 7 52
1 70 3 86 3 410
10 44 1 56 3 628
9 15 9 94 10 15
9 95 9 61 2 525
2 23 8 37 10 108
5 92 3 65 10 331
6 54 6 44 3 537
2...

output:

9
110
82
107
93
73
13
17
10
307
33215
321
713
40551
37995
217
9145
1782
13378
8730
270
2433
143
17
30
136
10
82
33
97
733
126
2917
623
364
1448
1212
872
268
1031
1601
3889
122
523
819
83
17513
277
2973
4651
202187
42602
17225
7881
32574
7087
4453
34029
20529
17520
58488
189327
14380
67133
90956
4328...

result:

ok 100 numbers

Extra Test:

score: 0
Extra Test Passed