-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1993F.cpp
315 lines (272 loc) · 7.48 KB
/
1993F.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
// Created at: Tue Aug 6 19:58:18 CST 2024
/*
Problem Summary:
Given multiple test cases, each case contains a script describing the movements
of a robot (U, D, L, R). We need to determine how many times the robot returns
to the origin (0, 0) after executing the script 'k' times.
Key Insight:
The number of times the robot returns to the origin in one execution of the
script can be calculated based on the changes in x, y coordinates caused by the
script. If the net displacement for the script is (dx, dy), the robot will
return to the origin whenever: dx * m <= w and dy * m <= h where m is a
multiplier.
Step-by-Step Plan:
1. Parse the input and store necessary values.
2. Compute the net displacement after one execution of the script.
3. Check how many times the robot returns to the origin based on w, h, and the
net displacement.
*/
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
#ifdef DBG
#include "debug.h"
#else
#define dbg(...)
#define dbg_export(...)
#endif
int n, w, h; // 1e6
i64 k; // 1e12
string s;
/* Snippet Header */
#include <bits/stdc++.h>
using namespace std;
/* Snippet BEGIN */
class NumberTheory
{
public:
NumberTheory(int n) : n(n), min_divisor(n + 1, 0), phi(n + 1) { init(); }
vector<int> factorize(int x)
{
vector<int> ret;
while (x != 1) {
int xp = min_divisor[x];
ret.push_back(min_divisor[x]);
while (x % xp == 0)
x /= xp;
}
return ret;
}
vector<int> &all_primes() { return primes; }
bool is_prime(int n) { return min_divisor[n] == n; }
// Returns x^(-1) (mod m)
int inv(int x, int m)
{
assert(2 <= m && m <= n);
// inv(x, m) = x^(phi(m) - 1)
int result = pow(x, phi[m] - 1, m);
#ifdef DBG
assert((i64)result * x % m == 1);
#endif
return result;
}
private:
int n;
vector<int> min_divisor;
vector<int> primes;
vector<int> phi; // Euler's totient function
void init()
{
phi[1] = 1;
for (int i = 2; i <= n; i++) {
if (min_divisor[i] == 0) {
min_divisor[i] = i;
phi[i] = i - 1;
primes.push_back(i);
}
for (int j : primes) {
if (i * j > n)
break;
min_divisor[i * j] = j;
if (i % j == 0) {
phi[i * j] = phi[i] * j;
break;
} else {
phi[i * j] = phi[i] * (j - 1);
}
}
}
}
static int pow(int a, int n, int mod)
{
int res = 1;
while (n) {
if (n & 1) {
res = (i64)res * a % mod;
}
a = (i64)a * a % mod;
n >>= 1;
}
return res;
}
};
// Return [ x, y, d ]
// a * x + b * y = d = gcd(a, b)
//
// All solution x' and y':
// x' = x + k * (b / d)
// y' = y - k * (a / d)
tuple<i64, i64, i64> extended_gcd(i64 a, i64 b)
{
if (b == 0)
return {1, 0, a};
auto [x, y, d] = extended_gcd(b, a % b);
return {y, x - a / b * y, d};
}
// [Tutorial] Chinese Remainder Theorem
// https://codeforces.com/blog/entry/61290
// x == a1 (mod n1)
// x == a2 (mod n2)
// Return [ ret, lcm(n1, n2) ]
// Solution:
// x == ret (mod lcm(n1, n2))
// 0 <= ret < lcm(n1, n2)
optional<tuple<i64, i64>> chinese_remainder(i64 a1, i64 n1, i64 a2, i64 n2)
{
// x = k1 * n1 + a1 = k2 * n2 + a2
// n1 * (-k1) + n2 * k2 = a1 - a2
auto [k1, k2, d] = extended_gcd(n1, n2);
k1 = -k1;
#ifdef DBG
assert(n1 * (-k1) + n2 * k2 == d);
#endif
if ((a1 - a2) % d != 0) {
return nullopt;
}
i64 scaled = (a1 - a2) / d;
i64 n2_ = n2 / d;
i64 lcm = n1 * n2_;
dbg(a1, n1, a2, n2, k1, k2, lcm);
// x = (a1 + k1 * n1 % lcm) % lcm
// Note that k1 * n1 overflows (because k1~1e18, n1~1e9)
//
// Note that X * n1 % lcm
// = X * n1 % (n1 * n2 / d) % lcm
// = X * (n2 / d) * n1 % lcm
i64 x = (a1 + k1 % n2_ * scaled % n2_ * n1 % lcm) % lcm;
if (x < lcm)
x += lcm;
return {{x, lcm}};
}
i64 floor_div(i64 a, i64 b)
{
// auto res = lldiv(a, b);
i64 d = a / b;
i64 r = a % b;
return r ? (d - ((a < 0) ^ (b < 0))) : d;
}
i64 ceil_div(i64 a, i64 b)
{
i64 d = a / b;
i64 r = a % b;
bool quotientPositive = (b >= 0) == (a >= 0);
return d + (r != 0 && quotientPositive);
}
// Given that x == a (mod m)
// Returns how many times x fall in [l, r]
i64 count_fallin(i64 a, i64 m, i64 l, i64 r)
{
dbg(a, m, l, r);
assert(l <= r);
assert(m > 0);
i64 k_min = ceil_div(l - a, m);
i64 k_max = floor_div(r - a, m);
if (k_max < k_min) {
return 0;
}
return k_max - k_min + 1;
}
/* Snippet END */
NumberTheory nt(2e6 + 3);
// Function to compute number of times the robot reaches the origin
i64 solve()
{
int dx = 0, dy = 0;
i64 result = 0;
vector<tuple<int, int>> positions;
int w2 = w * 2;
int h2 = h * 2;
dbg(w, h, s, k);
// Calculate net displacement for one script execution
for (char dir : s) {
if (dir == 'L')
dx--;
if (dir == 'R')
dx++;
if (dir == 'U')
dy++;
if (dir == 'D')
dy--;
dx = (dx + w2) % w2;
dy = (dy + h2) % h2;
positions.push_back({dx, dy});
}
auto [dxn, dyn] = positions.back();
dbg(dxn, dyn);
for (auto [dx, dy] : positions) {
// dx + i * dxn == 0 (mod w2), i = 0, 1, 2, ..., k-1
// dy + i * dyn == 0 (mod h2), i = 0, 1, 2, ..., k-1
//
// dx + i * dxn == 0 (mod w2), i = 0, 1, 2, ..., k-1
// i * dxn == -dx (mod w2)
// i == -dx * dxn^(-1) (mod w2) <require gcd(dnx, w2)=1>
// suppose d = gcd(dxn, w2), dxn' = dxn / d, w2' = w2 / d
// i * dxn' == -dx / d (mod w2')
// if no d | dx, no solution
// i == -dx / d * dxn'^(-1) (mod w2')
// let rightside = di
// i == di (mod w2')
// j == dj (mod h2')
dbg("----", dx, dy);
i64 d0 = gcd(dxn, w2);
i64 d1 = gcd(dyn, h2);
dbg(dxn, w2, d0);
if (dx % d0 > 0)
continue;
if (dy % d1 > 0)
continue;
i64 w2_ = w2 / d0;
i64 h2_ = h2 / d1;
i64 dxn_ = dxn / d0;
i64 dyn_ = dyn / d1;
i64 count;
if (w2_ == 1 && h2_ == 1) {
count = k;
} else if (w2_ == 1) {
i64 dj = -dy / d1 * nt.inv(dyn_, h2_);
count = count_fallin(dj, h2_, 0, k - 1);
} else if (h2_ == 1) {
i64 di = -dx / d0 * nt.inv(dxn_, w2_);
count = count_fallin(di, w2_, 0, k - 1);
} else {
i64 di = -dx / d0 * nt.inv(dxn_, w2_);
i64 dj = -dy / d1 * nt.inv(dyn_, h2_);
dbg(di, dj);
auto res = chinese_remainder(di, w2_, dj, h2_);
if (!res)
continue;
auto [i, shift] = *res;
count = count_fallin(i, shift, 0, k - 1);
dbg(i, shift, count);
}
result += count;
}
return result;
}
int main()
{
#ifndef DBG
ios::sync_with_stdio(false);
cin.tie(nullptr);
#endif
int t;
cin >> t;
while (t--) {
cin >> n >> k >> w >> h;
cin >> s;
auto ans = solve();
cout << ans << endl;
}
return 0;
}
// Time Complexity: O(n) per test case.