254 words
1 minutes
[BOJ] 피보나치 수의 합
TimeLimit | MemoryLimit | Condition | TAG |
---|---|---|---|
2s | 128MB | 1 ≤ a ≤ b ≤ 9,000,000,000,000,000,000 | Math, Dynamic Programming |
저번에 풀었던 문제의 약간 쉬운 버전이네요..
약간 실수할 수 있는건 음수에 관한건데 조금 생각하면 금방 ac 할 수 있어요!
정답 코드
#include <iostream>
#include <queue>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <sstream>
#include <iomanip>
#include <string>
#include <numeric>
#include <unordered_set>
#include <unordered_map>
#include <climits>
#include <math.h>
using namespace std;
using ll = unsigned long long;
using ld = long double;
//ll MOD = 1'000'000'007;
ll MOD = 1'000'000'000;
//ll MOD = 1.8446744074E19;
map<ll, ll> dp;
ll solution(ll n){
if(dp.find(n) != dp.end()) return dp[n];
ll result;
ll a, b;
a = solution(n/2);
b = solution(n/2+1+(-2 *!(n%2)));
if(n%2) result = ((a * a)%MOD + (b * b)%MOD)%MOD;
else result = ((((2*b)%MOD + a)%MOD)*a)%MOD;
dp[n] = result;
return result;
}
int main() {
ll A, B;
cin >> A >> B;
dp[0] = 0;
dp[1] = 1;
dp[2] = 1;
dp[3] = 2;
ll sumA = solution(A+1);
ll sumB = solution(B+2);
// 이게 F_n*F_(n+1)이여서
// 아 생각해보니까 dp자체에서 mod를 쓰니까 더 작을 수 있구나..
if(sumB < sumA) cout << (sumB - sumA + MOD) % MOD;
else cout << (((sumB - sumA))%MOD);
return 0;
}
[BOJ] 피보나치 수의 합
https://compy07.github.io/Blog/posts/boj/2086/