您的位置:首页 > 其它

codeforces round #266 C. Number of Ways

2014-09-13 19:57 381 查看
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
24 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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: