0

This is the piece of code I've got in a Russian website, I've tried to cout the pair function but all of my attempt failed. Please guide me how to do this.

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<char> vc;
typedef vector<ll> vll;
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef vector<ii> vii;

pair<int, int> fib (int n) {
    if (n == 0)
        return {0, 1};

    auto p = fib(n >> 1);
    int c = p.first * (2 * p.second - p.first);
    int d = p.first * p.first + p.second * p.second;
    if (n & 1)
        return {d, c + d};
    else
        return {c, d};
}

int main() {
    cin.tie(0);
    cout.tie(0);
    ios_base::sync_with_stdio(0);

    int n;
    cin >> n;
    return 0;
}
Blackdark
  • 33
  • 4
  • 7
    A better alternative would be to pick a [good C++ textbook](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) instead of some random web site in order to learn C++. The code you saw on that random web site demonstrates many bad programming practices, like using non-standard header files, obfuscating typedefs, and several others. You should avoid writing code that looks like this. A job interview candidate who would write out something like that, is unlikely to get the job. – Sam Varshavchik Apr 03 '21 at 14:15

1 Answers1

4

You have to get the result in the pair and then print using first and second property.

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<char> vc;
typedef vector<ll> vll;
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef vector<ii> vii;

pair<int, int> fib (int n) {
    if (n == 0)
        return {0, 1};

    auto p = fib(n >> 1);
    int c = p.first * (2 * p.second - p.first);
    int d = p.first * p.first + p.second * p.second;
    if (n & 1)
        return {d, c + d};
    else
        return {c, d};
}

int main() {
    cin.tie(0);
    cout.tie(0);
    ios_base::sync_with_stdio(0);

    int n;
    cin >> n;
    pair<int, int> res = fib(n);
    cout << res.first << " " << res.second << endl;
    return 0;
}
starboy_jb
  • 899
  • 1
  • 6
  • 13