Number of Ways

Description

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 integersa[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 Input

Input

51 2 3 0 3

Output

2

Input

40 1 -1 0

Output

1

Input

24 1

Output

0大意:给你一串数让你分成平均三分,问共有多少种分法,主要是复杂度问题,用一个for把复杂度降到O(n),先求出平均值,让后计算为平均值的结点和两倍平均值的结点,如果前面已经有结点为平均值,那么两倍结点的那个就加上前面共有多少一倍结点的,就把复杂度降低了orz自己太笨想不到

#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
const int MAX = 520000;
long long a[MAX];
int main()
{
   int n;
   int x;
   scanf("%d",&n);
   a[0] = 0;
      for(int i = 1; i <= n ; i++){
        scanf("%d",&x);
        a[i] = a[i-1] + x;
    }
long long  flag ,flag1;
flag = flag1 = 0;
if(a[n]%3 == 0){
long long k = a[n]/3;
long long m = a[n]/3 * 2;
for(int i = 1; i < n; i++){
    if(a[i] == m)
    flag1 += flag;
    if(a[i] == k)
    flag++;
   }
}
cout << flag1;
return 0;
}

 
时间: 2024-11-06 16:36:29

Number of Ways的相关文章

Codeforces Round #266 (Div. 2)C. Number of Ways

传送门 Description 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 nee

[GeeksForGeeks] Count Number of ways to reach a given score in a game

Consider a game where a player can score 3 or 5 or 10 points in a move. Given a total score n, find the number of ways to reach the given score. Example: Input n = 20 Output: 4 There are the following 4 ways to reach 20. (10, 10) (5, 5, 10) (5, 5, 5,

[Coding Made Simple] Coin Changes Number of ways to get a total

Given coins of certain denominations and a total, how many ways these coins can be combined to get the total. Dynamic Programming solution State: T[i][j]: given the first i coins, the total number of ways these coins can be combined to get the total

CF 466C Number of Ways(数学 / 思维 / DP)

题目链接:http://codeforces.com/problemset/problem/466/C 题目: 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

[CodeForces 466C] Number of Ways

Given an array that has n integers, count the number of ways to split all elements of this array into 3 contiguous parts so that the sum of each part is the same. Each part must not be empty. Algorithm: O(N) runtime 1. If total sum % 3 != 0, return 0

【leetcode】1269. Number of Ways to Stay in the Same Place After Some Steps

题目如下: You have a pointer at index 0 in an array of size arrLen. At each step, you can move 1 position to the left, 1 position to the right in the array or stay in the same place  (The pointer should not be placed outside the array at any time). Given

LeetCode 1269. Number of Ways to Stay in the Same Place After Some Steps

原题链接在这里:https://leetcode.com/problems/number-of-ways-to-stay-in-the-same-place-after-some-steps/ 题目: You have a pointer at index 0 in an array of size arrLen. At each step, you can move 1 position to the left, 1 position to the right in the array or

[LintCode] 1835. Number of Ways to Stay in the Same Place After Some Steps I

You have a pointer at index 00 in an array of size arrLenarrLen. At each step, you can move 11 position to the left, 11 position to the right in the array or stay in the same place (The pointer should not be placed outside the array at any time). Giv

Codeforces466C Number of Ways

题目链接: http://codeforces.com/problemset/problem/466/C 题意: 给一个长度为n的数组,将其分成连续的三段使三段的和相等,求有几种这样的组合 分析: 从头扫到尾,将所有的前缀和为(sum/3)的点统计起来,然后再从尾开始统计,找到统计所有后缀和为(sum/3)的节点 然后这种方案的数为 这个点之前所有前缀和为sum/3的个数 代码如下: #include <iostream> #include <cstring> #include &