Advertisement
pb_jiang

CF466C

Mar 23rd, 2025
381
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.35 KB | None | 0 0
  1. // Problem: C. Number of Ways
  2. // Contest: Codeforces - Codeforces Round 266 (Div. 2)
  3. // URL: https://codeforces.com/problemset/problem/466/C
  4. // Memory Limit: 256 MB
  5. // Time Limit: 2000 ms
  6. //
  7. // Powered by CP Editor (https://cpeditor.org)
  8.  
  9. #include <assert.h>
  10. #include <bits/stdc++.h>
  11. using namespace std;
  12. #ifndef __DEBUG__
  13. #define dbg(...) 42
  14. #endif
  15. template <class T> using mpq = priority_queue<T, vector<T>, greater<T>>;
  16.  
  17. using ll = long long;
  18. using a2l = array<ll, 2>;
  19. using pll = pair<ll, ll>;
  20. using vl = vector<ll>;
  21.  
  22. void solve()
  23. {
  24.     ll n;
  25.     cin >> n;
  26.     vl a(n);
  27.     for (auto &x : a)
  28.         cin >> x;
  29.     ll tot = accumulate(a.begin(), a.end(), 0ll), ans = 0;
  30.     if (tot % 3) {
  31.         cout << 0 << '\n';
  32.         return;
  33.     }
  34.  
  35.     vl post1(n), post2(n);
  36.     for (ll i = n - 1, sum = 0; i >= 0; --i) {
  37.         sum += a[i];
  38.         post1[i] = (i == n - 1 ? 0 : post1[i + 1]) + (sum == tot / 3);
  39.     }
  40.     for (ll i = n - 1, sum = 0; i >= 0; --i) {
  41.         sum += a[i];
  42.         post2[i] = (sum == tot / 3 * 2 ? (i == n - 1 ? 0 : post1[i + 1]) : 0);
  43.     }
  44.  
  45.     for (ll i = 0, sum = 0; i < n; ++i) {
  46.         sum += a[i];
  47.         if (sum == tot / 3)
  48.             ans += i + 1 < n ? post2[i + 1] : 0;
  49.     }
  50.     dbg(post1, post2);
  51.     cout << ans << '\n';
  52. }
  53.  
  54. int main(int argc, char **argv)
  55. {
  56.     solve();
  57.     return 0;
  58. };
  59.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement