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. // 2
  28. // 12
  29. // 2 -3 6 0 4 -5 6 0 2 1 -8 10
  30. // 3
  31. // 0 0 0
  32.  
  33.  
  34.  
  35.  
  36. vector<int> a;
  37.  
  38. int consistency1(int n){
  39.  
  40. int total = 0;
  41. for(int i=0; i<n; i++){
  42. total += a[i];
  43. }
  44. if(total % 3 != 0) return 0;
  45.  
  46. int x = total/3;
  47.  
  48. int ans = 0;
  49. int sum = 0;
  50.  
  51. for(int i=0; i<=n-3; i++){
  52. sum += a[i];
  53.  
  54. if(sum != x) continue;
  55.  
  56. int sum2 = 0;
  57. for(int j=i+1; j<=n-2; j++){
  58. sum2 += a[j];
  59. if(sum2 == x) ans++;
  60. }
  61. }
  62.  
  63. return ans;
  64. }
  65.  
  66.  
  67.  
  68. int consistency2(int n){
  69.  
  70. int total = 0;
  71. for(int i=0; i<n; i++) total += a[i];
  72.  
  73. if(total % 3 != 0) return false;
  74. int x = total/3;
  75.  
  76.  
  77. unordered_map<int,int> prefixSum;
  78.  
  79. int ans = 0;
  80. int sum = 0;
  81.  
  82. for(int i=0; i<=n-2; i++){
  83. sum += a[i];
  84.  
  85. if( !(sum & 1) && sum == 2*x ){
  86. ans += prefixSum[sum/2];
  87. }
  88. prefixSum[sum]++;
  89. }
  90.  
  91. return ans;
  92.  
  93. }
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100. int consistency3(int n){
  101.  
  102. int total = 0;
  103. for(int i=0; i<n; i++){
  104. total += a[i];
  105. }
  106. if(total % 3 != 0) return 0;
  107.  
  108. int x = total/3;
  109.  
  110. int ans = 0;
  111.  
  112. int count_x = 0;
  113. int sum = 0;
  114.  
  115. for(int i=0; i<=n-2; i++){
  116. sum += a[i];
  117.  
  118. if(sum == 2*x) ans += count_x;
  119. if(sum == x) count_x++;
  120. }
  121.  
  122. return ans;
  123.  
  124. }
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141.  
  142.  
  143.  
  144.  
  145.  
  146.  
  147.  
  148.  
  149.  
  150. int practice(int n){
  151.  
  152.  
  153. }
  154.  
  155.  
  156.  
  157.  
  158.  
  159. void solve() {
  160.  
  161. int n;
  162. cin>> n;
  163.  
  164. a.resize(n);
  165. for(int i=0; i<n; i++) cin >> a[i];
  166.  
  167. cout << consistency1(n) << " " << consistency2(n) << " " << consistency3(n) << endl;
  168.  
  169. // cout << practice(n) << endl;
  170.  
  171. }
  172.  
  173.  
  174.  
  175.  
  176.  
  177. int32_t main() {
  178. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  179.  
  180. int t = 1;
  181. cin >> t;
  182. while (t--) {
  183. solve();
  184. }
  185.  
  186. return 0;
  187. }
Success #stdin #stdout 0s 5316KB
stdin
2
12
2 -3 6 0 4 -5 6 0 2 1 -8 10
3
0 0 0
stdout
4 4 4
1 1 1