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
output
input
output
input
output
统计有多少组i,j,使得sum[1,i].sum[i,j],sum[j+1,n]相等
考虑的东西太少了,fst了。。
不说了。。刚开始少考虑了2*sum/3 可能在 sum/3后面的。。
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; }
相关文章推荐
- Codeforces-466C-Number of Ways
- Codeforces 466 C. Number of Ways
- Codeforces - 466C. Number of Ways - 思维、暴力
- CareerCup Number of ways to take n identical objects out of a bucket
- codeforces 466-C. Number of Ways(前缀和+尺取)
- codeforce Number of Ways(暴力)
- codeforces-466C-Number of Ways
- Codeforces Round #266 (Div. 2)C. Number of Ways(想法题)
- 【寒江雪】C.Number of Ways
- Codeforces Round #266 (Div. 2)C. Number of Ways(想法题)
- Codeforces466C Number of Ways
- 【CODEFORCES】 C. Number of Ways
- codeforces 466 C Number of Ways
- Number of Ways
- CF 266 C. Number of Ways
- Codeforces466C Number of Ways
- [LeetCode] Number of decoding ways
- Q9.11 count the number of ways of parenthesizing the expression
- Number of Ways
- Codeforces Round #266 (Div. 2) C. Number of Ways