-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFibonacci.c
More file actions
53 lines (50 loc) · 1.46 KB
/
Copy pathFibonacci.c
File metadata and controls
53 lines (50 loc) · 1.46 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
#include<stdio.h>
int fibonacci(int n);
int main(void){
int a;
int i = 0,j = 0,f = 0;
int store[1000] = {0};
while ((scanf("%d", &a) != EOF) && (a != 0)){
store[i] = a;
i++;
}
for (j = 0; j < 1000; j++){
if (store[j] == 0) break;
f = fibonacci(store[j]);
printf("%d\n", f);
}
return 0;
}
int fibonacci(int n){
int matrixoe[2] = {0, 1};
int matrixtw[4] = {0, 1, 1, 1};
int matrixth[4] = {1, 0, 1, 0};
int matrixtemp[4] = {0, 1, 1, 1};
while (n > 0){
if (n % 2 == 1){
matrixtemp[0] = (matrixth[0] * matrixtw[0] + matrixth[1] * matrixtw[2]) % 1997;
matrixtemp[1] = (matrixth[0] * matrixtw[1] + matrixth[1] * matrixtw[3]) % 1997;
matrixtemp[2] = (matrixth[2] * matrixtw[0] + matrixth[3] * matrixtw[2]) % 1997;
matrixtemp[3] = (matrixth[2] * matrixtw[1] + matrixth[3] * matrixtw[3]) % 1997;
for(int i = 0; i < 4; i++) matrixth[i] = matrixtemp[i];
}
n = n / 2;
matrixtemp[0] = (matrixtw[0] * matrixtw[0] + matrixtw[1] * matrixtw[2]) % 1997;
matrixtemp[1] = (matrixtw[0] * matrixtw[1] + matrixtw[1] * matrixtw[3]) % 1997;
matrixtemp[2] = (matrixtw[2] * matrixtw[0] + matrixtw[3] * matrixtw[2]) % 1997;
matrixtemp[3] = (matrixtw[2] * matrixtw[1] + matrixtw[3] * matrixtw[3]) % 1997;
for(int i = 0; i < 4; i++) matrixtw[i] = matrixtemp[i];
}
return matrixth[1];
}
/*
long long fibonacci(int n){
long long sum = 1, temp1 = 1,temp2 = 1;
for(int i = 3; i <= n; i++){
temp1 = temp2;
temp2 = sum;
sum = temp1 + temp2;
}
return sum;
}
*/