-
Notifications
You must be signed in to change notification settings - Fork 0
/
Count Binary Strings With No Consecutive 1s.cpp
66 lines (58 loc) · 1.37 KB
/
Count Binary Strings With No Consecutive 1s.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
//{ Driver Code Starts
// Initial Template for C++
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
// User function Template for C++
class Solution {
public:
long long int mod = 1e9+7;
vector<vector<int>> multiply(vector<vector<int>> A1,vector<vector<int>> A2)
{
vector<vector<int>> res = {{0,0},{0,0}};
for(int k=0;k<2;k++)
{
for(int i=0;i<2;i++)
{
long long int sum = 0;
for(int j=0;j<2;j++)
{
sum += ((long long int)A1[k][j]*A2[j][i]);
}
res[k][i] = sum%mod;
}
}
return res;
}
int pow(vector<vector<int>> &A,long long int N)
{
vector<vector<int>> res = {{1,1},{1,1}};
while(N)
{
if(N%2)
{
res = multiply(res,A);
N--;
}
A = multiply(A,A);
N /= 2;
}
return res[0][1]%mod;
}
int countStrings(long long int N) {
vector<vector<int>> A = {{1,1},{1,0}};
return pow(A,N+1);
}
};
//{ Driver Code Starts.
int main() {
int t;
cin >> t;
while (t--) {
long long int N;
cin >> N;
Solution obj;
cout << obj.countStrings(N) << endl;
}
}
// } Driver Code Ends