티스토리 뷰
17435번: 합성함수와 쿼리
함수 f : {1, 2, ..., m}→{1, 2, ..., m}이 있다. 이때 fn : {1, 2, ..., m}→{1, 2, ..., m}을 다음과 같이 정의하자. f1(x) = f(x) fn+1(x) = f(fn(x)) 예를 들어 f4(1) = f(f(f(f(1))))이다. n과 x가 주어질 때 fn(x)를 계산하는
www.acmicpc.net
백준 소스코드 [C++] 17435 합성함수와 쿼리
#include <iostream>
#include <algorithm>
#include <queue>
#include <string.h>
#include <limits.h>
#include <vector>
#include <math.h>
#include <stack>
#include <bitset>
#include <string>
#include <set>
#include <map>
#include <unordered_map>
#include <sstream>
#include <cstdlib>
#define all(v) v.begin(), v.end()
#define pii pair<int, int>
#define make_unique(v) v.erase(unique(v.begin(), v.end()), v.end())
typedef long long ll;
using namespace std;
int table[500001][22];
int m, q, n, x;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> m;
for (int i = 1; i <= m; i++)
cin >> table[i][0];
for (int j = 1; j < 22; j++)
for (int i = 1; i <= m; i++)
table[i][j] = table[table[i][j - 1]][j - 1];
cin >> q;
for (int i = 1; i <= q; i++) {
cin >> n >> x;
for (int j = 0; j < 22; j++)
if (n & (1<<j)) x = table[x][j];
cout << x << "\n";
}
return 0;
}
'백준' 카테고리의 다른 글
백준 소스코드 [C++] 1208 부분수열의 합 2 (0) | 2021.04.02 |
---|---|
백준 소스코드 [C++] 14225 부분수열의 합 (0) | 2021.04.02 |
백준 소스코드 [C++] 1162 도로포장 (0) | 2021.03.30 |
백준 소스코드 [C++] 11952 좀비 (0) | 2021.03.30 |
백준 소스코드 [C++] 17835 면접보는 승범이네 (0) | 2021.03.30 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
TAG
- 최단경로문제
- rxswift
- WWDC19
- WWDC16
- IOS
- CompositionalLayout
- 코딩대회
- 포드 풀커슨 알고리즘
- WWDC17
- 네트워크 플로우
- 벨만포드 알고리즘
- HIG
- Testable
- 부스트캠프 6기
- 최단경로 문제
- observeOn
- 다익스트라 시간복잡도
- 에드몬드 카프 알고리즘
- 컴퓨터 추상화
- State Restoration
- WWDC21
- 벨만포드 시간복잡도
- CPU와 Memory
- 네트워크 유량
- 최단경로 알고리즘
- 최대 매칭
- mach-o
- test coverage
- 강한 순환 참조
- MeTal
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함