luyencode / comments

Server lưu trữ bình luận trên Luyện Code
https://luyencode.net
6 stars 3 forks source link

https://oj.luyencode.net/problem/MAXDIV?fbclid=IwAR0AtqzyngeNYQAq2esgk7zdJpcZZ3Cn9ZLEzvLRDz3wCxVBHbfve-84KsM #737

Open utterances-bot opened 2 years ago

utterances-bot commented 2 years ago

Chi tiết bài tập - Luyện Code Online

https://luyencode.net/problem/MAXDIV?fbclid=IwAR0AtqzyngeNYQAq2esgk7zdJpcZZ3Cn9ZLEzvLRDz3wCxVBHbfve-84KsM

thequan0912 commented 2 years ago
Xem code AC

#include using namespace sdt; int a[300009], c[]; int countK(int L, int R) { return c[R] - c[L - 1]; } int searchL(int R) { int ans = R; int l = 1, r = R; while (l <= r) { int m = (l + r) / 2; if (countK(m, R) <= 1) { ans = m; r = m - 1; } else { l = m + 1; } } return ans; } int main() { int ans = 0; int n, k; cin >> n >> k; c[0] = 0; for (int i = 1; i <= n; i++) cin >> a[i]; for (int i = 1; i <= n; i++) c[i] = c[i - 1] + (a[i] % k == 0); for (int R = 1; R <= n; R++) { int L = searchL(R); ans += R - L + 1; } cout << ans;