#include "bits/stdc++.h"
using namespace std;
#define int long long
const int MAXN = 2e5 + 10;
const int MOD = 998244353;
#define ll __int128
mt19937_64 rng((int)std::chrono::steady_clock::now().time_since_epoch().count());
int rnd(int x, int y) {
int u = uniform_int_distribution<int>(x, y)(rng); return u;
}
ll read() { int x; cin >> x; return (ll)x; }
long long bm(long long b, long long p) {
if(p==0) return 1 % MOD;
long long r = bm(b, p >> 1);
if(p&1) return (((r*r) % MOD) * b) % MOD;
return (r*r) % MOD;
}
long long inv(long long b) {
return bm(b, MOD-2);
}
long long f[MAXN];
long long nCr(int n, int r) {
long long ans = f[n]; ans *= inv(f[r]); ans %= MOD;
ans *= inv(f[n-r]); ans %= MOD; return ans;
}
void precomp() { for(int i=0; i<MAXN; i++) f[i] = (i == 0 ? 1 % MOD : (f[i-1] * i) % MOD); }
int fastlog(int x) {
return (x == 0 ? -1 : 32 - __builtin_clz(x) - 1);
}
void gay(int i) { cout << "Case #" << i << ": "; }
void solve(int tc) {
int k, n, m, q;
cin >> k >> n >> m >> q;
int st[16][k][n];
for(int i=0; i<16; i++) {
for(int j=0; j<k; j++) {
for(int l=0; l<n; l++) {
st[i][j][l] = 1e16;
}
}
}
for(int i=0; i<m; i++) {
int a, b, t;
cin >> a >> b >> t;
st[0][a % k][b] = min(st[0][a % k][b], t);
}
for(int i=1; i<16; i++) {
for(int j=0; j<k; j++) {
for(int l=0; l<n; l++) {
if(l / k >= (1 << i)) {
for(int g=0; g<k; g++) {
int block = l / k - (1 << (i-1));
st[i][j][l] = min(st[i][j][l], st[i-1][g][l] + st[i-1][j][block * k + g]);
}
}
}
}
}
for(int i=0; i<q; i++) {
int a, b;
cin >> a >> b;
int A = a / k, B = b / k;
int d = B - A;
int dp[k];
for(int j=0; j<k; j++) dp[j] = 1e16;
dp[b % k] = 0;
for(int j=15; j>=0; j--) {
if(d >= (1 << j)) {
int newdp[k];
for(int l=0; l<k; l++) newdp[l] = 1e16;
for(int l=0; l<k; l++) {
int rel = B * k + l;
for(int c=0; c<k; c++) {
newdp[c] = min(newdp[c], dp[l] + st[j][c][rel]);
}
}
d -= (1 << j);
B -= (1 << j);
for(int l=0; l<k; l++) dp[l] = newdp[l];
}
}
cout << (dp[a % k] == 1e16 ? -1 : dp[a % k]) << '\n';
}
}
int32_t main() {
precomp();
ios::sync_with_stdio(0); cin.tie(0);
int t = 1; //cin >> t;
for(int i=1; i<=t; i++) solve(i);
}
// I don't know geometry.
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
9080 KB |
Output is correct |
2 |
Correct |
2 ms |
1884 KB |
Output is correct |
3 |
Correct |
3 ms |
1876 KB |
Output is correct |
4 |
Correct |
3 ms |
1876 KB |
Output is correct |
5 |
Correct |
4 ms |
2028 KB |
Output is correct |
6 |
Correct |
3 ms |
2032 KB |
Output is correct |
7 |
Correct |
4 ms |
2004 KB |
Output is correct |
8 |
Correct |
26 ms |
9008 KB |
Output is correct |
9 |
Correct |
26 ms |
9056 KB |
Output is correct |
10 |
Correct |
16 ms |
8256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
46 ms |
16048 KB |
Output is correct |
2 |
Correct |
2 ms |
1888 KB |
Output is correct |
3 |
Correct |
2 ms |
1876 KB |
Output is correct |
4 |
Correct |
3 ms |
1876 KB |
Output is correct |
5 |
Correct |
2 ms |
1888 KB |
Output is correct |
6 |
Correct |
2 ms |
1876 KB |
Output is correct |
7 |
Correct |
6 ms |
2004 KB |
Output is correct |
8 |
Correct |
6 ms |
2280 KB |
Output is correct |
9 |
Correct |
27 ms |
9040 KB |
Output is correct |
10 |
Correct |
66 ms |
22940 KB |
Output is correct |
11 |
Correct |
46 ms |
16076 KB |
Output is correct |
12 |
Correct |
51 ms |
21940 KB |
Output is correct |
13 |
Correct |
68 ms |
22988 KB |
Output is correct |
14 |
Correct |
39 ms |
14540 KB |
Output is correct |
15 |
Correct |
49 ms |
21836 KB |
Output is correct |
16 |
Correct |
49 ms |
21860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
1876 KB |
Output is correct |
2 |
Correct |
3 ms |
1888 KB |
Output is correct |
3 |
Correct |
3 ms |
1888 KB |
Output is correct |
4 |
Correct |
3 ms |
1888 KB |
Output is correct |
5 |
Correct |
3 ms |
1876 KB |
Output is correct |
6 |
Correct |
4 ms |
2024 KB |
Output is correct |
7 |
Correct |
4 ms |
2132 KB |
Output is correct |
8 |
Correct |
5 ms |
2544 KB |
Output is correct |
9 |
Correct |
4 ms |
2388 KB |
Output is correct |
10 |
Correct |
22 ms |
8908 KB |
Output is correct |
11 |
Correct |
42 ms |
15920 KB |
Output is correct |
12 |
Correct |
60 ms |
22736 KB |
Output is correct |
13 |
Correct |
65 ms |
23068 KB |
Output is correct |
14 |
Correct |
54 ms |
22424 KB |
Output is correct |
15 |
Correct |
54 ms |
21732 KB |
Output is correct |
16 |
Correct |
48 ms |
21708 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
1876 KB |
Output is correct |
2 |
Correct |
3 ms |
1888 KB |
Output is correct |
3 |
Correct |
3 ms |
1888 KB |
Output is correct |
4 |
Correct |
3 ms |
1888 KB |
Output is correct |
5 |
Correct |
3 ms |
1876 KB |
Output is correct |
6 |
Correct |
4 ms |
2024 KB |
Output is correct |
7 |
Correct |
4 ms |
2132 KB |
Output is correct |
8 |
Correct |
5 ms |
2544 KB |
Output is correct |
9 |
Correct |
4 ms |
2388 KB |
Output is correct |
10 |
Correct |
22 ms |
8908 KB |
Output is correct |
11 |
Correct |
42 ms |
15920 KB |
Output is correct |
12 |
Correct |
60 ms |
22736 KB |
Output is correct |
13 |
Correct |
65 ms |
23068 KB |
Output is correct |
14 |
Correct |
54 ms |
22424 KB |
Output is correct |
15 |
Correct |
54 ms |
21732 KB |
Output is correct |
16 |
Correct |
48 ms |
21708 KB |
Output is correct |
17 |
Correct |
46 ms |
15948 KB |
Output is correct |
18 |
Correct |
2 ms |
1876 KB |
Output is correct |
19 |
Correct |
3 ms |
1876 KB |
Output is correct |
20 |
Correct |
4 ms |
1888 KB |
Output is correct |
21 |
Correct |
2 ms |
1876 KB |
Output is correct |
22 |
Correct |
2 ms |
1884 KB |
Output is correct |
23 |
Correct |
5 ms |
2004 KB |
Output is correct |
24 |
Correct |
4 ms |
2132 KB |
Output is correct |
25 |
Correct |
5 ms |
2584 KB |
Output is correct |
26 |
Correct |
5 ms |
2388 KB |
Output is correct |
27 |
Correct |
25 ms |
8988 KB |
Output is correct |
28 |
Correct |
64 ms |
22828 KB |
Output is correct |
29 |
Correct |
69 ms |
23064 KB |
Output is correct |
30 |
Correct |
60 ms |
22604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
9080 KB |
Output is correct |
2 |
Correct |
2 ms |
1884 KB |
Output is correct |
3 |
Correct |
3 ms |
1876 KB |
Output is correct |
4 |
Correct |
3 ms |
1876 KB |
Output is correct |
5 |
Correct |
4 ms |
2028 KB |
Output is correct |
6 |
Correct |
3 ms |
2032 KB |
Output is correct |
7 |
Correct |
4 ms |
2004 KB |
Output is correct |
8 |
Correct |
26 ms |
9008 KB |
Output is correct |
9 |
Correct |
26 ms |
9056 KB |
Output is correct |
10 |
Correct |
16 ms |
8256 KB |
Output is correct |
11 |
Correct |
46 ms |
16048 KB |
Output is correct |
12 |
Correct |
2 ms |
1888 KB |
Output is correct |
13 |
Correct |
2 ms |
1876 KB |
Output is correct |
14 |
Correct |
3 ms |
1876 KB |
Output is correct |
15 |
Correct |
2 ms |
1888 KB |
Output is correct |
16 |
Correct |
2 ms |
1876 KB |
Output is correct |
17 |
Correct |
6 ms |
2004 KB |
Output is correct |
18 |
Correct |
6 ms |
2280 KB |
Output is correct |
19 |
Correct |
27 ms |
9040 KB |
Output is correct |
20 |
Correct |
66 ms |
22940 KB |
Output is correct |
21 |
Correct |
46 ms |
16076 KB |
Output is correct |
22 |
Correct |
51 ms |
21940 KB |
Output is correct |
23 |
Correct |
68 ms |
22988 KB |
Output is correct |
24 |
Correct |
39 ms |
14540 KB |
Output is correct |
25 |
Correct |
49 ms |
21836 KB |
Output is correct |
26 |
Correct |
49 ms |
21860 KB |
Output is correct |
27 |
Correct |
3 ms |
1876 KB |
Output is correct |
28 |
Correct |
3 ms |
1888 KB |
Output is correct |
29 |
Correct |
3 ms |
1888 KB |
Output is correct |
30 |
Correct |
3 ms |
1888 KB |
Output is correct |
31 |
Correct |
3 ms |
1876 KB |
Output is correct |
32 |
Correct |
4 ms |
2024 KB |
Output is correct |
33 |
Correct |
4 ms |
2132 KB |
Output is correct |
34 |
Correct |
5 ms |
2544 KB |
Output is correct |
35 |
Correct |
4 ms |
2388 KB |
Output is correct |
36 |
Correct |
22 ms |
8908 KB |
Output is correct |
37 |
Correct |
42 ms |
15920 KB |
Output is correct |
38 |
Correct |
60 ms |
22736 KB |
Output is correct |
39 |
Correct |
65 ms |
23068 KB |
Output is correct |
40 |
Correct |
54 ms |
22424 KB |
Output is correct |
41 |
Correct |
54 ms |
21732 KB |
Output is correct |
42 |
Correct |
48 ms |
21708 KB |
Output is correct |
43 |
Correct |
46 ms |
15948 KB |
Output is correct |
44 |
Correct |
2 ms |
1876 KB |
Output is correct |
45 |
Correct |
3 ms |
1876 KB |
Output is correct |
46 |
Correct |
4 ms |
1888 KB |
Output is correct |
47 |
Correct |
2 ms |
1876 KB |
Output is correct |
48 |
Correct |
2 ms |
1884 KB |
Output is correct |
49 |
Correct |
5 ms |
2004 KB |
Output is correct |
50 |
Correct |
4 ms |
2132 KB |
Output is correct |
51 |
Correct |
5 ms |
2584 KB |
Output is correct |
52 |
Correct |
5 ms |
2388 KB |
Output is correct |
53 |
Correct |
25 ms |
8988 KB |
Output is correct |
54 |
Correct |
64 ms |
22828 KB |
Output is correct |
55 |
Correct |
69 ms |
23064 KB |
Output is correct |
56 |
Correct |
60 ms |
22604 KB |
Output is correct |
57 |
Correct |
103 ms |
30228 KB |
Output is correct |
58 |
Correct |
29 ms |
9056 KB |
Output is correct |
59 |
Correct |
49 ms |
16136 KB |
Output is correct |