codeforces-scrape

Codeforces scraped problems and editorials
git clone git://git.laack.co/codeforces-scrape.git
Log | Files | Refs | README

1985C.md (2152B)


      1 Source: https://codeforces.com/problemset/problem/1985/C
      2 
      3 C. Good Prefixes
      4 
      5 time limit per test
      6 
      7 2 seconds
      8 
      9 memory limit per test
     10 
     11 256 megabytes
     12 
     13 input
     14 
     15 standard input
     16 
     17 output
     18 
     19 standard output
     20 
     21 Alex thinks some array is good if there exists some element that can be represented as the sum of all other elements (the sum of all other elements is $0$ if there are no other elements). For example, the array $[1,6,3,2]$ is good since $1+3+2=6$. Furthermore, the array $[0]$ is also good. However, the arrays $[1,2,3,4]$ and $[1]$ are not good.
     22 
     23 Alex has an array $a_1,a_2,\ldots,a_n$. Help him count the number of good non-empty prefixes of the array $a$. In other words, count the number of integers $i$ ($1 \le i \le n$) such that the length $i$ prefix (i.e. $a_1,a_2,\ldots,a_i$) is good.
     24 
     25 Input
     26 
     27 The first line of the input contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases.
     28 
     29 The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the number of elements in the array.
     30 
     31 The second line of each test case contains $n$ integers $a_1,a_2,\ldots,a_n$ ($0 \le a_i \le 10^9$) — the elements of the array.
     32 
     33 It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
     34 
     35 Output
     36 
     37 For each test case, output a single integer — the number of good non-empty prefixes of the array $a$.
     38 
     39 Example
     40 
     41 Input
     42 
     43 ```
     44 7
     45 
     46 1
     47 
     48 0
     49 
     50 1
     51 
     52 1
     53 
     54 4
     55 
     56 1 1 2 0
     57 
     58 5
     59 
     60 0 1 2 1 4
     61 
     62 7
     63 
     64 1 1 0 3 5 2 12
     65 
     66 7
     67 
     68 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 294967296
     69 
     70 10
     71 
     72 0 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 589934592
     73 ```
     74 
     75 Output
     76 
     77 ```
     78 1
     79 0
     80 3
     81 3
     82 4
     83 1
     84 2
     85 ```
     86 
     87 Note
     88 
     89 In the fourth test case, the array has five prefixes:
     90 
     91 * prefix $[0]$ is a good array, as mentioned in the statement;
     92 * prefix $[0, 1]$ is not a good array, since $0 \ne 1$;
     93 * prefix $[0, 1, 2]$ is not a good array, since $0 \ne 1 + 2$, $1 \ne 0 + 2$ and $2 \ne 0 + 1$;
     94 * prefix $[0, 1, 2, 1]$ is a good array, since $2 = 0 + 1 + 1$;
     95 * prefix $[0, 1, 2, 1, 4]$ is a good array, since $4 = 0 + 1 + 2 + 1$.
     96 
     97 As you can see, three of them are good, so the answer is $3$.