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
2
4 1
output
0

题意是给定50w的序列,要求分成连续的3份,使得每一份的和相等。

一开始没想出来……然后被黄巨大D成sb

先判一下总的sum是不是3的倍数,不是直接return

然后求个前缀和、后缀和,显然当且仅当前缀和=后缀和=sum/3时方案可行

然后for一遍,先判断后缀和是否=sum/3,如果是,答案更新为(当前前缀和=sum/3的个数),这个可以在for的时候算出来

#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<ctime>
#define LL long long
using namespace std;
int n;
int a[500010];
LL s[500010];
LL t[500010];
LL ans,tot,toadd;
inline LL read()
{
LL x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
int main()
{
n=read();
for (int i=1;i<=n;i++)a[i]=read(),tot+=a[i];
if (tot%3)
{
printf("0");
return 0;
}
tot/=3;
for (int i=1;i<=n;i++)s[i]=s[i-1]+a[i];
for (int i=n;i>=1;i--)t[i]=t[i+1]+a[i];
for (int i=2;i<=n;i++)
{
if (t[i]==tot) ans+=toadd;
if (s[i-1]==tot)toadd++;
}
printf("%lld",ans);
}

  

cf466C Number of Ways的更多相关文章

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

    You've got array a[1], a[2], ..., a[n], consisting of n integers. Count the number of ways to split ...

  2. LeetCode 5274. Number of Ways to Stay in the Same Place After Some Steps - Java - DP

    题目链接:5274. 停在原地的方案数 You have a pointer at index 0 in an array of size arrLen. At each step, you can ...

  3. 【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 positio ...

  4. codeforce Number of Ways(暴力)

    #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #d ...

  5. codeforces 466C. Number of Ways 解题报告

    题目链接:http://codeforces.com/problemset/problem/466/C 题目意思:给出一个 n 个数的序列你,问通过将序列分成三段,使得每段的和都相等的分法有多少种. ...

  6. Codeforces - 466C - Number of Ways - 组合数学

    https://codeforces.com/problemset/problem/466/C 要把数据分为均等的非空的三组,那么每次确定第二个分割点的时候把(除此之外的)第一个分割点的数目加上就可以 ...

  7. Codeforces466C Number of Ways

    题目链接: http://codeforces.com/problemset/problem/466/C 题意: 给一个长度为n的数组,将其分成连续的三段使三段的和相等.求有几种这种组合 分析: 从头 ...

  8. [Codeforces 466C] Number of Ways

    [题目链接] https://codeforces.com/contest/466/problem/C [算法] 维护序列前缀和 , 枚举中间一段即可 , 详见代码 时间复杂度 : O(N) [代码] ...

  9. 【Codeforces 466C】Number of Ways

    [链接] 我是链接,点我呀:) [题意] 让你把数组分成3个连续的部分 每个部分的和要一样 问你有多少种分法 [题解] 先处理出来num[i] 表示i..n这里面有多少个j 满足aft[j] = af ...

随机推荐

  1. LeetCode_Interleaving String

    Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given: s1 ...

  2. 找不到Qt5Cored.dll(Release和Debug版连接了不同的库)

    Qt5Cored.dll和Qt5Core.dll文件分别用于Qt软件的Debug版和Release版. 通常会有两个Qt5Core.dll文件,分别位于Qti安装目录下的“Qt5.1.0\5.1.0\ ...

  3. undefined reference to `png_set_longjmp_fn'

    这个是在Linux上编译项目的时候,一个动态库层用到的一个函数实现未找到,即使我链接了libpng2也没有找到,原因是这个库老了一些,没有这个函数定义,需要链接更高版本的png库,CentOS上有了在 ...

  4. Unique Paths 解答

    Question A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram be ...

  5. UVA 10047-The Monocycle(队列bfs+保存4种状态)

    题意:给你一张地图,S代表起点,T代表终点,有一个轮盘,轮盘平均分成5份,每往前走一格恰好转1/5,轮盘只能往前进,但可以向左右转90°,每走一步或是向左向右转90° 要花费1单位的时间,问最少的时间 ...

  6. hdu 4751 Divide Groups(dfs染色 或 2-sat)

    Problem Description   This year is the 60th anniversary of NJUST, and to make the celebration more c ...

  7. libaio under MIPS architecture /在mips架构下使用的libaio

    First, you can find libaio source in http://libaio.sourcearchive.com/ Second,download the libaio_0.3 ...

  8. Hacker(三)之黑客定位目标---IP

    IP即Internet Protocol的简称,中文简称"网协",是为计算机网络相互连接进行通信而设计的协议.无论何种操作系统,只要遵守IP协议就可以与Internet互联互通. ...

  9. mysql-5.6.27源码安装及错误解决办法

    wget http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.27.tar.gz yum install -y cmake  当然也可以自己下载源码包安 ...

  10. resolv.conf 是什么

    From Wikipedia, the free encyclopedia This article does not cite any references or sources. Please h ...