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. I2C_周立功标准驱动程序_c代码

    /****************************************Copyright (c)********************************************** ...

  2. 【转】Compile、Make和Build的区别

    原文网址:http://lavasoft.blog.51cto.com/62575/436216 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任 ...

  3. UESTC_秋实大哥与花 2015 UESTC Training for Data Structures<Problem B>

    B - 秋实大哥与花 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submi ...

  4. html中隐藏域hidden的作用

    基本语法: <input type="hidden" name="field_name" value="value"> 作用: ...

  5. Python的字符串与数字

    Python3.0通过“input”实现读取控制台的输入与用户实现交互.值得注意的是input接受的所有数据都是字符串,即使输入的是数字,依然会被当作字符串来处理.这就会出现一些问题,所以需要进行类型 ...

  6. 兼容各个浏览器的H.264播放: H.264+HTML5+FLOWPLAYER+WOWZA+RMTP

    一.方案确定 计划做视频播放,要求可以播放H264编码的mp4文件,各个浏览器,各种终端都能播放. 首先查找可行性方案, http://www.cnblogs.com/sink_cup/archive ...

  7. swiftTools

    String+Exten.swift // // String+Exten.swift // swiftTest // // Created by napiao on 15/11/27. // Cop ...

  8. FlashBack-SCN-TIMESTAMP

    一.基于时间(as of timestamp)的flashback1.创建表create table flash_tab(id,vl) as select rownum,oname from ( se ...

  9. NET基础课--WinForm开发推荐3

    用户体验 较长时间的运算:使用进度条(progress bar) 不要阻塞界面(UI)线程:使用多线程进行长时间的运算 状态栏(status bar)提示应用程序的状态 操作开始之后,用户应当能够通过 ...

  10. net 2.0使用ajax

    asp.net ajax中用到了几个dll文件,这些可以从网上下载.http://ajax.asp.net站点下可以找到相关的下载.这其中包括:System.Web.Extensions.dll.Sy ...