Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- 오에스
- 브루트포스
- c++
- 구현
- 코딩
- 정석
- 컴퓨터공학과
- Computer science
- coding
- Operating System
- 너비우선탐색
- 컴공
- 컴공과
- 백준
- 문제풀이
- 코테
- Stack
- 그래프
- 자료구조
- OS
- 북리뷰
- cs
- DP
- 스택
- 정석학술정보관
- vector
- 개발
- 알고리즘
- bfs
- 오퍼레이팅시스템
Archives
- Today
- Total
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