-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfibonacciAgain.cpp
More file actions
59 lines (50 loc) · 1.1 KB
/
fibonacciAgain.cpp
File metadata and controls
59 lines (50 loc) · 1.1 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
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
typedef struct Matrix Matrix;
struct Matrix{
int mx[2][2];
void Out() {
for(int i = 0; i < 2; ++i) {
for(int j = 0; j < 2; ++j)
printf("%d ",mx[i][j]);
puts("");
}
}
void Init() {
mx[0][0] = mx[0][1] = mx[1][0] = 1;
mx[1][1] = 0;
}
void Emp() {
mx[0][0] = mx[1][1] = 1;
mx[0][1] = mx[1][0] = 0;
}
Matrix operator * (const Matrix a)const {
Matrix x;
memset(x.mx,0,sizeof(x.mx));
for(int i = 0; i < 2; ++i)
for(int j = 0; j < 2; ++j)
for(int k = 0; k < 2; ++k)
x.mx[i][j] = (x.mx[i][j]+mx[i][k]*a.mx[k][j])%3;
return x;
}
};
bool pow(int n) {
Matrix a,b;
a.Emp();
b.Init();
while(n) {
if(n&1) a = a*b;
b = b*b;
n >>= 1;
}
return !((a.mx[0][0]*7+a.mx[0][1])%3);
}
int main() {
int n;
while(cin >> n && n) {
cout << (pow(n) ? "yes" : "no") << endl;
}
return 0;
}