Cod sursa(job #2711925)
| Utilizator | Data | 24 februarie 2021 21:30:23 | |
|---|---|---|---|
| Problema | Al k-lea termen Fibonacci | Scor | 0 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 0.45 kb |
#include <bits/stdc++.h>
using namespace std;
pair <int, int> fib(int n) {
if (!n) return {0, 1};
auto p = fib(n >> 1);
int c = p.first * (2 * p.second - p.first),
d = p.first * p.first + p.second * p.second;
return (n & 1) ? {d, c + d} : {c, d};
}
int main() {
ifstream fin("kfib.in");
ofstream fout("kfib.out");
int n;
fin >> n;
auto ans = fib(n);
fout << ans.first;
return 0;
}