Notice
Recent Posts
Recent Comments
Link
Little Jay
[C++] 백준 9471번 피사노 주기 본문
백준 2749번 피보나치3을 풀기 전에 이 문제를 풀고가야 한다는 것을 알게 되었다
피사노 주기라는 것은 피보나치 수열을 m으로 나눈 나머지가 주기를 이룬다는 것을 의미한다.
주기를 생각할 때 다시 돌아오는 것은 1, 그 전의 것은 0이 되면 된다.
그리고 list에 넣을 때 피보나치 수를 직접 넣는 것이 아니라 나머지 연산이 된 값을
집어넣어야 하는 것을 주의하면 될 것 같다.
#include <iostream>
#include <vector>
using namespace std;
int main() {
int t;
int n, m;
cin >> t;
for (int i = 0; i < t; i++) {
cin >> n >> m;
vector<int> fibo_list;
fibo_list.push_back(0);
fibo_list.push_back(1);
fibo_list.push_back(1);
int temp = 3;
while (true) {
fibo_list.push_back((fibo_list.at(temp - 1) + fibo_list.at(temp - 2)) % m);
if (fibo_list.at(temp - 1) % m == 0 && fibo_list.at(temp - 2) % m == 1)
break;
temp++;
}
cout << n << " " << temp - 1 << "\n";
}
return 0;
}'알고리즘 > BOJ' 카테고리의 다른 글
| [C++] 백준 1436번 영화감독 숌 (0) | 2021.07.23 |
|---|---|
| [C++] 백준 2749번 피보나치 수 3 (0) | 2021.07.23 |
| [C++] 백준 1920 수 찾기 (0) | 2021.07.18 |
| [C++] 백준 15829번 Hashing (0) | 2021.07.18 |
| [C++] 백준 10816번 숫자 카드 2 (0) | 2021.07.18 |
Comments