Advertisement
wym1111

Untitled

Sep 23rd, 2023
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.45 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int n;
  5. int a[100010], sum[100010], cnt[7];
  6. //cnt[x] 记录到当前位置,前缀和 %7 = x 的有多少个
  7.  
  8. signed main () {
  9.     cin >> n;
  10.     cnt[0] = 1;
  11.     int ans = 0;
  12.     for (int i = 1; i <= n; i ++) {
  13.         cin >> a[i];
  14.         sum[i] = sum[i - 1] + a[i];
  15.         ans += cnt[sum[i] % 7]; // 当前位置作为右端点的答案数
  16.         cnt[sum[i] % 7] ++; // 更新cnt , 当前点的贡献
  17.     }
  18.     cout << ans;
  19.     return 0;
  20. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement