codeforces round #266 C. Number of Ways

C. Number of Ways
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You've got array a[1], a[2], ..., a[n], consisting of n integers. Count the number of ways to split all the elements of the array into three contiguous parts so that the sum of elements in each part is the same.

More formally, you need to find the number of such pairs of indices i, j (2 ≤ i ≤ j ≤ n - 1), that .

Input

The first line contains integer n (1 ≤ n ≤ 5·105), showing how many numbers are in the array. The second line contains n integers a[1],a[2], ..., a[n] (|a[i]| ≤  109) — the elements of array a.

Output

Print a single integer — the number of ways to split the array into three parts with the same sum.

Sample test(s)
input
5
1 2 3 0 3
output
2
input
4
0 1 -1 0
output
1
input
2
4 1
output
0

统计有多少组i,j,使得sum[1,i].sum[i,j],sum[j+1,n]相等

考虑的东西太少了,fst了。。

不说了。。刚开始少考虑了2*sum/3 可能在 sum/3后面的。。

/***********************************************\
 |Author: YMC
 |Created Time: 2014/9/13 0:15:56
 |File Name: c.cpp
 |Description: 
\***********************************************/
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <string>
#include <cstring>
#include <algorithm>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#define L(rt) (rt<<1)
#define R(rt) (rt<<1|1)
#define mset(l,n) memset(l,n,sizeof(l))
#define rep(i,n) for(int i=0;i<n;++i)
#define maxx(a) memset(a, 0x3f, sizeof(a))
#define zero(a) memset(a, 0, sizeof(a))
#define srep(i,n) for(int i = 1;i <= n;i ++)
#define MP make_pair
const int inf=0x3f3f3f3f ;
const double eps=1e-8 ;
const double pi=acos (-1.0);
typedef long long ll;

using namespace std;
ll da[500005];
int n;
ll sum;
int main() {
	//freopen("input.txt","r",stdin); 
    scanf("%d",&n);
    sum = 0;
    rep(i,n){
        scanf("%I64d",&da[i]);
        sum += da[i];
    }
    if(sum == 0){
        ll ss = 0;
        ll ss1 = 0;
        for(int i=0;i<n;++i){
            ss += da[i];
            if(ss == 0) ss1 ++;
        }
        if(ss1 == 1 || ss1 == 2) {
            cout<<0<<endl;
            return 0;
        } else {
            cout<<(1+ss1-2)*(ss1-2)/2<<endl;
            return 0;
        }
    } 
    ll tt = sum / 3;
    if(tt * 3 != sum){
        puts("0");
        return 0;
    }
    ll t1 = tt;ll t2 = tt*2;
    ll tt1 = 0,tt2 = 0;
    ll su = 0;
    ll cc = 0;
    ll ans = 0;
    rep(i,n){
        su += da[i];
        if(su == t2) ans += cc;
        if(su == t1) cc ++;
        //if(su == t2) tt2 ++; 
    }
    cout<<ans<<endl;
	return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值