#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> v;
typedef vector<v> vv;
typedef vector<vv> vvv;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<vvb> vvvb;
typedef pair<ll,ll> p;
typedef vector<p> vp;
typedef vector<vp> vvp;
typedef vector<vvp> vvvp;
typedef pair<ll, p> tri;
typedef vector<tri> vtri;
typedef vector<vtri> vvtri;
typedef vector<vvtri> vvvtri;
#define f first
#define s second
#define pb push_back
#define eb emplace_back
#define all(v) (v).begin(),(v).end()
const ll INF = 1e18;
const ll mod = 1e9 + 7;
vv multiply(vv &mat1, vv &mat2)
{
vv res(mat1.size(), v(mat2[0].size()));
for (ll i= 0; i < mat1.size(); i++)
{
for (ll j = 0; j < mat2[0].size(); j++)
{
for (ll k = 0; k < mat2.size(); k++)
{
res[i][j] += (mat1[i][k] * mat2[k][j]) % mod;
}
res[i][j] %= mod;
}
}
return res;
}
vv matPow(vv &mat, ll exp)
{
if (exp == 0)
{
vv id(mat.size(), v(mat.size(), 0));
for (ll i= 0; i <mat.size(); i++) id[i][i] = 1;
return id;
}
vv res = matPow(mat, exp / 2);
res = multiply(res, res);
if (exp % 2 == 1) res = multiply(res, mat);
return res;
}
int main()
{
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
ll R, C, Q;
cin >> R >> C >> Q;
vv matrix(C, v(C, 0));
for (ll i = 0; i < C; i++)
{
matrix[i][i] = 1;
if (i > 0) matrix[i][i - 1] = 1;
if (i < C - 1) matrix[i][i + 1] = 1;
}
vv powered = matPow(matrix, R - 1);
for (ll iter = 0; iter < Q; iter++)
{
char T;
cin >> T;
ll from, to;
cin >> from >> to;
if (T == 'K')
{
vv vec(C, v(1, 0)); vec[from - 1][0] = 1;
vv res = multiply(powered, vec);
cout << R - 1 << " " << res[to - 1][0] << "\n";
}
else
{
cout << "-1 -1\n";
}
}
return 0;
}
Compilation message
chessrush.cpp: In function 'vv multiply(vv&, vv&)':
chessrush.cpp:33:22: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::vector<long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
33 | for (ll i= 0; i < mat1.size(); i++)
| ~~^~~~~~~~~~~~~
chessrush.cpp:35:26: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
35 | for (ll j = 0; j < mat2[0].size(); j++)
| ~~^~~~~~~~~~~~~~~~
chessrush.cpp:37:30: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::vector<long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
37 | for (ll k = 0; k < mat2.size(); k++)
| ~~^~~~~~~~~~~~~
chessrush.cpp: In function 'vv matPow(vv&, ll)':
chessrush.cpp:52:26: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::vector<long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
52 | for (ll i= 0; i <mat.size(); i++) id[i][i] = 1;
| ~~^~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
464 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
21 ms |
704 KB |
Output is correct |
3 |
Correct |
14 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
21 ms |
704 KB |
Output is correct |
3 |
Correct |
14 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
4 ms |
344 KB |
Output is correct |
6 |
Correct |
4 ms |
348 KB |
Output is correct |
7 |
Correct |
31 ms |
668 KB |
Output is correct |
8 |
Correct |
50 ms |
600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
21 ms |
704 KB |
Output is correct |
3 |
Correct |
14 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
4 ms |
344 KB |
Output is correct |
6 |
Correct |
4 ms |
348 KB |
Output is correct |
7 |
Correct |
31 ms |
668 KB |
Output is correct |
8 |
Correct |
50 ms |
600 KB |
Output is correct |
9 |
Correct |
5 ms |
348 KB |
Output is correct |
10 |
Correct |
7 ms |
344 KB |
Output is correct |
11 |
Correct |
123 ms |
688 KB |
Output is correct |
12 |
Correct |
121 ms |
688 KB |
Output is correct |
13 |
Correct |
5 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
21 ms |
704 KB |
Output is correct |
3 |
Correct |
14 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
4 ms |
344 KB |
Output is correct |
6 |
Correct |
4 ms |
348 KB |
Output is correct |
7 |
Correct |
31 ms |
668 KB |
Output is correct |
8 |
Correct |
50 ms |
600 KB |
Output is correct |
9 |
Correct |
5 ms |
348 KB |
Output is correct |
10 |
Correct |
7 ms |
344 KB |
Output is correct |
11 |
Correct |
123 ms |
688 KB |
Output is correct |
12 |
Correct |
121 ms |
688 KB |
Output is correct |
13 |
Correct |
5 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
5 ms |
492 KB |
Output is correct |
16 |
Correct |
5 ms |
348 KB |
Output is correct |
17 |
Execution timed out |
2088 ms |
23900 KB |
Time limit exceeded |
18 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |