Team Training 1

Source: Internet
Author: User

      A Number of Ways

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, the sum of elements in Each of the same.

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

Input

The first line contains an integer n (1≤ n ≤5 105), showing how many numbers is in the array . The second line contains n integers a[1], a[2], ..., a[n< /c13>] (|  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.

The main idea is to give you the number of n, divide them into successive three parts so that each part and the same, to find out the number of the Division method.

This is done by using an array of a[i] to note the sum from the first point to the current I point. The last point is the sum of the points, only the sum of 1/3sum points and the sum of the points for the 2/3sum of all cases. The number of points that iterate through the sum of 2/3sum points before the sum of 1/3sum.

#include <stdio.h>#include<string.h>#include<iostream>using namespacestd;Const intmaxn=500005; typedefLong LongLl;ll A[MAXN];intMain () {intN;  while(~SCANF ("%d",&N)) {a[0]=0;  for(intI=1; i<=n;i++) {cin>>A[i]; A[i]=a[i]+a[i-1]; }                    if(a[n]%3!=0) {printf ("0\n"); Continue; } ll sum1=a[n]/3; ll Sum2=sum1*2;intans=0; ll cut=0;  for(intI=1; i<n;i++)        {                 if(a[i]==sum2) {Cut+=cn1; }            if(A[i]==sum1 && i<n-1) {cn1++; }} cout<<cut<<Endl; }     return 0;}

You should simulate the process rather than consider multiple situations to make a math problem (...), and a point is the sum of the records of the array and the value of the sum of records to apply long long, carefully read the problem, see the scope of the problem data, do not fear is dry.

I

CMP Write Miss ... The first two statements are written against the same effect ...

BOOL cmp (post A,post b) {    if(a.x==b.x)        return a.y<b.y;     return a.x<b.x;           }           

Team Training 1

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.