题目链接:
题目链接:点击打开链接
#include <cstdio> #include <cstring> #include <algorithm> #include <vector> #include <iostream> #include <map> #include <set> #include <math.h> using namespace std; #define inf 10000000 #define N 100050 #define ll __int64 ll n; ll a[N], lsum[N], rsum[N]; ll lok[N], rok[N]; int main(){ ll i, j; while(cin>>n) { ll sum = 0; bool siz = false; for(i=1;i<=n;i++)cin>>a[i], sum += a[i], siz |= a[i]; if(!siz) { cout<<(n-1)*(n-2)/2<<endl; continue; } if(sum%3){puts("0");continue;} sum/=3; lsum[0] = 0; for(i=1;i<=n;i++)lsum[i] = lsum[i-1]+a[i]; rsum[n+1] = 0; for(i=n;i;i--)rsum[i] = rsum[i+1]+a[i]; memset(lok, 0, sizeof lok); memset(rok, 0, sizeof rok); for(i=1;i<=n;i++) { if(lsum[i]==sum){ lok[i] = 1; } // if(i==n)lok[i]=0; // lok[i] += lok[i-1]; } for(i=n;i;i--) { if(rsum[i]==sum){ rok[i] = 1; } rok[i]+=rok[i+1]; } ll ans = 0; for(i = 1; i+2<=n; i++) ans += lok[i] * rok[i+2]; // ll ans = (lok[n]*(lok[n]-1))/2; cout<<ans<<endl; } return 0; } /* 7 -1 1 -1 1 -1 1 0 9 -5 -2 1 1 5 0 -4 4 0 100 3 0 -5 2 -3 -1 -1 0 -2 -5 -4 2 1 2 -2 -1 -1 -4 3 -1 -3 -1 5 0 -4 -4 -1 0 -2 -2 0 1 -1 -2 -1 -5 -4 -2 3 1 -3 0 -1 1 0 -1 2 0 -2 -1 -3 1 -2 2 3 2 -3 -5 2 2 -2 -2 1 2 -2 -1 3 0 -4 7 -2 2 1 4 -9 -1 -2 -1 0 -1 0 -2 -2 -1 1 1 -4 2 -3 -3 7 1 1 -3 -7 0 -2 0 5 -2 */
时间: 2024-10-19 22:06:06