-
[BOJ] 13172 ΣPS/BOJ 2022. 7. 18. 22:12
https://www.acmicpc.net/problem/13172
13172번: Σ
모듈러가 11에서 1,000,000,007이 되어 답이 달라졌지만, 역시 3을 곱한 다음 1,000,000,007으로 나눈 나머지는 7이 된다.
www.acmicpc.net
알고리즘: 분할정복(제곱)
S1N1+S2N2에서, S1N1:a1×b−11 (mod X) S2N2:a2×b−12 (mod X) S1N1+S2N2:a1×b−11 (mod X)+a2×b−12 (mod X) 이 성립한다.
분할정복을 이용하여 X-2제곱(=10^9 + 5)을 구해주고 각각을 다 더해주면 AC.
더보기This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters#include <iostream> #include <algorithm> using namespace std; typedef long long ll; int M, p; ll N, S, ans, b; const int X = 1e9 + 7; int main(){ ios_base::sync_with_stdio(0); cin.tie(0), cout.tie(0); cin >> M; while(M--){ cin >> N >> S; p = X - 2; b = 1LL; while (p){ if (p & 1) b = (b * N) % X; N = (N * N) % X; p >>= 1; } ans = (ans + (S * b) % X) % X; } cout << ans; return 0; } 'PS > BOJ' 카테고리의 다른 글
[BOJ] 5639 이진 검색 트리 (0) 2022.07.18 [BOJ] 단계별로 풀어보기 - 최단 경로 (0) 2021.06.15 [BOJ] 수열과 쿼리 13 (0) 2020.10.23 [BOJ] 13545 수열과 쿼리 0, 13550 수열과 쿼리 7 (0) 2020.10.23 [BOJ] 13546 수열과 쿼리 4 (0) 2020.10.22