백준
백준 소스코드 [C++] 1788 피보나치 수의 확장
Hani_Levenshtein
2020. 10. 30. 18:39
1788번: 피보나치 수의 확장
첫째 줄에 F(n)이 양수이면 1, 0이면 0, 음수이면 -1을 출력한다. 둘째 줄에는 F(n)의 절댓값을 출력한다. 이 수가 충분히 커질 수 있으므로, 절댓값을 1,000,000,000으로 나눈 나머지를 출력한다.
www.acmicpc.net
백준 소스코드 [C++] 1788 피보나치 수의 확장
#include <iostream>
#include <algorithm>
#include <queue>
#include <string.h>
#include <limits.h>
#include <vector>
#include <math.h>
#include <stack>
#include <bitset>
#include <string>
typedef long long ll;
using namespace std;
int main() {
ios_base::sync_with_stdio(false), cin.tie(nullptr);
int n;
cin >> n;
int first = 0, second = 1, res = 1;
for (int i = 1;i < abs(n);i++) {
res = (first+second) % 1000000000;
first = second;
second = res;
}
if (n == 0) cout << "0\n0";
else if(n%2==0 && n<0)cout << "-1\n"<<res << '\n';
else cout << "1\n"<<res << '\n';
return 0;
}