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 }

 

转载于:https://www.cnblogs.com/hujunzheng/p/3969946.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值