-
Notifications
You must be signed in to change notification settings - Fork 1
/
1065.cpp
87 lines (72 loc) · 1.89 KB
/
1065.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
#include<bits/stdc++.h>
using namespace std;
#define __ ios_base::sync_with_stdio(0); cin.tie(0);
#define endl '\n'
#define foreach(it, x) for (__typeof (x).begin() it = (x).begin(); it != (x).end(); ++it)
#define all(x) x.begin(),x.end()
#define D(x) cout << #x " = " << (x) << endl;
template <class T> string toStr(const T &x)
{ stringstream s; s << x; return s.str(); }
template <class T> int toInt(const T &x)
{ stringstream s; s << x; int r; s >> r; return r; }
int dx[8] = {-1,-1,-1,0,1,1, 1, 0};
int dy[8] = {-1, 0, 1,1,1,0,-1,-1};
const int MN = 2;
const int mod = 10000;
struct matrix {
int r, c;
int m[MN][MN];
matrix (int _r, int _c) : r (_r), c (_c) {
memset(m, 0, sizeof m);
}
void print() {
for (int i = 0; i < r; ++i) {
for (int j = 0; j < c; ++j)
cout << m[i][j] << " ";
cout << endl;
}
}
int x[MN][MN];
matrix & operator *= (const matrix &o) {
memset(x, 0, sizeof x);
for (int i = 0; i < r; ++i)
for (int k = 0; k < c; ++k)
if (m[i][k] != 0)
for (int j = 0; j < c; ++j) {
x[i][j] = (x[i][j] + ((m[i][k] * o.m[k][j]) % mod) ) % mod;
}
memcpy(m, x, sizeof(m));
return *this;
}
};
void matrix_pow(matrix b, long long e, matrix &res) {
memset(res.m, 0, sizeof res.m);
for (int i = 0; i < b.r; ++i)
res.m[i][i] = 1;
if (e == 0) return;
while (true) {
if (e & 1) res *= b;
if ((e >>= 1) == 0) break;
b *= b;
}
}
int main() {
int T;
scanf("%d", &T);
int v[4] = {10, 100, 1000, 10000};
matrix mat(2, 2);
mat.m[0][0] = mat.m[1][0] = mat.m[0][1] = 1;
matrix mat2(2, 2);
int TC = 1;
while (T --> 0) {
int a, b, n, m;
scanf("%d %d %d %d", &a, &b, &n, &m);
int ans = a;
if (n > 0) {
matrix_pow(mat, n - 1, mat2);
ans = ((mat2.m[0][0]* b) + (mat2.m[0][1] * a)) % v[m - 1];
}
printf("Case %d: %d\n", TC++, ans);
}
return 0;
}