forked from marioyc/Online-Judge-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3690 - Choosing number.cpp
53 lines (39 loc) · 1023 Bytes
/
3690 - Choosing number.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
#include <cstdio>
using namespace std;
#define MOD 1000000007
struct Matrix{
long long a,b,c,d;
Matrix(){
a = d = 1;
b = c = 0;
}
Matrix(long long _a, long long _b, long long _c, long long _d):
a(_a),b(_b),c(_c),d(_d){}
Matrix operator * (Matrix X){
return Matrix(
(a * X.a + b * X.c) % MOD,
(a * X.b + b * X.d) % MOD,
(c * X.a + d * X.c) % MOD,
(c * X.b + d * X.d) % MOD);
}
Matrix pow(int n){
Matrix ret,a = *this;
while(n){
if(n & 1) ret = ret * a;
a = a * a;
n >>= 1;
}
return ret;
}
};
int main(){
int N,M,K;
Matrix M0;
while(scanf("%d %d %d",&N,&M,&K) == 3){
M0.a = M - K; M0.b = M - K;
M0.c = K; M0.d = K - 1;
M0 = M0.pow(N - 1);
printf("%lld\n",((M0.a + M0.c) * (M - K) + (M0.b + M0.d) * K) % MOD);
}
return 0;
}