-
Notifications
You must be signed in to change notification settings - Fork 2
/
UVA 10518 - How Many Calls?.cpp
67 lines (57 loc) · 1.4 KB
/
UVA 10518 - How Many Calls?.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
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define inf 1000000000
#define ii pair<int, int>
#define vi vector<int>
#define vl vector<long long>
#define vii vector<pair<int, int> >
#define vvi vector< vector<int> >
#define vvl vector< vector<long long> >
#define vvii vector< vector< pair<int, int > > >
ll mod;
vvl multiply(vvl a, vvl b)
{
vvl ret(2, vl(2) );
for(int i=0; i<2; i++){
for(int j=0; j<2; j++){
ret[i][j] = 0;
for(int k=0; k<2; k++)
ret[i][j] = (ret[i][j] + a[i][k] * b[k][j]) % mod;
}
}
return ret;
}
vvl mat_pow(vvl a, ll e)
{
if(e == 1) return a;
vvl ret = mat_pow(a, e / 2);
ret = multiply(ret, ret);
if(e % 2){
vvl temp(2, vl(2, 1) );
temp[1][1] = 0;
ret = multiply(ret, temp);
}
return ret;
}
int main()
{
ll n;
int tc = 1, f[3] = {1, 1, 3};
while(cin>>n>>mod){
if(!n && !mod) return 0;
if(n <= 2){
printf("Case %d: %lld %d %lld\n", tc++, n, mod, f[n] % mod);
continue;
}
vvl t(2, vl(2, 1) );
t[1][1] = 0;
vl c(2, 1);
vvl b = mat_pow(t, n - 1);
ll ans = 0;
for(int i=0; i<2; i++)
ans = (ans + b[0][i] * c[i] ) % mod;
ans = ( (ans * 2) % mod - 1 + mod ) % mod;
printf("Case %d: %lld %d %lld\n", tc++, n, mod, ans);
}
}