codeforces原题链接:https://codeforces.com/problemset/problem/466/C

CF466C 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 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 .

输入格式

The first line contains integer $ n $ $ (1<=n<=5·10^{5}) $ , showing how many numbers are in the array. The second line contains $ n $ integers $ a[1] $ , $ a[2] $ , ..., $ a[n] $ $ (|a[i]|<=10^{9}) $ — the elements of array $ a $ .

输出格式

Print a single integer — the number of ways to split the array into three parts with the same sum.

输入输出样例 #1

输入 #1

5
1 2 3 0 3

输出 #1

2

输入输出样例 #2

输入 #2

4
0 1 -1 0

输出 #2

1

输入输出样例 #3

输入 #3

2
4 1

输出 #3

0

思路:

这道题不难发现,可以利用前缀和更快捷的划分区间求和,将数组划分为3份,说明需要找到两个断点,在第一个断点前面的数字的和为所有数字加起来的sum的三分之一,而从索引1到第二个断点之间的数字之和则是sum的三分之二,而当sum无法被3整除时候一定无解,所以在前面可以对sum先进行一个特判,同时要对两个断点设定好满足条件,上题解

题解

#include <bits/stdc++.h>
using namespace std;
const int N=5e5+10;
typedef long long ll;
int n;
int a[N];
ll presum[N];
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n;
ll sum=0;
for(int i=1;i<=n;i++)
{
cin>>a[i];
sum+=a[i];
presum[i]+=presum[i-1]+a[i]; }
if(sum%3)cout<<0<<endl;
else
{
ll ans=0;
ll cnt=0;
ll div= sum/3 ;
for(int i=1;i<n;i++)
{
if(presum[i]==(2*div)&&i<=n-1&&i>1)ans+=cnt;
if(presum[i]==div&&i<=n-2)cnt++;
}
cout<<ans<<endl;
} return 0;
}

CF466C Number of Ways (前缀和)的更多相关文章

  1. cf466C Number of Ways

    C. Number of Ways time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  2. 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 ...

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

  4. 【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 ...

  5. codeforce Number of Ways(暴力)

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

  6. Codeforces466C Number of Ways

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

  7. [Codeforces 466C] Number of Ways

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

  8. HDU 4055 Number String:前缀和优化dp【增长趋势——处理重复选数】

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4055 题意: 给你一个由'I', 'D', '?'组成的字符串,长度为n,代表了一个1~n+1的排列中 ...

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

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

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

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

随机推荐

  1. AI模型的回调能力的理解和实现

    前言 BigTall最近把RAG和Agent的原理想通了,对于"一切都是提示词"的理解又更多了一些.本文把我的理解大致整理了一下,给出BigTall自己的一个实验.希望能够对大家有 ...

  2. Pytorch之Tensor学习

    Pytorch之Tensor学习 Tensors是与数组和矩阵类似的数据结构,比如它与numpy 的ndarray类似,但tensors可以在GPU上运行.实际上,tensors和numpy数组经常共 ...

  3. Java面试|BIO、NIO、AIO的区别和应用场景

    摘要:BIO是一个连接一个线程,NIO是一个请求一个线程,AIO是一个有效请求一个线程.   在学习Java I/O类库时,容易混淆NIO.BIO.AIO这几个概念,同时对于阻塞和非阻塞.同步和异步的 ...

  4. Ubuntu 通过 docker 启动 mysql

    1.首先拉取MySQL的镜像 docker pull mysql 2.运行mysql容器 docker run --name mysql -p 3306:3306 -e MYSQL_ROOT_PASS ...

  5. HanTTS简单文档

    先下载 https://gitee.com/dhfhub/HanTTS 然后输入命令 main.py synthesize --text 你的话 --src "syllables/" ...

  6. Linux | base64编码与解码命令

    1.base64编码 (1)base64 file 功能:从指定的文件file中读取数据,编码为base64的字符串然后输出: (2)echo "string" | base64 ...

  7. java-web与jdbc 的使用

    1:本地连接数据库的步骤 1 public class LoginDao { 2 3 // jdbc操作的五部曲 4 5 // 1: 导入jar包 6 // 2: 加载驱动 7 // 3: 获取连接 ...

  8. DotTrace系列:3. 时间度量之墙钟时间和线程时间

    一:背景 1. 讲故事 在用 dotTrace 对程序进行性能评测的时候,有一个非常重要的概念需要使用者明白,那就是 时间度量 (Time measurement),主要分为两种. 墙钟时间 线程时间 ...

  9. 数栈 × AWS EMR On EC2 适配实践:打造出海企业可落地的云上数据中台解决方案

    随着袋鼠云全面推进数栈产品的出海战略,我们在服务多个头部出海客户的过程中发现,真正做好"海外可用"的数据平台,关键不仅在于部署全球化,还在于深入适配 AWS 的核心计算平台EMR, ...

  10. Python全栈应用开发利器Dash 3.x新版本介绍(1)

    更多Dash应用开发干货知识.案例,欢迎关注"玩转Dash"微信公众号 大家好我是费老师,Dash作为Python生态中强大且灵活的全栈应用开发框架,早在几个月前就发布了其3.0新 ...