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. SqlBulkCopy 类

    1.SqlBulkCopy 简介  Microsoft SQL Server 提供一个称为 bcp 的流行的命令提示符实用工具,用于将数据从一个表移动到另一个表(表既可以在同一个服务器上,也可以在不同 ...

  2. C# System.AppDomain类

    进程是存在独立的内存和资源的,但是AppDomain仅仅是逻辑上的一种抽象.一个process可以存在多个AppDomain.各个AppDomain之间的数据时相互独立的.一个线程可以穿梭多个AppD ...

  3. Linux命令之xargs的分析及隐患

    写一个main.c: #include <stdio.h> int main(){ printf("foo"); printf("bar"); re ...

  4. 非常棒的Java REST服务器栈

    Dropwizard 是一个开源的Java框架,用于开发OPS友好.高性能的基于REST的后端.它是由Yammer开发的,来驱动基于JVM的后端. Dropwizard提供同类最佳的Java库到一个嵌 ...

  5. Clone使用方法详解【转载】

    博客引用地址:Clone使用方法详解 Clone使用方法详解   java“指针”       Java语言的一个优点就是取消了指针的概念,但也导致了许多程序员在编程中常常忽略了对象与引用的区别,本文 ...

  6. 使用Comparable接口的小例子

    代码: public class Student implements Comparable<Student> { private int id; private String name; ...

  7. 使用CSS达到阴阳八卦图等图形

    CSS还是比較强大的,能够实现中国古典的"阴阳八卦图"等形状. 正方形 #rectangle { width: 200px; height: 100px; backgrount-c ...

  8. linux下SSH远程连接服务慢解决方案

    1.适用命令及方案如下:[远程连接及执行命令]ssh -p22root@10.0.0.19ssh -p22 root@10.0.0.19 /sbin/ifconfig[远程拷贝:推送及拉取]scp - ...

  9. Hadoop: HDFS 格式化时,出现 “ERROR namenode.NameNode: java.io.IOException: Cannot create directory /usr/hadoop/tmp/dfs/name/current”

    原因是 没有设置 /usr/hadoop/tmp 的权限没有设置, 将之改为: chown –R hadoop:hadoop /usr/hadoop/tmp 查看:

  10. Android Studio自定义注释模板及生成JavaDoc

    刚开始学习Android,使用了Android Studio IDE.为了将来生产JavaDoc,学习一下如何自定义注释模板. . 自定义注释模板 1. 通过 File –>Settings 或 ...