728x90
2008년 Google code jam round A에 나왔던 문제이다. 이 문제에서는 매우 정밀한 수의 연산을 요구하기 때문에 double이나 float, Decimal 등을 이용한 연산이 옳은 결과를 내지 못하도록 설계되어 있다.
Solution
먼저,
이제,
이 포스팅에서 처음 도출한 식인
이제
그러므로,
이를 행렬로 표현하여 나타냄으로서 빠르게
행렬의 거듭제곱은 분할 정복을 통해
정답 코드
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
const int mod=1000;
void solve(){
int n;cin>>n;
int A[2][2]={{3,5},{1,3}},B[2][2]={{3,5},{1,3}};n--;
while(n){
int a,b,c,d;
if(n&1){
a=(B[0][0]*A[0][0]+B[0][1]*A[1][0])%mod;
b=(B[0][0]*A[0][1]+B[0][1]*A[1][1])%mod;
c=(B[1][0]*A[0][0]+B[1][1]*A[1][0])%mod;
d=(B[1][0]*A[0][1]+B[1][1]*A[1][1])%mod;
B[0][0]=a;B[0][1]=b;B[1][0]=c;B[1][1]=d;
}
a=(A[0][0]*A[0][0]+A[0][1]*A[1][0])%mod;
b=(A[0][0]*A[0][1]+A[0][1]*A[1][1])%mod;
c=(A[1][0]*A[0][0]+A[1][1]*A[1][0])%mod;
d=(A[1][0]*A[0][1]+A[1][1]*A[1][1])%mod;
A[0][0]=a;A[0][1]=b;A[1][0]=c;A[1][1]=d;
n>>=1;
}
ll x=(B[0][0]*2+999)%mod;
if(x<10)cout<<"00";
else if(x<100)cout<<"0";
cout<<x;
}
int main(){
ios_base::sync_with_stdio(false);cin.tie(NULL);
int T;cin>>T;
for(int i=1;i<=T;i++){
cout<<"Case #"<<i<<": ";
solve();
cout<<"\n";
}
return 0;
}
728x90
'BAEKJOON' 카테고리의 다른 글
BAEKJOON 1941번: 소문난 칠공주 (0) | 2023.02.23 |
---|---|
BAEKJOON 16367번: TV Show Game (0) | 2023.02.22 |
BAEKJOON 구슬 탈출 1~3 (0) | 2023.02.14 |
BAEKJOON 14426번: 접두사 찾기 (feat. Trie) (0) | 2023.02.02 |
BAEKJOON 2981번: 검문 (0) | 2023.01.24 |