알고리즘 문제풀이/백준
[백준 12925번] Numbers
슥지니
2020. 3. 1. 05:58
반응형
문제 링크: https://www.acmicpc.net/problem/12925
12925번: Numbers
각 Test case에 대해, “Case #c: x”의 형식으로 각 줄에 정답을 출력한다. c는 Test Case의 번호이다. (1부터 매겨진다.) x는 해당 Test Case의 정답이다.
www.acmicpc.net
<문제 풀이> 수학, 선형대수학, 행렬 거듭제곱
https://seokjin2.tistory.com/13
[백준 12728번] n제곱 계산
문제 링크: https://www.acmicpc.net/problem/12728 12728번: n제곱 계산 이 문제에서 숫자 (3 + √5)n 에 대한 소수점 앞에 마지막 세 자리를 찾아야합니다. 예를 들어, n = 5 일 때 (3 + √5)5 = 3935..
seokjin2.tistory.com
[백준 12728번]랑 똑같은 문제
<C++ 소스코드>
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;
typedef long long ll;
typedef vector<vector<ll> > matrix;
matrix operator * (const matrix &a, const matrix &b) {
ll size = a.size();
matrix res(size, vector<ll>(size));
for (ll i = 0; i < size; i++) {
for (ll j = 0; j < size; j++) {
for (ll k = 0; k < size; k++) {
res[i][j] += a[i][k] * b[k][j];
}
res[i][j] %= 1000;
}
}
return res;
}
matrix power(matrix a, ll n) {
ll size = a.size();
matrix res(size, vector<ll>(size));
for (ll i = 0; i < size; i++) { // 단위 행렬
res[i][i] = 1;
}
while (n > 0) {
if (n % 2 == 1) {
res = res * a;
}
n /= 2;
a = a * a;
}
return res;
}
int main(void) {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int test_case;
cin >> test_case;
for(int i=1; i<= test_case; i++){
ll n;
cin >> n;
matrix a = { {6,-4}, {1, 0} };
matrix res = power(a, n - 1);
string ans = to_string((((28 * res[1][0] + 6 * res[1][1]) - 1) % 1000 + 1000) % 1000);
ll size = ans.size();
while (true) {
if (size == 3) break;
ans = "0" + ans;
size++;
}
cout << "Case #" << i << ": " << ans << '\n';
}
return 0;
}
|
cs |
반응형