您的位置:首页 > 其它

C. Number of Ways

2015-08-30 21:12 309 查看
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


解题说明:此题是一道数学题,若平分分成若干种情况,应当整体(SUM)考虑,对SUM/3进行分析。它是区分3段的标准。

所以当部分和tmp==SUM/3,部分统计加一。

当tmp==sUM*2/3,则全部统计ans+=部分统计(s)

#include<stdio.h>
#include <string.h>
#include<iostream>
#include<algorithm>
using namespace std;

int i,n;
long long a,s,b[500005],ans;

int main()
{
cin>>n;
for (i=0;i<n;i++)
{
cin>>a;
s+=a;
b[i]=s;
}
a=0;
for (i=0;i<n;i++)
{
if (i>0&&i<n-1&&b[i]*3==b[n-1]*2)
{
ans+=a;
}
if (b[i]*3==b[n-1])
{
a++;
}
}
cout<<ans<<endl;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: