-
[10826] 피보나치 수 4BOJ 2021. 12. 18. 14:10
https://www.acmicpc.net/problem/10826
10826번: 피보나치 수 4
피보나치 수는 0과 1로 시작한다. 0번째 피보나치 수는 0이고, 1번째 피보나치 수는 1이다. 그 다음 2번째 부터는 바로 앞 두 피보나치 수의 합이 된다. 이를 식으로 써보면 Fn = Fn-1 + Fn-2 (n ≥ 2)가
www.acmicpc.net
<문제>
큰수연산을 요하는 문제이고, geeksforgeeks에서 구현체를 가져왔다.
i번째 피보나치 수를 x에,
i+1번째 피보나치 수를 y에 넣어두었다면 sum=(x+y), x=y, y=(x+y)로 피보나치를 이어갈 수 있겠다.
<소스코드>
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426// C++ program to implement// the above approach#include <bits/stdc++.h>using namespace std;class BigInt {string digits;public:// Constructors:BigInt(unsigned long long n = 0);BigInt(string &);BigInt(const char *);BigInt(BigInt &);// Helper Functions:friend void divide_by_2(BigInt &a);friend bool Null(const BigInt &);friend int Lenght(const BigInt &);int operator[](const int) const;/* * * * Operator Overloading * * * */// Direct assignmentBigInt &operator=(const BigInt &);// Post/Pre - IncrementationBigInt &operator++();BigInt operator++(int temp);BigInt &operator--();BigInt operator--(int temp);// Addition and Subtractionfriend BigInt &operator+=(BigInt &, const BigInt &);friend BigInt operator+(const BigInt &, const BigInt &);friend BigInt operator-(const BigInt &, const BigInt &);friend BigInt &operator-=(BigInt &, const BigInt &);// Comparison operatorsfriend bool operator==(const BigInt &, const BigInt &);friend bool operator!=(const BigInt &, const BigInt &);friend bool operator>(const BigInt &, const BigInt &);friend bool operator>=(const BigInt &, const BigInt &);friend bool operator<(const BigInt &, const BigInt &);friend bool operator<=(const BigInt &, const BigInt &);// Multiplication and Divisionfriend BigInt &operator*=(BigInt &, const BigInt &);friend BigInt operator*(const BigInt &, const BigInt &);friend BigInt &operator/=(BigInt &, const BigInt &);friend BigInt operator/(const BigInt &, const BigInt &);// Modulofriend BigInt operator%(const BigInt &, const BigInt &);friend BigInt &operator%=(BigInt &, const BigInt &);// Power Functionfriend BigInt &operator^=(BigInt &, const BigInt &);friend BigInt operator^(BigInt &, const BigInt &);// Square Root Functionfriend BigInt sqrt(BigInt &a);// Read and Writefriend ostream &operator<<(ostream &, const BigInt &);friend istream &operator>>(istream &, BigInt &);// Othersfriend BigInt NthCatalan(int n);friend BigInt NthFibonacci(int n);friend BigInt Factorial(int n);};BigInt::BigInt(string &s) {digits = "";int n = s.size();for (int i = n - 1; i >= 0; i--) {if (!isdigit(s[i])) throw("ERROR");digits.push_back(s[i] - '0');}}BigInt::BigInt(unsigned long long nr) {do {digits.push_back(nr % 10);nr /= 10;} while (nr);}BigInt::BigInt(const char *s) {digits = "";for (int i = strlen(s) - 1; i >= 0; i--) {if (!isdigit(s[i])) throw("ERROR");digits.push_back(s[i] - '0');}}BigInt::BigInt(BigInt &a) { digits = a.digits; }bool Null(const BigInt &a) {if (a.digits.size() == 1 && a.digits[0] == 0) return true;return false;}int Lenght(const BigInt &a) { return a.digits.size(); }int BigInt::operator[](const int index) const {if (digits.size() <= index || index < 0) throw("ERROR");return digits[index];}bool operator==(const BigInt &a, const BigInt &b) {return a.digits == b.digits;}bool operator!=(const BigInt &a, const BigInt &b) { return !(a == b); }bool operator<(const BigInt &a, const BigInt &b) {int n = Lenght(a), m = Lenght(b);if (n != m) return n < m;while (n--)if (a.digits[n] != b.digits[n]) return a.digits[n] < b.digits[n];return false;}bool operator>(const BigInt &a, const BigInt &b) { return b < a; }bool operator>=(const BigInt &a, const BigInt &b) { return !(a < b); }bool operator<=(const BigInt &a, const BigInt &b) { return !(a > b); }BigInt &BigInt::operator=(const BigInt &a) {digits = a.digits;return *this;}BigInt &BigInt::operator++() {int i, n = digits.size();for (i = 0; i < n && digits[i] == 9; i++) digits[i] = 0;if (i == n)digits.push_back(1);elsedigits[i]++;return *this;}BigInt BigInt::operator++(int temp) {BigInt aux;aux = *this;++(*this);return aux;}BigInt &BigInt::operator--() {if (digits[0] == 0 && digits.size() == 1) throw("UNDERFLOW");int i, n = digits.size();for (i = 0; digits[i] == 0 && i < n; i++) digits[i] = 9;digits[i]--;if (n > 1 && digits[n - 1] == 0) digits.pop_back();return *this;}BigInt BigInt::operator--(int temp) {BigInt aux;aux = *this;--(*this);return aux;}BigInt &operator+=(BigInt &a, const BigInt &b) {int t = 0, s, i;int n = Lenght(a), m = Lenght(b);if (m > n) a.digits.append(m - n, 0);n = Lenght(a);for (i = 0; i < n; i++) {if (i < m)s = (a.digits[i] + b.digits[i]) + t;elses = a.digits[i] + t;t = s / 10;a.digits[i] = (s % 10);}if (t) a.digits.push_back(t);return a;}BigInt operator+(const BigInt &a, const BigInt &b) {BigInt temp;temp = a;temp += b;return temp;}BigInt &operator-=(BigInt &a, const BigInt &b) {if (a < b) throw("UNDERFLOW");int n = Lenght(a), m = Lenght(b);int i, t = 0, s;for (i = 0; i < n; i++) {if (i < m)s = a.digits[i] - b.digits[i] + t;elses = a.digits[i] + t;if (s < 0)s += 10, t = -1;elset = 0;a.digits[i] = s;}while (n > 1 && a.digits[n - 1] == 0) a.digits.pop_back(), n--;return a;}BigInt operator-(const BigInt &a, const BigInt &b) {BigInt temp;temp = a;temp -= b;return temp;}BigInt &operator*=(BigInt &a, const BigInt &b) {if (Null(a) || Null(b)) {a = BigInt();return a;}int n = a.digits.size(), m = b.digits.size();vector<int> v(n + m, 0);for (int i = 0; i < n; i++)for (int j = 0; j < m; j++) {v[i + j] += (a.digits[i]) * (b.digits[j]);}n += m;a.digits.resize(v.size());for (int s, i = 0, t = 0; i < n; i++) {s = t + v[i];v[i] = s % 10;t = s / 10;a.digits[i] = v[i];}for (int i = n - 1; i >= 1 && !v[i]; i--) a.digits.pop_back();return a;}BigInt operator*(const BigInt &a, const BigInt &b) {BigInt temp;temp = a;temp *= b;return temp;}BigInt &operator/=(BigInt &a, const BigInt &b) {if (Null(b)) throw("Arithmetic Error: Division By 0");if (a < b) {a = BigInt();return a;}if (a == b) {a = BigInt(1);return a;}int i, lgcat = 0, cc;int n = Lenght(a), m = Lenght(b);vector<int> cat(n, 0);BigInt t;for (i = n - 1; t * 10 + a.digits[i] < b; i--) {t *= 10;t += a.digits[i];}for (; i >= 0; i--) {t = t * 10 + a.digits[i];for (cc = 9; cc * b > t; cc--);t -= cc * b;cat[lgcat++] = cc;}a.digits.resize(cat.size());for (i = 0; i < lgcat; i++) a.digits[i] = cat[lgcat - i - 1];a.digits.resize(lgcat);return a;}BigInt operator/(const BigInt &a, const BigInt &b) {BigInt temp;temp = a;temp /= b;return temp;}BigInt &operator%=(BigInt &a, const BigInt &b) {if (Null(b)) throw("Arithmetic Error: Division By 0");if (a < b) {a = BigInt();return a;}if (a == b) {a = BigInt(1);return a;}int i, lgcat = 0, cc;int n = Lenght(a), m = Lenght(b);vector<int> cat(n, 0);BigInt t;for (i = n - 1; t * 10 + a.digits[i] < b; i--) {t *= 10;t += a.digits[i];}for (; i >= 0; i--) {t = t * 10 + a.digits[i];for (cc = 9; cc * b > t; cc--);t -= cc * b;cat[lgcat++] = cc;}a = t;return a;}BigInt operator%(const BigInt &a, BigInt &b) {BigInt temp;temp = a;temp %= b;return temp;}BigInt &operator^=(BigInt &a, const BigInt &b) {BigInt Exponent, Base(a);Exponent = b;a = 1;while (!Null(Exponent)) {if (Exponent[0] & 1) a *= Base;Base *= Base;divide_by_2(Exponent);}return a;}BigInt operator^(BigInt &a, BigInt &b) {BigInt temp(a);temp ^= b;return temp;}void divide_by_2(BigInt &a) {int add = 0;for (int i = a.digits.size() - 1; i >= 0; i--) {int digit = (a.digits[i] >> 1) + add;add = ((a.digits[i] & 1) * 5);a.digits[i] = digit;}while (a.digits.size() > 1 && !a.digits.back()) a.digits.pop_back();}BigInt sqrt(BigInt &a) {BigInt left(1), right(a), v(1), mid, prod;divide_by_2(right);while (left <= right) {mid += left;mid += right;divide_by_2(mid);prod = (mid * mid);if (prod <= a) {v = mid;++mid;left = mid;} else {--mid;right = mid;}mid = BigInt();}return v;}BigInt NthCatalan(int n) {BigInt a(1), b;for (int i = 2; i <= n; i++) a *= i;b = a;for (int i = n + 1; i <= 2 * n; i++) b *= i;a *= a;a *= (n + 1);b /= a;return b;}BigInt NthFibonacci(int n) {BigInt a(1), b(1), c;if (!n) return c;n--;while (n--) {c = a + b;b = a;a = c;}return b;}BigInt Factorial(int n) {BigInt f(1);for (int i = 2; i <= n; i++) f *= i;return f;}istream &operator>>(istream &in, BigInt &a) {string s;in >> s;int n = s.size();for (int i = n - 1; i >= 0; i--) {if (!isdigit(s[i])) throw("INVALID NUMBER");a.digits[n - i - 1] = s[i];}return in;}ostream &operator<<(ostream &out, const BigInt &a) {for (int i = a.digits.size() - 1; i >= 0; i--) cout << (short)a.digits[i];return cout;}// Driver code with some examples// This code is contributed// by Gatea Davidint main(void) {int n;cin >> n;if (n == 0) {cout << 0;return 0;}if (n == 1 || n == 2) {cout << 1;return 0;}int i;BigInt x("1");BigInt y("1");for (i = 0; i < n - 2; i++) {BigInt next;next = x + y;x = y;y = next;}cout << y;return 0;}cs 'BOJ' 카테고리의 다른 글
[1525] 퍼즐 (0) 2021.12.22 [2749] 피보나치 수 3 (0) 2021.12.19 [6543] 그래프의 싱크 (0) 2021.12.17 [1292] 쉽게 푸는 문제 (0) 2021.12.15 [2816] 디지털 티비 (0) 2021.12.14