首页 > 代码库 > codeforce Number of Ways(暴力)

codeforce Number of Ways(暴力)

 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #define N 500005 6 using namespace std; 7 typedef long long LL; 8 LL prefix[N], suffix[N], num[N]; 9 LL cntSuf[N];10 int main(){11     int n;12     scanf("%d", &n);13     for(int i=1; i<=n; ++i){14         scanf("%lld", &num[i]);15         prefix[i]=prefix[i-1]+num[i];//前缀和16     }    17     for(int i=n; i>=1; --i)18         suffix[i]=suffix[i+1]+num[i];//后缀和19     LL s=prefix[n]/3;20     if(prefix[n]%3!=0){21         printf("0\n");22         return 0;23     }24     LL ans=0;    25     for(int i=1; i<=n; ++i)        26         if(suffix[n-i+1]==s) cntSuf[n-i+1]=cntSuf[n-i+2]+1;27         else cntSuf[n-i+1]=cntSuf[n-i+2];28     for(int i=1; i<=n; ++i)29         if(prefix[i]==s) ans+=cntSuf[i+2];30     printf("%lld\n", ans);31     return 0;32 }

 

codeforce Number of Ways(暴力)