-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathFinalCBMatrixExponentiation.cpp
More file actions
136 lines (83 loc) · 2.25 KB
/
FinalCBMatrixExponentiation.cpp
File metadata and controls
136 lines (83 loc) · 2.25 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
//Codersbit Final question on Matrix Exponentiation
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define sf scanf
#define pf printf
#define f first
//#define s second
#define clr(x,y) memset(x,y,sizeof x)
#define LL long long
#define mx 100009
long long mod = 1000000007;
// #define LL long long
vector<vector<LL> > multiply(vector<vector<LL> >g1, vector<vector<LL> >g2)
{
vector<vector<LL> >mul(g1.size(), vector<LL> (g1.size(), 0));
for (LL i = 1; i < g1.size(); i++)
{
for (LL j = 1; j < g1.size(); j++)
{
mul[i][j] = 0;
for (LL k = 1; k < g1.size(); k++)
mul[i][j] = (mul[i][j] + g1[i][k] * g2[k][j]) % mod;
}
}
return mul;
}
LL solve(LL A, LL B) {
vector<LL> cnt(B + 1, 0);
vector<vector<LL> > tot(B + 1, vector<LL>(B + 1, 0));
vector<vector<LL> > fin(B + 1, vector<LL>(B + 1, 0));
for (LL i = 1; i <= B; i++) {
for (LL j = 1; j <= B; j++) {
if (__gcd(i, j) == 1) {
cnt[i]++;
tot[i][j] = 1;
}
// cout << tot[i][j] << " ";
}
fin[i][i] =1;
// cout << endl;
}
vector<LL> bin;
LL n = A-1;
// A==1 i.e ans is B then
if(n==0){
return B;
}
while (n != 0) {
bin.pb(n % 2);
n = n / 2;
}
// for(auto i:bin)
// cout << i << " ";
// cout << endl;
vector<vector<LL> > g = tot;
for (LL i = 0; i < bin.size(); i++) {
if (bin[i] != 0 ) {
fin = multiply(fin,g);
}
g = multiply(g, g);
}
long long val = 0;
for (LL i = 0; i < g.size(); i++)
{
for (LL j = 0; j < g.size(); j++)
{
val = (val + fin[i][j] ) % mod;
}
}
return val;
}
main() {
// LL start_s = clock();
ios_base::sync_with_stdio(false);
cin.tie(NULL);
LL a, b;
cin >> a >> b;
cout << solve(a, b) << endl;
// LL stop_s = clock();
// cout << "time: " << (stop_s-start_s)/double(CLOCKS_PER_SEC) << endl;
return 0;
}