codeforce Number of Ways(暴力)

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #define N 500005
 6 using namespace std;
 7 typedef long long LL;
 8 LL prefix[N], suffix[N], num[N];
 9 LL cntSuf[N];
10 int main(){
11     int n;
12     scanf("%d", &n);
13     for(int i=1; i<=n; ++i){
14         scanf("%lld", &num[i]);
15         prefix[i]=prefix[i-1]+num[i];//前缀和
16     }
17     for(int i=n; i>=1; --i)
18         suffix[i]=suffix[i+1]+num[i];//后缀和
19     LL s=prefix[n]/3;
20     if(prefix[n]%3!=0){
21         printf("0\n");
22         return 0;
23     }
24     LL ans=0;
25     for(int i=1; i<=n; ++i)
26         if(suffix[n-i+1]==s) cntSuf[n-i+1]=cntSuf[n-i+2]+1;
27         else cntSuf[n-i+1]=cntSuf[n-i+2];
28     for(int i=1; i<=n; ++i)
29         if(prefix[i]==s) ans+=cntSuf[i+2];
30     printf("%lld\n", ans);
31     return 0;
32 }
时间: 2025-01-02 17:19:20

codeforce Number of Ways(暴力)的相关文章

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

Mine Number(搜索,暴力) ACM省赛第三届 G

1 安装及下载client 端 2 什么是SVN(Subversion)? 3 为甚么要用SVN? 4 怎么样在Windows下面建立SVN Repository? 5 建立一个Working目录 6 新增档案及目录到Repository中 7 更新档案及目录 8 更新至特定版本 9 复制档案及目录 10 制作Tag或是Release 11 快速参考 11.1 取得(Checkout)Repository 11.2 更新(Update)档案或目录 11.3 新增(Add)档案或目录 11.4 提

Codeforces Round #266 (Div. 2)C. Number of Ways

传送门 Description 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 nee

[GeeksForGeeks] Count Number of ways to reach a given score in a game

Consider a game where a player can score 3 or 5 or 10 points in a move. Given a total score n, find the number of ways to reach the given score. Example: Input n = 20 Output: 4 There are the following 4 ways to reach 20. (10, 10) (5, 5, 10) (5, 5, 5,

[Coding Made Simple] Coin Changes Number of ways to get a total

Given coins of certain denominations and a total, how many ways these coins can be combined to get the total. Dynamic Programming solution State: T[i][j]: given the first i coins, the total number of ways these coins can be combined to get the total

Number of Ways

Description 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

[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

【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