-
Notifications
You must be signed in to change notification settings - Fork 0
/
climbing_stairs.cpp
133 lines (96 loc) · 1.87 KB
/
climbing_stairs.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
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
#include <iostream>
#define mod 1000000007;
using namespace std;
void power_n( long long int arr[][2], long long int sol[][2], long long int n );
void multiply( long long int a[][2], long long int b[][2], long long int c[][2] ) {
long long int d[2][2], e[2][2];
for ( int i = 0; i < 2; i++ ) {
for ( int j = 0; j < 2; j++ ) {
e[i][j] = b[i][j];
d[i][j] = a[i][j];
}
}
for ( int i = 0; i < 2; i++ ) {
for ( int j = 0; j < 2; j++ ) {
int sum = 0;
for ( int k = 0; k < 2; k++ ) {
sum = sum % mod ;
sum = sum + ( d[i][k] * e[k][j] ) % mod ;
}
c[i][j] = sum % mod ;
}
}
}
long long int fib( long long int n ) {
long long int a[2][2] = { 1, 1, 1, 0 };
long long int c[2][2];
power_n(a, c, n );
return c[0][0];
}
/* long long int a = 1;
long long int b = 2;
long long int k = 3;
long long int c = a + b;
if ( n == 1)
return a;
if ( n == 2 )
return b;
while ( k <= n ) {
c = a + b;
a = b;
b = c;
k++;
}
return c % mod;
*/
void power_n( long long int arr[][2], long long int sol[][2], long long int n ) {
if ( n == 1 ) {
sol[0][0] = arr[0][0];
sol[0][1] = arr[0][1];
sol[1][0] = arr[1][0];
sol[1][1] = arr[1][1];
return;
}
if ( n == 2 ) {
multiply( arr, arr, sol );
return;
}
else if ( n % 2 == 0 ) {
power_n( arr, sol, n/2 );
multiply( sol, sol, sol);
} else {
power_n(arr, sol, n - 1);
multiply(arr, sol, sol );
}
return;
}
long long int no_of_ones( long long int n ) {
long long int sol = 0;
while ( n > 1 ) {
if ( n % 2 == 1 ) {
sol++;
}
n = n / 2;
}
if ( n == 1 ) {
sol++;
}
return sol;
}
int main()
{
int t;
cin >> t;
for ( int i = 0; i < t; i++ ) {
long long int k;
cin >> k;
long long int g;
cin >> g;
if ( g == no_of_ones( fib(k) ) ) {
cout << "CORRECT" << endl;
} else {
cout << "INCORRECT" << endl;
}
}
return 0;
}