fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define int long long int
  4. #define double long double
  5. #define print(a) for(auto x : a) cout << x << " "; cout << endl
  6.  
  7.  
  8. const int M = 1000000007;
  9. const int N = 3e5+9;
  10. const int INF = 2e9+1;
  11. const int LINF = 2000000000000000001;
  12.  
  13. inline int power(int a, int b, int mod=M) {
  14. int x = 1;
  15. a %= mod;
  16. while (b) {
  17. if (b & 1) x = (x * a) % mod;
  18. a = (a * a) % mod;
  19. b >>= 1;
  20. }
  21. return x;
  22. }
  23.  
  24.  
  25. //_ ***************************** START Below *******************************
  26.  
  27.  
  28.  
  29.  
  30. vector<int> a;
  31.  
  32. int consistency1(int n){
  33.  
  34. int total = 0;
  35. vector<int> prefix(n+1, 0);
  36.  
  37. for(int i=0; i<n; i++){
  38. prefix[i+1] = prefix[i] + a[i];
  39. total += a[i];
  40. }
  41.  
  42. if(total % 3 != 0) return false;
  43.  
  44. int ans = 0;
  45. for(int i=1; i<=n-2; i++){
  46. for(int j=i+1; j<=n-1; j++){
  47. int leftSum = prefix[i];
  48. int midSum = prefix[j] - prefix[i];
  49. if(leftSum == total/3 && midSum == total/3 ) ans++;
  50. }
  51. }
  52.  
  53. return ans;
  54.  
  55. }
  56.  
  57.  
  58.  
  59. int consistency2(int n){
  60.  
  61. int total = 0;
  62. for(int i=0; i<n; i++) total += a[i];
  63.  
  64. if(total % 3 != 0) return false;
  65. int x = total/3;
  66.  
  67.  
  68. unordered_map<int,int> prefixSum;
  69.  
  70. int ans = 0;
  71. int sum = 0;
  72.  
  73. for(int i=0; i<=n-2; i++){
  74. sum += a[i];
  75.  
  76. if( !(sum & 1) && sum == 2*x ){
  77. ans += prefixSum[sum/2];
  78. }
  79. prefixSum[sum]++;
  80. }
  81.  
  82. return ans;
  83.  
  84. }
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91. int consistency3(int n){
  92.  
  93. int total = 0;
  94. for(int i=0; i<n; i++) total += a[i];
  95.  
  96. if(total % 3 != 0) return false;
  97. int x = total/3;
  98.  
  99.  
  100. int ct = 0;
  101. int ans = 0;
  102. int sum = 0;
  103.  
  104. for(int i=0; i<=n-2; i++){
  105. sum += a[i];
  106.  
  107. if(sum == x ){
  108. ct++;
  109. }
  110. if(sum == 2*x){
  111. ans += ct;
  112. }
  113.  
  114. }
  115.  
  116. return ans;
  117.  
  118. }
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141.  
  142.  
  143.  
  144. int practice(int n){
  145.  
  146.  
  147. return 0;
  148. }
  149.  
  150.  
  151.  
  152.  
  153.  
  154. void solve() {
  155.  
  156. int n;
  157. cin>> n;
  158.  
  159. a.resize(n);
  160. for(int i=0; i<n; i++) cin >> a[i];
  161.  
  162. cout << consistency1(n) << " " << consistency2(n) << " " << consistency3(n) << endl;
  163.  
  164.  
  165. }
  166.  
  167.  
  168.  
  169.  
  170.  
  171. int32_t main() {
  172. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  173.  
  174. int t = 1;
  175. // cin >> t;
  176. while (t--) {
  177. solve();
  178. }
  179.  
  180. return 0;
  181. }
Success #stdin #stdout 0s 5316KB
stdin
7
1 2 0 0 1 2 3
stdout
3 3 3