C. Number of Ways

C.Number of Ways

time limit per test: 2 secondsmemory limit per test: 256 megabytes
C.每次测试的次数限制:每次测试2次极限:256兆字节

input: standard input
输入:标准输入

output: standard output
产出:标准产出

Youve 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 threecontiguous parts so that the sum of elements in each part is the same.
Youve 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 threecontiguous 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 (2Si<jSn- 1.that三-点w-o
更正式地说,您需要找到这样的指数对的数目I.J(2Si<jSn-1)和三-点w-o

lnput
输入

The first line contains integer n(1≤n<5-10*), showing how many numbers are in the aray. The second line contains n integers a[1]a[2]… a[n](|a[们]≤ 109) —the elements of array a.
第一行包含整数n(1≤n<5-10*),显示数组中有多少个数字。第二行包含n个整数a[1]a[2].An-数组a的元素。

output
输出量

Print a single integer —the number of ways to split the array into three parts with the same sum.
打印一个整数–将数组拆分为具有相同和的三个部分的方法的数目。
前缀和后缀和
即 求前面满足sum(1 i)=sum(1 n)/3的组数乘sum(j n)=sum(1 n)/3的组数 ,(j-i>2)

#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
#define llm 5000003

long long a[llm], lsum[llm], rsum[llm], c[llm], rk[llm],ans;
int main()
{
	int n;
	cin >> n>>lsum[0];
	for (int i = 1; i < n; ++i)
		cin >> a[i],lsum[i]=a[i]+lsum[i-1];
	if (lsum[n-1] % 3 != 0)
	{
		printf("0\n");
		return 0;
	}
	for (int i = 1; i <= n; i++) rsum[i] = lsum[n-1] - lsum[i] + a[i];
	for (int i = n; i >= 1; i--) if (rsum[i] == lsum[n-1] / 3) c[i] = c[i + 1] + 1;
	else c[i] = c[i + 1];
	for (int i = 1; i <= n - 1; i++) if (lsum[i] == lsum[n-1] / 3) ans += c[i + 2];
	cout << ans << endl;
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值