CF 466C Number of Ways(数学 / 思维 / DP)

题目链接:http://codeforces.com/problemset/problem/466/C

题目:

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.

Examples

input

Copy

51 2 3 0 3

output

2

input

Copy

40 1 -1 0

output

1

input

Copy

24 1

output

0

题解:前缀和暴力,找下规律。(好像还能用DP解,明天起来再看看!)

 1 #include <map>
 2 #include <cstdio>
 3 #include <iostream>
 4 #include <algorithm>
 5 using namespace std;
 6
 7 typedef long long LL;
 8 const int N=1e6;
 9 LL f[N],c1[N],c2[N];
10
11 int main(){
12     LL n,ans=0,cnt=0;
13     scanf("%lld",&n);
14     for(int i=1;i<=n;i++){
15         scanf("%lld",&f[i]);
16         f[i]+=f[i-1];
17         if(!f[i]) cnt++;
18     }
19     if(f[n]%3!=0) {printf("0\n");return 0;}
20     if(f[n]==0){
21         printf("%lld\n",(cnt-1)*(cnt-2)/2);
22         return 0;
23     }
24
25     for(int i=1;i<=n;i++){
26         c1[i]=c1[i-1];c2[i]=c2[i-1];
27         if(f[i]==(f[n]/3)) c1[i]++;
28         if(f[i]==(f[n]/3*2)) c2[i]++;
29     }
30     for(int i=2;i<n;i++){
31         if(f[i]==(f[n]/3*2)) ans+=c1[i];
32     }
33     printf("%lld\n",ans);
34     return 0;
35 }

原文地址:https://www.cnblogs.com/Leonard-/p/8503515.html

时间: 2024-10-25 07:44:36

CF 466C Number of Ways(数学 / 思维 / DP)的相关文章

[CodeForces 466C] Number of Ways

Given an array that has n integers, count the number of ways to split all elements of this array into 3 contiguous parts so that the sum of each part is the same. Each part must not be empty. Algorithm: O(N) runtime 1. If total sum % 3 != 0, return 0

[2013山东ACM省赛] The number of steps (概率DP,数学期望)

The number of steps Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 Mary stands in a strange maze, the maze looks like a triangle(the first layer have one room,the second layer have two rooms,the third layer have three rooms -). Now she st

hdu 4661 Message Passing (思维 dp求拓扑排序数)

Message Passing Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 1184    Accepted Submission(s): 420 Problem Description There are n people numbered from 1 to n. Each people have a unique mes

CF# 149 D Coloring Brackets(区间dp)

D - Coloring Brackets Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Practice CodeForces 149D Description Once Petya read a problem about a bracket sequence. He gave it much thought but didn't find a solut

【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 position to the left, 1 position to the right in the array or stay in the same place  (The pointer should not be placed outside the array at any time). Given

LeetCode 1269. Number of Ways to Stay in the Same Place After Some Steps

原题链接在这里:https://leetcode.com/problems/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 position to the left, 1 position to the right in the array or

程序设计中的数学思维函数总结(代码以C#为例)

最近以C#为例,学习了程序设计基础,其中涉及到一些数学思维,我们可以巧妙的将这些逻辑问题转换为代码,交给计算机运算. 现将经常会使用到的基础函数做一总结,供大家分享.自己备用. 1.判断一个数是否为奇数 定义:整数中,能被2整除的数是偶数,不能被2整除的数是奇数 思路点:n%2!=0则为奇数 /// <summary> /// 判断一个整数是不是奇数 /// </summary> /// <param name="n">要判断的整数</para

hdu 4710 Balls Rearrangement (数学思维)

题意:就是  把编号从0-n的小球对应放进i%a编号的盒子里,然后又买了新盒子, 现在总共有b个盒子,Bob想把球装进i%b编号的盒子里.求重置的最小花费. 每次移动的花费为y - x ,即移动前后盒子编号的差值的绝对值. 算法: 题目就是要求                  先判断  n与  lcm(a,b)的大小,每一个周期存在循环,这样把区间缩短避免重复计算. 如果n>lcm(a,b)则   ans = (n/lcm)*solve(lcm)+solve(n%lcm) 否则   ans =

Acdreamoj1115(数学思维题)

题意:1,3是完美数,如果a,b是完美数,则2+a*b+2*a+2*b,判断给出的n是否是完美数. 解法:开始只看出来2+a*b+2*a+2*b=(a+2)*(b+2)-2,没推出更多结论,囧.没办法,只能暴力将所有的完美数求出来然后查表.正解是c+2=(a+2)*(b+2);完美数都是有质因子3或5组成的(5本身除外): 自己暴力代码: /****************************************************** * author:xiefubao *****