description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$). For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$. Calculate the number of good subarrays of the array $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$)Β β€” the length of the array $a$. The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the number of good subarrays of the array $a$. -----Example----- Input 3 3 120 5 11011 6 600005 Output 3 6 1 -----Note----- The first test case is considered in the statement. In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$. In the third test case there is only one good subarray: $a_{2 \dots 6}$.
for _ in range(int(input())): n = int(input()) s = input() m = [1] + [0] * 9 * n cur, ans = 0, 0 for i in s: cur += ord(i) - 49 ans += m[cur] m[cur] += 1 print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP LIST NUMBER NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$). For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$. Calculate the number of good subarrays of the array $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$)Β β€” the length of the array $a$. The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the number of good subarrays of the array $a$. -----Example----- Input 3 3 120 5 11011 6 600005 Output 3 6 1 -----Note----- The first test case is considered in the statement. In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$. In the third test case there is only one good subarray: $a_{2 \dots 6}$.
t = int(input()) def Solve(presum): cnt = 0 dp = {} for i in range(len(presum)): if presum[i] == 0: cnt = cnt + 1 if presum[i] in dp: cnt = cnt + dp[presum[i]] if presum[i] not in dp: dp[presum[i]] = 1 else: dp[presum[i]] += 1 return cnt for _ in range(t): n = int(input()) inp = input() s = 0 Presum = [] for i in range(len(inp)): s = s + int(inp[i]) - 1 Presum.append(s) print(Solve(Presum))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$). For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$. Calculate the number of good subarrays of the array $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$)Β β€” the length of the array $a$. The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the number of good subarrays of the array $a$. -----Example----- Input 3 3 120 5 11011 6 600005 Output 3 6 1 -----Note----- The first test case is considered in the statement. In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$. In the third test case there is only one good subarray: $a_{2 \dots 6}$.
t = int(input()) myList = [] for _ in range(t): p = [] sum = 0 res = 0 d = {(0): 1} n = int(input()) a = list(input()) a = list(map(int, a)) k = 0 for i in range(n): sum += a[i] x = sum - i - 1 if x not in d: d[x] = 0 d[x] += 1 for s in d: res += d[s] * (d[s] - 1) // 2 myList.append(res) for elem in myList: print(elem)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER FOR VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$). For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$. Calculate the number of good subarrays of the array $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$)Β β€” the length of the array $a$. The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the number of good subarrays of the array $a$. -----Example----- Input 3 3 120 5 11011 6 600005 Output 3 6 1 -----Note----- The first test case is considered in the statement. In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$. In the third test case there is only one good subarray: $a_{2 \dots 6}$.
from sys import stdin, stdout t = int(stdin.readline()) for _ in range(t): n = int(stdin.readline()) ar = stdin.readline() di = {(1): 1} psum = 0 countps = 0 for i in range(0, n): psum += int(ar[i]) if psum - i in di: countps += di[psum - i] if psum - i in di: di[psum - i] += 1 else: di[psum - i] = 1 print(countps)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$). For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$. Calculate the number of good subarrays of the array $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$)Β β€” the length of the array $a$. The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the number of good subarrays of the array $a$. -----Example----- Input 3 3 120 5 11011 6 600005 Output 3 6 1 -----Note----- The first test case is considered in the statement. In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$. In the third test case there is only one good subarray: $a_{2 \dots 6}$.
T = int(input().strip()) for i in range(T): N = int(input().strip()) a = [i for i in input().strip()] d = {(0): 1} a.insert(0, "f") answer, s = 0, 0 for j in range(1, N + 1): s += int(a[j]) x = s - j if x not in d: d[x] = 0 answer += d[x] d[x] += 1 print(answer)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER STRING ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$). For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$. Calculate the number of good subarrays of the array $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$)Β β€” the length of the array $a$. The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the number of good subarrays of the array $a$. -----Example----- Input 3 3 120 5 11011 6 600005 Output 3 6 1 -----Note----- The first test case is considered in the statement. In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$. In the third test case there is only one good subarray: $a_{2 \dots 6}$.
t = int(input()) for i in range(t): n = int(input()) dic = {} dic[1] = 1 temp = 0 arr = input() for i in range(n): temp += int(arr[i]) try: dic[temp - i] += 1 except: dic[temp - i] = 1 ans = 0 for value in dic.values(): ans += value * (value - 1) // 2 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$). For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$. Calculate the number of good subarrays of the array $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$)Β β€” the length of the array $a$. The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the number of good subarrays of the array $a$. -----Example----- Input 3 3 120 5 11011 6 600005 Output 3 6 1 -----Note----- The first test case is considered in the statement. In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$. In the third test case there is only one good subarray: $a_{2 \dots 6}$.
import sys input = sys.stdin.readline def solve(): n = int(input()) a = list(map(int, input()[:-1])) fr = {(0): 1} res = 0 acum = 0 for x in a: acum += x - 1 if acum in fr: res += fr[acum] fr[acum] += 1 else: fr[acum] = 1 return res def main(): T = int(input()) while T > 0: T -= 1 print(solve()) main()
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$). For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$. Calculate the number of good subarrays of the array $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$)Β β€” the length of the array $a$. The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the number of good subarrays of the array $a$. -----Example----- Input 3 3 120 5 11011 6 600005 Output 3 6 1 -----Note----- The first test case is considered in the statement. In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$. In the third test case there is only one good subarray: $a_{2 \dots 6}$.
def fact(n): f = 1 for i in range(1, n + 1): f *= i return f def ncr(n, r): if r > n: return 0 if n - r < r: r = n - r f = 1 for i in range(n, n - r, -1): f *= i return f // fact(r) def subarrays(arr): counts = {(0): 1} summ = 0 for i in range(len(arr)): summ += arr[i] counts[summ - i - 1] = counts.get(summ - i - 1, 0) + 1 arrs = 0 for r in counts.values(): arrs += ncr(r, 2) return arrs t = int(input()) for i in range(t): n = int(input()) s = [int(i) for i in input()] print(subarrays(s))
FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR RETURN VAR FUNC_DEF IF VAR VAR RETURN NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER VAR VAR RETURN BIN_OP VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$). For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$. Calculate the number of good subarrays of the array $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$)Β β€” the length of the array $a$. The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the number of good subarrays of the array $a$. -----Example----- Input 3 3 120 5 11011 6 600005 Output 3 6 1 -----Note----- The first test case is considered in the statement. In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$. In the third test case there is only one good subarray: $a_{2 \dots 6}$.
import sys def minp(): return sys.stdin.readline().strip() def mint(): return int(minp()) def mints(): return map(int, minp().split()) def solve(): n = mint() s = minp() d = dict() a = [0] * (n + 1) p = 0 d[0] = 1 for i in range(n): p += int(s[i]) - 1 a[i + 1] = p d[a[i + 1]] = d.get(a[i + 1], 0) + 1 r = 0 for v in a: d[v] = d.get(v, 0) - 1 r += d.get(v, 0) print(r) for i in range(mint()): solve()
IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$). For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$. Calculate the number of good subarrays of the array $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$)Β β€” the length of the array $a$. The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the number of good subarrays of the array $a$. -----Example----- Input 3 3 120 5 11011 6 600005 Output 3 6 1 -----Note----- The first test case is considered in the statement. In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$. In the third test case there is only one good subarray: $a_{2 \dots 6}$.
t = int(input()) for _ in range(t): n = int(input()) a = input() a = [(int(i) - 1) for i in a] sum, mp = 0, {} for i in range(n): sum += a[i] mp[sum] = 0 mp[0], sum, res = 1, 0, 0 for i in range(n): sum += a[i] res += mp[sum] mp[sum] += 1 print(res)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER DICT FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$). For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$. Calculate the number of good subarrays of the array $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$)Β β€” the length of the array $a$. The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the number of good subarrays of the array $a$. -----Example----- Input 3 3 120 5 11011 6 600005 Output 3 6 1 -----Note----- The first test case is considered in the statement. In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$. In the third test case there is only one good subarray: $a_{2 \dots 6}$.
def main(): t = int(input()) for tt in range(t): n = int(input()) a = list(map(int, input())) s = 0 c = 0 d = {(0): 1} for i in range(0, len(a)): s = s + (a[i] - 1) ss = s - 0 if ss in d.keys(): c = c + d[ss] d[s] = d.get(s, 0) + 1 print(c) main()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$). For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$. Calculate the number of good subarrays of the array $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$)Β β€” the length of the array $a$. The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the number of good subarrays of the array $a$. -----Example----- Input 3 3 120 5 11011 6 600005 Output 3 6 1 -----Note----- The first test case is considered in the statement. In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$. In the third test case there is only one good subarray: $a_{2 \dots 6}$.
def find(arr, n): d = {} sm = 0 cnt = 0 for i in range(n): sm += arr[i] if sm == 0: cnt += 1 if d.get(sm): cnt += d[sm] d[sm] = d.get(sm, 0) + 1 return cnt t = int(input()) for _ in range(t): n = int(input()) s = list(input()) ans = [] for i in range(n): if s[i] == "1": ans.append(0) elif s[i] == "0": ans.append(-1) else: ans.append(int(s[i]) - 1) print(find(ans, n))
FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR NUMBER IF VAR VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$). For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$. Calculate the number of good subarrays of the array $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$)Β β€” the length of the array $a$. The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the number of good subarrays of the array $a$. -----Example----- Input 3 3 120 5 11011 6 600005 Output 3 6 1 -----Note----- The first test case is considered in the statement. In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$. In the third test case there is only one good subarray: $a_{2 \dots 6}$.
def main(): t = int(input()) for _ in range(t): n = int(input().strip()) s = input().strip() a = [int(s[0]) - 1] for i in range(1, n): d = int(s[i]) - 1 a.append(a[-1] + d) ma = {(0): 1} ret = 0 for i in range(n): ret += ma.get(a[i], 0) ma[a[i]] = ma.get(a[i], 0) + 1 print(ret) main()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$). For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$. Calculate the number of good subarrays of the array $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$)Β β€” the length of the array $a$. The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the number of good subarrays of the array $a$. -----Example----- Input 3 3 120 5 11011 6 600005 Output 3 6 1 -----Note----- The first test case is considered in the statement. In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$. In the third test case there is only one good subarray: $a_{2 \dots 6}$.
t = int(input()) for tt in range(t): n = int(input()) s = input() a = [] for i in range(n): a.append(int(s[i])) p = [(0) for i in range(n)] p[0] = a[0] for i in range(1, n): p[i] = p[i - 1] + a[i] cnt = {(0): 1} ans = 0 for i in range(n): x = p[i] - i - 1 if x not in cnt: cnt[x] = 0 cnt[x] += 1 ans += cnt[x] - 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$). For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$. Calculate the number of good subarrays of the array $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$)Β β€” the length of the array $a$. The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the number of good subarrays of the array $a$. -----Example----- Input 3 3 120 5 11011 6 600005 Output 3 6 1 -----Note----- The first test case is considered in the statement. In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$. In the third test case there is only one good subarray: $a_{2 \dots 6}$.
t = int(input().split()[0]) for case in range(t): n = int(input().split()[0]) s = input().split()[0] curr = 0 ans = 0 prev = {} for i in range(n): curr += int(s[i]) - 1 if curr == 0: ans += 1 if curr in prev: ans += prev[curr] prev[curr] += 1 else: prev[curr] = 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$). For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$. Calculate the number of good subarrays of the array $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$)Β β€” the length of the array $a$. The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the number of good subarrays of the array $a$. -----Example----- Input 3 3 120 5 11011 6 600005 Output 3 6 1 -----Note----- The first test case is considered in the statement. In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$. In the third test case there is only one good subarray: $a_{2 \dots 6}$.
for nt in range(int(input())): n = int(input()) a = list(map(int, list(input()))) p = [a[0]] for i in range(1, n): p.append(p[-1] + a[i]) d = {} ans = 0 diff = 0 for i in range(n): if -diff in d: d[-diff] += 1 else: d[-diff] = 1 diff += a[i] - 1 if -diff in d: ans += d[-diff] print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$). For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$. Calculate the number of good subarrays of the array $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$)Β β€” the length of the array $a$. The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the number of good subarrays of the array $a$. -----Example----- Input 3 3 120 5 11011 6 600005 Output 3 6 1 -----Note----- The first test case is considered in the statement. In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$. In the third test case there is only one good subarray: $a_{2 \dots 6}$.
for _ in range(int(input())): n = int(input()) s = input() dp = [0] * (9 * n) dp[0] = 1 a = 0 lol = 0 for i in range(n): a += int(s[i]) lol += dp[a - i - 1] dp[a - i - 1] += 1 print(lol)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$). For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$. Calculate the number of good subarrays of the array $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$)Β β€” the length of the array $a$. The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the number of good subarrays of the array $a$. -----Example----- Input 3 3 120 5 11011 6 600005 Output 3 6 1 -----Note----- The first test case is considered in the statement. In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$. In the third test case there is only one good subarray: $a_{2 \dots 6}$.
T = int(input()) for _ in range(0, T): n = int(input()) arr = input() s = [] for i in range(0, len(arr)): s.append(int(arr[i])) pre = [s[0]] for i in range(1, len(s)): pre.append(pre[-1] + s[i]) kk = [0] * (n + 1) diff = dict() org = n exp = 1 for i in range(len(s) - 1, -1, -1): if s[i] != 1: fnd = 0 if i > 0: fnd = org - 1 - pre[i - 1] if diff.get(fnd) == None: kk[i] = n else: kk[i] = diff[fnd] diff[org - pre[i]] = i exp += 1 org -= 1 loss = [0] * (n + 2) exp = 1 ans = 0 for i in range(len(s) - 1, -1, -1): if s[i] == 1: loss[i] = loss[i + 1] else: loss[i] = kk[i] - i + loss[kk[i] + 1] ans += exp - loss[i] exp += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NONE ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$). For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$. Calculate the number of good subarrays of the array $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$)Β β€” the length of the array $a$. The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the number of good subarrays of the array $a$. -----Example----- Input 3 3 120 5 11011 6 600005 Output 3 6 1 -----Note----- The first test case is considered in the statement. In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$. In the third test case there is only one good subarray: $a_{2 \dots 6}$.
list_ans = [] for _ in range(int(input())): n = int(input()) dict1 = {} list1 = [0] + list(int(x) for x in input()) for i in range(1, n + 1): list1[i] = list1[i] + list1[i - 1] for i in range(n + 1): list1[i] -= i for x in list1: dict1[x] = dict1.get(x, 0) + 1 ans = 0 for s in dict1.values(): ans += s * (s - 1) // 2 list_ans.append(ans) print(*list_ans, sep="\n")
ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR STRING
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$). For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$. Calculate the number of good subarrays of the array $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$)Β β€” the length of the array $a$. The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the number of good subarrays of the array $a$. -----Example----- Input 3 3 120 5 11011 6 600005 Output 3 6 1 -----Note----- The first test case is considered in the statement. In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$. In the third test case there is only one good subarray: $a_{2 \dots 6}$.
try: for _ in range(int(input())): a = int(input()) b = [int(i) for i in input()] ans = 0 c = [] + b for i in range(1, a): c[i] += c[i - 1] f = [] + c for i in range(a): f[i] -= i there = dict() for j in range(a): if f[j] == 1: ans += 1 if f[j] in there: ans += there[f[j]] else: there[f[j]] = 0 there[f[j]] += 1 print(ans) except Exception as e: print(e)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$). For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$. Calculate the number of good subarrays of the array $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$)Β β€” the length of the array $a$. The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the number of good subarrays of the array $a$. -----Example----- Input 3 3 120 5 11011 6 600005 Output 3 6 1 -----Note----- The first test case is considered in the statement. In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$. In the third test case there is only one good subarray: $a_{2 \dots 6}$.
for _ in range(int(input())): n = int(input()) w = input() dic = {(0): 1} s, ans = 0, 0 for i in range(n): s += int(w[i]) x = s - i - 1 dic[x] = dic.get(x, 0) + 1 ans += dic[x] - 1 print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$). For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$. Calculate the number of good subarrays of the array $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$)Β β€” the length of the array $a$. The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the number of good subarrays of the array $a$. -----Example----- Input 3 3 120 5 11011 6 600005 Output 3 6 1 -----Note----- The first test case is considered in the statement. In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$. In the third test case there is only one good subarray: $a_{2 \dots 6}$.
for _ in range(int(input())): n = int(input()) a = list(map(int, input())) x_pos = [0] * n cum_pos = [0] * n cum = 0 ans = 0 for i in range(n): cum += a[i] x_pos[i] = a[i] - i cum_pos[i] = cum - i occurences = {(1): 1} for i in range(n): if cum_pos[i] in occurences: occurences[cum_pos[i]] += 1 else: occurences[cum_pos[i]] = 1 for x in occurences.values(): ans += x * (x - 1) // 2 print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR ASSIGN VAR DICT NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$). For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$. Calculate the number of good subarrays of the array $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$)Β β€” the length of the array $a$. The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the number of good subarrays of the array $a$. -----Example----- Input 3 3 120 5 11011 6 600005 Output 3 6 1 -----Note----- The first test case is considered in the statement. In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$. In the third test case there is only one good subarray: $a_{2 \dots 6}$.
def solve(): n = int(input()) s = input() dp = [0] * (n * 10) dp[0] = 1 check = 0 ans = 0 for i in range(n): check += int(s[i]) x = check - i - 1 dp[x] += 1 ans += dp[x] - 1 print(ans) for _ in range(int(input())): solve()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$). For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$. Calculate the number of good subarrays of the array $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$)Β β€” the length of the array $a$. The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the number of good subarrays of the array $a$. -----Example----- Input 3 3 120 5 11011 6 600005 Output 3 6 1 -----Note----- The first test case is considered in the statement. In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$. In the third test case there is only one good subarray: $a_{2 \dots 6}$.
from itertools import accumulate, combinations, combinations_with_replacement for _ in range(int(input())): n = int(input()) arr = [int(i) for i in input()] arr[0] -= 1 d = {(0): 1} res = 0 for idx, s in enumerate(accumulate(arr)): x = s - idx d[x] = d.get(x, 0) res += d[x] d[x] += 1 print(res)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$). For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$. Calculate the number of good subarrays of the array $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$)Β β€” the length of the array $a$. The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the number of good subarrays of the array $a$. -----Example----- Input 3 3 120 5 11011 6 600005 Output 3 6 1 -----Note----- The first test case is considered in the statement. In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$. In the third test case there is only one good subarray: $a_{2 \dots 6}$.
import sys input = sys.stdin.readline t = int(input()) for t1 in range(t): n = int(input()) s = input() s = s.strip() l = [] for i in range(n): l.append(int(s[i])) for i in range(n): l[i] -= 1 pre = [0] * n pre[0] = l[0] d = {} c = 0 for i in range(n): if i == 0: if pre[0] == 0: c += 1 d[pre[0]] = d.get(pre[0], 0) + 1 else: pre[i] = pre[i - 1] + l[i] if pre[i] == 0: c += 1 c += d.get(pre[i], 0) d[pre[i]] = d.get(pre[i], 0) + 1 print(c)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$). For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$. Calculate the number of good subarrays of the array $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$)Β β€” the length of the array $a$. The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the number of good subarrays of the array $a$. -----Example----- Input 3 3 120 5 11011 6 600005 Output 3 6 1 -----Note----- The first test case is considered in the statement. In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$. In the third test case there is only one good subarray: $a_{2 \dots 6}$.
t = int(input()) for _ in range(t): n = int(input()) a = [int(i) for i in list(input())] s = 0 dp = [0] * (9 * n) cnt = 0 dp[0] = 1 for i in range(n): s += a[i] cnt += dp[s - i - 1] dp[s - i - 1] += 1 print(cnt)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$). For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$. Calculate the number of good subarrays of the array $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$)Β β€” the length of the array $a$. The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the number of good subarrays of the array $a$. -----Example----- Input 3 3 120 5 11011 6 600005 Output 3 6 1 -----Note----- The first test case is considered in the statement. In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$. In the third test case there is only one good subarray: $a_{2 \dots 6}$.
t = int(input()) a = [] for i in range(t): n = int(input()) s = input() a = [] for i in range(n): g = int(s[i]) a.append(g - 1) if len(a) > 1: a[len(a) - 1] = a[len(a) - 2] + a[len(a) - 1] a.sort() d = 1 if a[0] == 0: t = 1 else: t = 0 for i in range(1, len(a)): if a[i] != a[i - 1] or i == len(a) - 1: if i == len(a) - 1 and a[i] == a[i - 1]: d += 1 if d > 1: t = t + d * (d - 1) // 2 d = 1 else: d += 1 if a[i] == 0: t += 1 print(t)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$). For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$. Calculate the number of good subarrays of the array $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$)Β β€” the length of the array $a$. The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the number of good subarrays of the array $a$. -----Example----- Input 3 3 120 5 11011 6 600005 Output 3 6 1 -----Note----- The first test case is considered in the statement. In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$. In the third test case there is only one good subarray: $a_{2 \dots 6}$.
n_inputs = int(input()) for _ in range(n_inputs): str_len = int(input()) str_input = input() sums = 0 sum_dict = {(0): 1} current_sum = 0 for char in str_input: current_sum += int(char) - 1 if current_sum in sum_dict: sum_dict[current_sum] += 1 else: sum_dict[current_sum] = 1 ans = 0 for n_occur in sum_dict.values(): ans += n_occur * (n_occur - 1) // 2 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$). For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$. Calculate the number of good subarrays of the array $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$)Β β€” the length of the array $a$. The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the number of good subarrays of the array $a$. -----Example----- Input 3 3 120 5 11011 6 600005 Output 3 6 1 -----Note----- The first test case is considered in the statement. In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$. In the third test case there is only one good subarray: $a_{2 \dots 6}$.
def countPairs(arr, n): map = dict() for i in range(n): map[arr[i] - i] = map.get(arr[i] - i, 0) + 1 res = 0 for x in map: cnt = map[x] res += cnt * (cnt - 1) // 2 if 1 in map: return res + map[1] else: return res t = int(input()) for _ in range(t): n = int(input()) a = list(input()) for i in range(n): a[i] = int(a[i]) prefix = [a[0]] for i in range(1, n): prefix.append(prefix[-1] + a[i]) print(countPairs(prefix, n))
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF NUMBER VAR RETURN BIN_OP VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$). For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$. Calculate the number of good subarrays of the array $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$)Β β€” the length of the array $a$. The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the number of good subarrays of the array $a$. -----Example----- Input 3 3 120 5 11011 6 600005 Output 3 6 1 -----Note----- The first test case is considered in the statement. In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$. In the third test case there is only one good subarray: $a_{2 \dots 6}$.
for _ in range(int(input())): sequence_length = int(input()) sequence = input() partial_sums = {(1): int(sequence[0])} temp_sum = partial_sums[1] for index in range(1, sequence_length): temp_sum += int(sequence[index]) partial_sums[index + 1] = temp_sum good_sublines = 0 prefixes = {(0): 1} for index in range(1, sequence_length + 1): prefix = partial_sums[index] - index if prefix not in prefixes: prefixes[prefix] = 0 prefixes[prefix] += 1 for prefix in prefixes: if prefixes[prefix] >= 2: good_sublines += prefixes[prefix] * (prefixes[prefix] - 1) // 2 print(good_sublines)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER FOR VAR VAR IF VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$). For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$. Calculate the number of good subarrays of the array $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$)Β β€” the length of the array $a$. The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the number of good subarrays of the array $a$. -----Example----- Input 3 3 120 5 11011 6 600005 Output 3 6 1 -----Note----- The first test case is considered in the statement. In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$. In the third test case there is only one good subarray: $a_{2 \dots 6}$.
t = int(input()) for i in range(t): n = int(input()) a1 = input() a = [int(x) for x in a1] s1 = [0] c = 0 for j in range(n): c = c + a[j] s1.append(c - j - 1) d = {} for k in s1: if k in d: d[k] += 1 else: d[k] = 1 ans = 0 for y in list(d.values()): ans += y * (y - 1) / 2 print(int(ans))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$). For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$. Calculate the number of good subarrays of the array $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$)Β β€” the length of the array $a$. The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the number of good subarrays of the array $a$. -----Example----- Input 3 3 120 5 11011 6 600005 Output 3 6 1 -----Note----- The first test case is considered in the statement. In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$. In the third test case there is only one good subarray: $a_{2 \dots 6}$.
t = int(input()) for _ in range(t): n = int(input()) a = input() memo = {(0): 1} res = 0 prefix_sum = 0 for i in range(len(a)): prefix_sum += int(a[i]) res += memo.get(prefix_sum - i - 1, 0) memo[prefix_sum - i - 1] = memo.get(prefix_sum - i - 1, 0) + 1 print(res)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$). For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$. Calculate the number of good subarrays of the array $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$)Β β€” the length of the array $a$. The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the number of good subarrays of the array $a$. -----Example----- Input 3 3 120 5 11011 6 600005 Output 3 6 1 -----Note----- The first test case is considered in the statement. In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$. In the third test case there is only one good subarray: $a_{2 \dots 6}$.
T = int(input()) r = list() for _ in range(T): n = int(input()) a = list(map(int, input())) d = {(0): 1} res, s = 0, 0 for i in range(n): s += int(a[i]) x = s - i - 1 if x not in d: d[x] = 0 d[x] += 1 res += d[x] - 1 r.append(res) for i in r: print(i)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$). For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$. Calculate the number of good subarrays of the array $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$)Β β€” the length of the array $a$. The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the number of good subarrays of the array $a$. -----Example----- Input 3 3 120 5 11011 6 600005 Output 3 6 1 -----Note----- The first test case is considered in the statement. In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$. In the third test case there is only one good subarray: $a_{2 \dots 6}$.
for _ in range(int(input())): am = int(input()) arr = list(map(int, list(input()))) p = [0] for i in range(am): p.append(p[-1] + arr[i] - 1) obj = {} for i in range(am + 1): if p[i] in obj: obj[p[i]] += 1 else: obj[p[i]] = 1 c = 0 for i, d in obj.items(): c += d * (d - 1) // 2 print(c)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$). For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$. Calculate the number of good subarrays of the array $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$)Β β€” the length of the array $a$. The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the number of good subarrays of the array $a$. -----Example----- Input 3 3 120 5 11011 6 600005 Output 3 6 1 -----Note----- The first test case is considered in the statement. In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$. In the third test case there is only one good subarray: $a_{2 \dots 6}$.
for _ in range(int(input())): n = int(input()) ar = list(map(int, list(input()))) li = [] su = 0 for i in range(n): su = su - (ar[i] - 1) li.append(su) dic = {(0): 1} for i in li: if i in dic: dic[i] += 1 else: dic[i] = 1 ans = 0 for i in dic: ans += (dic[i] - 1) * dic[i] // 2 print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR DICT NUMBER NUMBER FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$). For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$. Calculate the number of good subarrays of the array $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$)Β β€” the length of the array $a$. The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the number of good subarrays of the array $a$. -----Example----- Input 3 3 120 5 11011 6 600005 Output 3 6 1 -----Note----- The first test case is considered in the statement. In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$. In the third test case there is only one good subarray: $a_{2 \dots 6}$.
import sys def i(): return sys.stdin.readline()[:-1] def listDigits(): return list(map(lambda x: int(x) - 1, i())) cases = int(i()) for x in range(cases): n = int(i()) digits = listDigits() goodSubArrays = 0 sums = {} for y, digit in enumerate(digits): if digit == 0: goodSubArrays += 1 if -digit in sums.keys(): goodSubArrays += sums[-digit] newSums = {} for key in sums: if -200 < key + digit < 150: newSums[key + digit] = sums[key] if digit in newSums.keys(): newSums[digit] += 1 else: newSums[digit] = 1 sums = newSums print(goodSubArrays)
IMPORT FUNC_DEF RETURN FUNC_CALL VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR DICT FOR VAR VAR IF NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$). For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$. Calculate the number of good subarrays of the array $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$)Β β€” the length of the array $a$. The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the number of good subarrays of the array $a$. -----Example----- Input 3 3 120 5 11011 6 600005 Output 3 6 1 -----Note----- The first test case is considered in the statement. In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$. In the third test case there is only one good subarray: $a_{2 \dots 6}$.
for _ in range(int(input())): n = int(input()) s = input() dp = [0, int(s[0])] for i in range(1, n): dp.append(int(s[i]) + dp[-1]) d = {} for i in range(len(dp)): tmp = dp[i] - i if tmp in d: d[tmp] += 1 else: d[tmp] = 1 ans = 0 for i in d: ans += d[i] * (d[i] - 1) // 2 print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$). For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$. Calculate the number of good subarrays of the array $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$)Β β€” the length of the array $a$. The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the number of good subarrays of the array $a$. -----Example----- Input 3 3 120 5 11011 6 600005 Output 3 6 1 -----Note----- The first test case is considered in the statement. In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$. In the third test case there is only one good subarray: $a_{2 \dots 6}$.
for q in range(int(input())): input() numList = [] for i in input(): numList.append(int(i) - 1) sumDict = {(0): 1} tally = 0 subLists = 0 for i in numList: tally += int(i) if tally in sumDict: subLists += sumDict[tally] sumDict[tally] += 1 else: sumDict[tally] = 1 print(str(subLists) + "\n")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$). For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$. Calculate the number of good subarrays of the array $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$)Β β€” the length of the array $a$. The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the number of good subarrays of the array $a$. -----Example----- Input 3 3 120 5 11011 6 600005 Output 3 6 1 -----Note----- The first test case is considered in the statement. In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$. In the third test case there is only one good subarray: $a_{2 \dots 6}$.
for t in range(int(input())): n = int(input()) l = list(map(int, list(input()))) ans = 0 d = {(0): 1} csl = 0 for i in range(n): csl += l[i] - 1 if csl in d: ans += d[csl] if csl in d: d[csl] += 1 else: d[csl] = 1 print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$). For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$. Calculate the number of good subarrays of the array $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$)Β β€” the length of the array $a$. The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the number of good subarrays of the array $a$. -----Example----- Input 3 3 120 5 11011 6 600005 Output 3 6 1 -----Note----- The first test case is considered in the statement. In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$. In the third test case there is only one good subarray: $a_{2 \dots 6}$.
def nc2(x): if x < 2: return 0 return round(x * (x - 1) / 2) t = int(input()) for tt in range(t): n = int(input()) s = input() a = [(int(x) - 1) for x in s] b = [0] for i in range(n): b.append(b[-1] + a[i]) b.sort() pcks = [[b[0]]] for i in range(1, len(b)): if pcks[-1][0] == b[i]: pcks[-1].append(b[i]) else: pcks.append([b[i]]) lens = [len(x) for x in pcks] q = 0 for x in lens: q += nc2(x) print(q)
FUNC_DEF IF VAR NUMBER RETURN NUMBER RETURN FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$). For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$. Calculate the number of good subarrays of the array $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$)Β β€” the length of the array $a$. The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the number of good subarrays of the array $a$. -----Example----- Input 3 3 120 5 11011 6 600005 Output 3 6 1 -----Note----- The first test case is considered in the statement. In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$. In the third test case there is only one good subarray: $a_{2 \dots 6}$.
t = int(input()) for te in range(t): n = int(input()) s = "0" + input() psum = [0] for i in range(1, n + 1): x = psum[i - 1] + int(s[i]) psum.append(x) cnt = {} cnt[0] = 1 ans = 0 for i in range(1, n + 1): if psum[i] - i in cnt: ans += cnt[psum[i] - i] cnt[psum[i] - i] += 1 else: cnt[psum[i] - i] = 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP STRING FUNC_CALL VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$). For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$. Calculate the number of good subarrays of the array $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$)Β β€” the length of the array $a$. The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the number of good subarrays of the array $a$. -----Example----- Input 3 3 120 5 11011 6 600005 Output 3 6 1 -----Note----- The first test case is considered in the statement. In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$. In the third test case there is only one good subarray: $a_{2 \dots 6}$.
import sys input = sys.stdin.readline def inInt(): return int(input()) def inStr(): return input().strip("\n") def inIList(): return list(map(int, input().split())) def inSList(): return input().split() def solve(n, case): freq = {} freq[1] = 1 s = 0 res = 0 for i in range(n): s += int(case[i]) if s - i not in freq: freq[s - i] = 0 res += freq[s - i] freq[s - i] += 1 print(res) t = inInt() for case in range(t): n = inInt() case = inStr() solve(n, case)
IMPORT ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$). For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$. Calculate the number of good subarrays of the array $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$)Β β€” the length of the array $a$. The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the number of good subarrays of the array $a$. -----Example----- Input 3 3 120 5 11011 6 600005 Output 3 6 1 -----Note----- The first test case is considered in the statement. In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$. In the third test case there is only one good subarray: $a_{2 \dots 6}$.
def solve(l, n): mp = {} s, ans = 0, 0 for i in range(n): s += l[i] if s == 0: ans += 1 if s in mp: ans += mp[s] mp[s] += 1 else: mp[s] = 1 print(ans) for _ in range(int(input())): n = int(input()) s = str(input()) l = list(s) for i in range(n): l[i] = int(l[i]) - 1 solve(l, n)
FUNC_DEF ASSIGN VAR DICT ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$). For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$. Calculate the number of good subarrays of the array $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$)Β β€” the length of the array $a$. The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the number of good subarrays of the array $a$. -----Example----- Input 3 3 120 5 11011 6 600005 Output 3 6 1 -----Note----- The first test case is considered in the statement. In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$. In the third test case there is only one good subarray: $a_{2 \dots 6}$.
T = int(input()) for _ in range(T): n = int(input()) a = input() sta = 0 end = 0 i = 1 ans = 0 summ = 0 t = [] kk = 0 for i in range(n): kk += int(a[i]) t.append(kk - i - 1) d = {(0): 1} for i in range(n): if d.get(t[i], False): d[t[i]] += 1 else: d[t[i]] = 1 for i in d: ans += d[i] * (d[i] - 1) // 2 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$). For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$. Calculate the number of good subarrays of the array $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$)Β β€” the length of the array $a$. The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the number of good subarrays of the array $a$. -----Example----- Input 3 3 120 5 11011 6 600005 Output 3 6 1 -----Note----- The first test case is considered in the statement. In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$. In the third test case there is only one good subarray: $a_{2 \dots 6}$.
q = int(input()) for _ in range(q): n = int(input()) A = list(int(x) for x in input()) d = {(0): 1} s = 0 ans = 0 for i, a in enumerate(A): s += a x = s - i - 1 if x in d: d[x] += 1 else: d[x] = 1 for k, v in d.items(): ans += v * (v - 1) // 2 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$). For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$. Calculate the number of good subarrays of the array $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$)Β β€” the length of the array $a$. The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the number of good subarrays of the array $a$. -----Example----- Input 3 3 120 5 11011 6 600005 Output 3 6 1 -----Note----- The first test case is considered in the statement. In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$. In the third test case there is only one good subarray: $a_{2 \dots 6}$.
tests = int(input()) n = [] s = [] for _ in range(tests): n.append(int(input())) s.append(list(map(int, input()))) for t in range(tests): good = 0 hash = {} hash[0] = 1 prefix_sum = 0 sub_len = 0 for i in range(n[t]): prefix_sum += s[t][i] sub_len = i + 1 if prefix_sum - sub_len in hash: good += hash[prefix_sum - sub_len] hash[prefix_sum - sub_len] += 1 else: hash[prefix_sum - sub_len] = 1 print(good)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$). For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$. Calculate the number of good subarrays of the array $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$)Β β€” the length of the array $a$. The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the number of good subarrays of the array $a$. -----Example----- Input 3 3 120 5 11011 6 600005 Output 3 6 1 -----Note----- The first test case is considered in the statement. In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$. In the third test case there is only one good subarray: $a_{2 \dots 6}$.
import sys def input(): return sys.stdin.readline().rstrip() def input_split(): return [int(i) for i in input().split()] testCases = int(input()) answers = [] for _ in range(testCases): n = int(input()) arr = [int(i) for i in input()] d = {} ans = 0 current = 0 for i, a in enumerate(arr): current += a extra = current - (i + 1) if extra in d: d[extra] += 1 else: d[extra] = 1 val = 0 if 0 in d: ans += d[val] till_now = {} current = 0 for i, a in enumerate(arr): current += a extra = current - (i + 1) val = extra if extra in till_now: till_now[extra] += 1 else: till_now[extra] = 1 if val in d and val in till_now: ans += d[val] - till_now[val] elif val in d: ans += d[val] answers.append(ans) print(*answers, sep="\n")
IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER IF NUMBER VAR VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR STRING
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$). For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$. Calculate the number of good subarrays of the array $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$)Β β€” the length of the array $a$. The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the number of good subarrays of the array $a$. -----Example----- Input 3 3 120 5 11011 6 600005 Output 3 6 1 -----Note----- The first test case is considered in the statement. In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$. In the third test case there is only one good subarray: $a_{2 \dots 6}$.
t = int(input()) for q in range(t): n = int(input()) ch = input() L = [int(i) for i in ch] L2 = [0] for i in range(n): L2.append(L2[-1] + L[i] - 1) L2.sort() nb = 0 L3 = [] for i in range(1, n + 1): if L2[i] != L2[i - 1]: L3.append(nb) nb = 0 else: nb += 1 L3.append(nb) for i in range(len(L3)): L3[i] = L3[i] * (L3[i] + 1) // 2 print(sum(L3))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$). For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$. Calculate the number of good subarrays of the array $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$)Β β€” the length of the array $a$. The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the number of good subarrays of the array $a$. -----Example----- Input 3 3 120 5 11011 6 600005 Output 3 6 1 -----Note----- The first test case is considered in the statement. In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$. In the third test case there is only one good subarray: $a_{2 \dots 6}$.
for _ in range(int(input())): n = int(input()) s = input() a = "" for i in s: a += chr(ord(i) - 1) d = {(0): 1} s = 0 ans = 0 for i in a: s += ord(i) - ord("0") if s in d: ans += d[s] d[s] += 1 else: d[s] = 1 print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING FOR VAR VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING IF VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$). For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$. Calculate the number of good subarrays of the array $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$)Β β€” the length of the array $a$. The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the number of good subarrays of the array $a$. -----Example----- Input 3 3 120 5 11011 6 600005 Output 3 6 1 -----Note----- The first test case is considered in the statement. In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$. In the third test case there is only one good subarray: $a_{2 \dots 6}$.
t = int(input()) for _ in range(t): n = int(input()) a = list(map(int, list(input()))) sa = [0] * (n + 1) C = dict() C[0] = 1 for i in range(n): sa[i + 1] = sa[i] + a[i] if sa[i + 1] - i - 1 in C: C[sa[i + 1] - i - 1] += 1 else: C[sa[i + 1] - i - 1] = 1 ans = 0 for x in C: ans += C[x] * (C[x] - 1) // 2 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR IF BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$). For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$. Calculate the number of good subarrays of the array $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$)Β β€” the length of the array $a$. The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the number of good subarrays of the array $a$. -----Example----- Input 3 3 120 5 11011 6 600005 Output 3 6 1 -----Note----- The first test case is considered in the statement. In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$. In the third test case there is only one good subarray: $a_{2 \dots 6}$.
gans = [] for _ in range(int(input())): n = int(input()) u = list(map(int, list(input()))) d = [0] * n mx = 10 * n + 1 cnst = n cnt = [0] * mx ans = 0 d[0] = u[0] - 1 if d[0] == 0: ans += 1 cnt[d[0] + cnst] += 1 for i in range(1, n): d[i] = d[i - 1] + u[i] - 1 ans += cnt[d[i] + cnst] if d[i] == 0: ans += 1 cnt[d[i] + cnst] += 1 gans.append(ans) print("\n".join(map(str, gans)))
ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR NUMBER NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR BIN_OP VAR VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$). For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$. Calculate the number of good subarrays of the array $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$)Β β€” the length of the array $a$. The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the number of good subarrays of the array $a$. -----Example----- Input 3 3 120 5 11011 6 600005 Output 3 6 1 -----Note----- The first test case is considered in the statement. In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$. In the third test case there is only one good subarray: $a_{2 \dots 6}$.
for _ in range(int(input())): w = int(input()) s = input() + "0" A = [] z = 0 for i in range(len(s)): A.append(z) z += int(s[i]) - 1 A.sort() Ans = 0 q = 1 for i in range(1, len(A)): if A[i] == A[i - 1]: q += 1 else: Ans += (q - 1) * q // 2 q = 1 Ans += (q - 1) * q // 2 print(Ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR STRING ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$). For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$. Calculate the number of good subarrays of the array $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$)Β β€” the length of the array $a$. The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the number of good subarrays of the array $a$. -----Example----- Input 3 3 120 5 11011 6 600005 Output 3 6 1 -----Note----- The first test case is considered in the statement. In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$. In the third test case there is only one good subarray: $a_{2 \dots 6}$.
for t in range(int(input())): n = int(input()) s = input() a = [0] last = 0 for i in range(n): if int(s[i]) == 0: a.append(a[i] - 1) else: a.append(a[i] + (int(s[i]) - 1)) rama = [] b = {} b[0] = 1 for i in range(1, n + 1): if a[i] in b: b[a[i]] += 1 if a[i] not in b: if int(s[i - 1]) == 1: b[a[i]] = 2 else: b[a[i]] = 1 add = 0 for i in b: d = b[i] for i in range(1, d): add += i print(add)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR DICT ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$). For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$. Calculate the number of good subarrays of the array $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$)Β β€” the length of the array $a$. The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the number of good subarrays of the array $a$. -----Example----- Input 3 3 120 5 11011 6 600005 Output 3 6 1 -----Note----- The first test case is considered in the statement. In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$. In the third test case there is only one good subarray: $a_{2 \dots 6}$.
t = int(input()) C = [(0) for i in range(900001)] C[0] = 1 for _ in range(t): n = int(input()) A = [int(s) for s in input()] ans, cur = 0, 0 for i in range(n): cur += A[i] - 1 ans += C[cur] C[cur] += 1 cur = 0 for i in range(n): cur += A[i] - 1 C[cur] -= 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$). For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$. Calculate the number of good subarrays of the array $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$)Β β€” the length of the array $a$. The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the number of good subarrays of the array $a$. -----Example----- Input 3 3 120 5 11011 6 600005 Output 3 6 1 -----Note----- The first test case is considered in the statement. In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$. In the third test case there is only one good subarray: $a_{2 \dots 6}$.
test_cases = int(input()) for test_case in range(test_cases): n = int(input()) s = input() sum = [] count = 0 ans = 0 a = list(map(int, list(s))) for i in range(n): a[i] = a[i] - 1 count += a[i] sum.append(count) d = {(0): 1} for i in sum: if not i in d: d[i] = 0 d[i] += 1 ans += d[i] - 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR DICT NUMBER NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$). For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$. Calculate the number of good subarrays of the array $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$)Β β€” the length of the array $a$. The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the number of good subarrays of the array $a$. -----Example----- Input 3 3 120 5 11011 6 600005 Output 3 6 1 -----Note----- The first test case is considered in the statement. In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$. In the third test case there is only one good subarray: $a_{2 \dots 6}$.
import sys LI = lambda: list(map(int, sys.stdin.readline().strip("\n").split())) MI = lambda: map(int, sys.stdin.readline().strip("\n").split()) SI = lambda: sys.stdin.readline().strip("\n") II = lambda: int(sys.stdin.readline().strip("\n")) for _ in range(II()): n = II() s = [0] + [(ord(i) - ord("0")) for i in SI()] cnt = {} for i in range(1, n + 1): s[i] += s[i - 1] for i, v in enumerate(s): cnt[v - i] = cnt.get(v - i, 0) + 1 ans = 0 for v in cnt.keys(): ans += cnt[v] * (cnt[v] - 1) // 2 print(ans)
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$). For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$. Calculate the number of good subarrays of the array $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$)Β β€” the length of the array $a$. The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the number of good subarrays of the array $a$. -----Example----- Input 3 3 120 5 11011 6 600005 Output 3 6 1 -----Note----- The first test case is considered in the statement. In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$. In the third test case there is only one good subarray: $a_{2 \dots 6}$.
for _ in range(int(input())): n = int(input()) st = input() prev = 0 ans = 0 dict = {} for i in range(n): prev += int(st[i]) - 1 if dict.get(prev, None) != None: if prev == 0: ans += 1 ans += dict[prev] dict[prev] += 1 else: if prev == 0: ans += 1 dict[prev] = 1 print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NONE NONE IF VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$). For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$. Calculate the number of good subarrays of the array $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$)Β β€” the length of the array $a$. The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the number of good subarrays of the array $a$. -----Example----- Input 3 3 120 5 11011 6 600005 Output 3 6 1 -----Note----- The first test case is considered in the statement. In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$. In the third test case there is only one good subarray: $a_{2 \dots 6}$.
for _ in range(int(input())): n = int(input()) arr = list(map(int, list(input()))) cnt = {} cnt[0] = 1 sum = 0 answer = 0 for i in range(0, n): sum += arr[i] if sum - i - 1 in cnt: answer += cnt[sum - i - 1] cnt[sum - i - 1] += 1 else: cnt[sum - i - 1] = 1 print(answer)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$). For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$. Calculate the number of good subarrays of the array $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$)Β β€” the length of the array $a$. The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the number of good subarrays of the array $a$. -----Example----- Input 3 3 120 5 11011 6 600005 Output 3 6 1 -----Note----- The first test case is considered in the statement. In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$. In the third test case there is only one good subarray: $a_{2 \dots 6}$.
for _ in range(int(input())): n = int(input()) a = [int(i) for i in input()] v = dict() v[1] = 1 s, ans = 0, 0 for i in range(n): s += a[i] try: ans += v[s - i] v[s - i] += 1 except KeyError: v[s - i] = 1 print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$). For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$. Calculate the number of good subarrays of the array $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$)Β β€” the length of the array $a$. The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the number of good subarrays of the array $a$. -----Example----- Input 3 3 120 5 11011 6 600005 Output 3 6 1 -----Note----- The first test case is considered in the statement. In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$. In the third test case there is only one good subarray: $a_{2 \dots 6}$.
t = int(input()) for ii in range(t): n = int(input()) s = list(input()) count = 0 for i in range(n): s[i] = int(s[i]) - 1 m = {} sm = 0 count = 0 for i in range(n): sm += s[i] diff = sm if sm == 0: count += 1 if diff in m: count += m[diff] if sm in m: m[sm] += 1 else: m[sm] = 1 print(count)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR IF VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$). For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$. Calculate the number of good subarrays of the array $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$)Β β€” the length of the array $a$. The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the number of good subarrays of the array $a$. -----Example----- Input 3 3 120 5 11011 6 600005 Output 3 6 1 -----Note----- The first test case is considered in the statement. In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$. In the third test case there is only one good subarray: $a_{2 \dots 6}$.
def fs(arr, n): hashMap = {} out = 0 s = 0 for i in range(n): s += arr[i] if s == 0: out += 1 bk = [] if s in hashMap: bk = hashMap.get(s) out += len(bk) bk.append(i) hashMap[s] = bk return out test = int(input()) for _ in range(test): n = int(input()) arr = list(input()) count = 0 for i in range(n): a = int(arr[i]) arr[i] = a - 1 count = fs(arr, n) print(count)
FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR LIST IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$). For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$. Calculate the number of good subarrays of the array $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$)Β β€” the length of the array $a$. The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the number of good subarrays of the array $a$. -----Example----- Input 3 3 120 5 11011 6 600005 Output 3 6 1 -----Note----- The first test case is considered in the statement. In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$. In the third test case there is only one good subarray: $a_{2 \dots 6}$.
cases = int(input()) for t in range(cases): n = int(input()) a = list(map(int, list(input()))) a = [(i - 1) for i in a] d = {} s = 0 out = 0 for i in range(n): s += a[i] if s == 0: out += 1 if s not in d: d[s] = 1 else: out += d[s] d[s] += 1 print(out)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$). For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$. Calculate the number of good subarrays of the array $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$)Β β€” the length of the array $a$. The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the number of good subarrays of the array $a$. -----Example----- Input 3 3 120 5 11011 6 600005 Output 3 6 1 -----Note----- The first test case is considered in the statement. In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$. In the third test case there is only one good subarray: $a_{2 \dots 6}$.
def multiple_input(): return map(int, input().split()) def list_input(): return list(map(int, input().split())) for _ in range(int(input())): n = int(input()) a = list(map(int, input().strip())) for i in range(n): a[i] -= 1 count = 0 s = 0 d = {} for i in a: s += i if s in d: d[s] += 1 else: d[s] = 1 for i in d: if i == 0: count += d[i] * (d[i] + 1) // 2 else: count += d[i] * (d[i] - 1) // 2 print(count)
FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR VAR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$). For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$. Calculate the number of good subarrays of the array $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$)Β β€” the length of the array $a$. The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the number of good subarrays of the array $a$. -----Example----- Input 3 3 120 5 11011 6 600005 Output 3 6 1 -----Note----- The first test case is considered in the statement. In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$. In the third test case there is only one good subarray: $a_{2 \dots 6}$.
for s in [*open(0)][2::2]: g = {(0): 1} c = a = i = 0 while i + 1 < len(s): c += int(s[i]) i += 1 f = g.get(c - i, 0) a += f g[c - i] = f + 1 print(a)
FOR VAR LIST FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER WHILE BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$). For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$. Calculate the number of good subarrays of the array $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$)Β β€” the length of the array $a$. The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the number of good subarrays of the array $a$. -----Example----- Input 3 3 120 5 11011 6 600005 Output 3 6 1 -----Note----- The first test case is considered in the statement. In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$. In the third test case there is only one good subarray: $a_{2 \dots 6}$.
def anido(n, k, vals): resultado = 0 atual = 0 soma = {(0): 1} i = 0 for x in vals: atual += x try: resultado += soma[atual - k] except: pass if atual in soma.keys(): soma[atual] += 1 else: soma[atual] = 1 i += 1 return resultado tc = int(input()) for _ in range(tc): n = int(input()) s = input() vals = [] for c in s: vals.append(ord(c) - ord("0") - 1) print(anido(n, 0, vals))
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR IF VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$). For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$. Calculate the number of good subarrays of the array $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$)Β β€” the length of the array $a$. The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the number of good subarrays of the array $a$. -----Example----- Input 3 3 120 5 11011 6 600005 Output 3 6 1 -----Note----- The first test case is considered in the statement. In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$. In the third test case there is only one good subarray: $a_{2 \dots 6}$.
for test_i in range(int(input())): n = int(input()) arr = list(map(lambda x: int(x) - 1, list(input()))) pref_arr = [0] sum_cur = 0 for el in arr: sum_cur += el pref_arr.append(sum_cur) pref_arr.sort() ans = 0 count = 1 i = 1 while i <= n: if pref_arr[i] == pref_arr[i - 1]: count += 1 else: ans += count * (count - 1) // 2 count = 1 i += 1 ans += count * (count - 1) // 2 print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$). For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$. Calculate the number of good subarrays of the array $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$)Β β€” the length of the array $a$. The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the number of good subarrays of the array $a$. -----Example----- Input 3 3 120 5 11011 6 600005 Output 3 6 1 -----Note----- The first test case is considered in the statement. In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$. In the third test case there is only one good subarray: $a_{2 \dots 6}$.
t = int(input()) for _ in range(t): n = int(input()) s = input() a = [0] * (10 * n) ans = 0 pre = 0 a[pre] = 1 for i in range(n): pre += int(s[i]) - 1 ans += a[pre] a[pre] += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$). For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$. Calculate the number of good subarrays of the array $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$)Β β€” the length of the array $a$. The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the number of good subarrays of the array $a$. -----Example----- Input 3 3 120 5 11011 6 600005 Output 3 6 1 -----Note----- The first test case is considered in the statement. In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$. In the third test case there is only one good subarray: $a_{2 \dots 6}$.
N = int(input()) for i in range(N): num = int(input()) lst = list(map(int, input().strip())) res, dic, temp = 0, {(0): 1}, 0 for j in lst: temp += j - 1 if temp in dic: res += dic[temp] dic[temp] += 1 else: dic[temp] = 1 print(res)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER DICT NUMBER NUMBER NUMBER FOR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$). For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$. Calculate the number of good subarrays of the array $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$)Β β€” the length of the array $a$. The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the number of good subarrays of the array $a$. -----Example----- Input 3 3 120 5 11011 6 600005 Output 3 6 1 -----Note----- The first test case is considered in the statement. In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$. In the third test case there is only one good subarray: $a_{2 \dots 6}$.
T = int(input()) for t in range(T): n = int(input()) arr = input() summ = dict() cnt = 0 ans = 0 for i in range(n): a = int(arr[i]) a -= 1 cnt += a if cnt == 0: ans += 1 if cnt in summ: ans += summ[cnt] summ[cnt] += 1 else: summ.update({cnt: 1}) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR IF VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR DICT VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$). For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$. Calculate the number of good subarrays of the array $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$)Β β€” the length of the array $a$. The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the number of good subarrays of the array $a$. -----Example----- Input 3 3 120 5 11011 6 600005 Output 3 6 1 -----Note----- The first test case is considered in the statement. In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$. In the third test case there is only one good subarray: $a_{2 \dots 6}$.
for _ in range(int(input())): n = int(input()) s = input().strip() d = {} p = 0 d[0] = 1 res = 0 for j in range(n): p = p + int(s[j]) q = p - j - 1 if q in d: res = res + d[q] d[q] = d[q] + 1 else: d[q] = 1 print(res)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$). For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$. Calculate the number of good subarrays of the array $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$)Β β€” the length of the array $a$. The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the number of good subarrays of the array $a$. -----Example----- Input 3 3 120 5 11011 6 600005 Output 3 6 1 -----Note----- The first test case is considered in the statement. In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$. In the third test case there is only one good subarray: $a_{2 \dots 6}$.
from sys import stdin t = int(stdin.readline()) for _ in range(t): length = int(stdin.readline()) a = list(map(int, list(stdin.readline().rstrip()))) currentSum = 0 sumCounts = {(0): 1} for n in a: currentSum += n - 1 if currentSum not in sumCounts: sumCounts[currentSum] = 1 else: sumCounts[currentSum] += 1 good = 0 for v in sumCounts.values(): good += v * (v - 1) // 2 print(good)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER FOR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$). For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$. Calculate the number of good subarrays of the array $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$)Β β€” the length of the array $a$. The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the number of good subarrays of the array $a$. -----Example----- Input 3 3 120 5 11011 6 600005 Output 3 6 1 -----Note----- The first test case is considered in the statement. In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$. In the third test case there is only one good subarray: $a_{2 \dots 6}$.
for _ in range(int(input())): n = int(input()) a = list(map(int, input())) a = [0] + a d = {(0): 1} ans = 0 cursum = 0 for z in range(1, n + 1): cursum += a[z] if cursum - z in d: ans += d[cursum - z] d[cursum - z] += 1 else: d[cursum - z] = 1 print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR IF BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$). For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$. Calculate the number of good subarrays of the array $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$)Β β€” the length of the array $a$. The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the number of good subarrays of the array $a$. -----Example----- Input 3 3 120 5 11011 6 600005 Output 3 6 1 -----Note----- The first test case is considered in the statement. In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$. In the third test case there is only one good subarray: $a_{2 \dots 6}$.
for _ in range(int(input())): n = int(input()) s = input() p = [int(s[0])] for i in range(1, len(s)): p.append(p[i - 1] + int(s[i])) c = {} for i in range(len(s)): p[i] -= i if p[i] in c: c[p[i]] += 1 else: c[p[i]] = 1 f = 0 for d in c: if c[d] != 1: f += c[d] * (c[d] - 1) // 2 print(f + (0 if 1 not in c else c[1]))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$). For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$. Calculate the number of good subarrays of the array $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$)Β β€” the length of the array $a$. The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the number of good subarrays of the array $a$. -----Example----- Input 3 3 120 5 11011 6 600005 Output 3 6 1 -----Note----- The first test case is considered in the statement. In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$. In the third test case there is only one good subarray: $a_{2 \dots 6}$.
t = int(input()) for q in range(t): n = int(input()) s = list(input()) cur, ans = 0, 0 dp = [1] + [0] * (9 * n + 10) for i in range(n): cur += int(s[i]) - 1 ans += dp[cur] dp[cur] += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP LIST NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$). For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$. Calculate the number of good subarrays of the array $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$)Β β€” the length of the array $a$. The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the number of good subarrays of the array $a$. -----Example----- Input 3 3 120 5 11011 6 600005 Output 3 6 1 -----Note----- The first test case is considered in the statement. In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$. In the third test case there is only one good subarray: $a_{2 \dots 6}$.
def solve(n): s = input() ans = [0] * (n + 1) ans[0] = int(s[0]) for i in range(1, n): ans[i] = int(s[i]) + ans[i - 1] sums = {} for i in range(n): sums[ans[i] - i] = sums.get(ans[i] - i, 0) + 1 answer = 0 for i in sums: answer += sums[i] * (sums[i] - 1) // 2 for i in range(n): if ans[i] == i + 1: answer += 1 print(answer) for _ in range(int(input())): n = int(input()) solve(n)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$). For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$. Calculate the number of good subarrays of the array $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$)Β β€” the length of the array $a$. The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the number of good subarrays of the array $a$. -----Example----- Input 3 3 120 5 11011 6 600005 Output 3 6 1 -----Note----- The first test case is considered in the statement. In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$. In the third test case there is only one good subarray: $a_{2 \dots 6}$.
from sys import stdin, stdout for _ in range(int(stdin.readline())): n = int(stdin.readline()) a = [(int(ch) - 1) for ch in input()] s = ans = 0 d = {(0): 1} for v in a: s += v ans += d.get(s, 0) d[s] = d.get(s, 0) + 1 print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER FOR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$). For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$. Calculate the number of good subarrays of the array $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$)Β β€” the length of the array $a$. The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the number of good subarrays of the array $a$. -----Example----- Input 3 3 120 5 11011 6 600005 Output 3 6 1 -----Note----- The first test case is considered in the statement. In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$. In the third test case there is only one good subarray: $a_{2 \dots 6}$.
for i in range(int(input())): n = int(input()) s = input() l = [int(i) for i in s] cum = [l[0]] for i in range(1, n): cum.append(cum[-1] + l[i]) d = {(1): 1} for i in range(n): if d.get(cum[i] - i): d[cum[i] - i] += 1 else: d[cum[i] - i] = 1 ct = 0 for i in d: ct += d[i] * (d[i] - 1) // 2 print(ct)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR DICT NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$). For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$. Calculate the number of good subarrays of the array $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$)Β β€” the length of the array $a$. The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the number of good subarrays of the array $a$. -----Example----- Input 3 3 120 5 11011 6 600005 Output 3 6 1 -----Note----- The first test case is considered in the statement. In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$. In the third test case there is only one good subarray: $a_{2 \dots 6}$.
t = int(input()) for i in range(t): n = int(input()) s = list(map(int, list(input()))) A = [0] * (n * 9 + 10) ind = 0 ans = 0 for i in range(n): A[ind] += 1 ind += int(s[i]) - 1 ans += A[ind] print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$). For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$. Calculate the number of good subarrays of the array $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$)Β β€” the length of the array $a$. The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the number of good subarrays of the array $a$. -----Example----- Input 3 3 120 5 11011 6 600005 Output 3 6 1 -----Note----- The first test case is considered in the statement. In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$. In the third test case there is only one good subarray: $a_{2 \dots 6}$.
test = int(input()) for each_test in range(test): d = {(0): 1} n = int(input()) s = input() curr_sum = 0 res = 0 for i in range(n): num = int(s[i]) curr_sum += num x = curr_sum - i - 1 if x not in d: d[x] = 0 d[x] += 1 res += d[x] - 1 print(res)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$). For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$. Calculate the number of good subarrays of the array $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$)Β β€” the length of the array $a$. The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the number of good subarrays of the array $a$. -----Example----- Input 3 3 120 5 11011 6 600005 Output 3 6 1 -----Note----- The first test case is considered in the statement. In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$. In the third test case there is only one good subarray: $a_{2 \dots 6}$.
for _ in range(int(input())): n = int(input()) a = list(map(int, list(input()))) pre = [0] su = 0 for i in a: su += i pre.append(su) ans1 = [] ans2 = [] for i in range(n + 1): ans1.append(pre[i] - i) d = dict() for i in ans1: if i not in d.keys(): d[i] = 1 else: d[i] += 1 fin_ans = 0 for i in d.values(): fin_ans += i * (i - 1) // 2 print(fin_ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$). For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$. Calculate the number of good subarrays of the array $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$)Β β€” the length of the array $a$. The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the number of good subarrays of the array $a$. -----Example----- Input 3 3 120 5 11011 6 600005 Output 3 6 1 -----Note----- The first test case is considered in the statement. In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$. In the third test case there is only one good subarray: $a_{2 \dots 6}$.
import sys buf = sys.stdin.read() nowbuf = 0 endbuf = len(buf) def getint(): global nowbuf valnow = 0 while buf[nowbuf] < "0" or buf[nowbuf] > "9": nowbuf += 1 while nowbuf < endbuf and buf[nowbuf] >= "0" and buf[nowbuf] <= "9": valnow = valnow * 10 + int(buf[nowbuf]) nowbuf += 1 return valnow def getstr(): global nowbuf strnow = "" while buf[nowbuf] == " " or buf[nowbuf] == "\n": nowbuf += 1 while nowbuf < endbuf and buf[nowbuf] != " " and buf[nowbuf] != "\n": strnow += buf[nowbuf] nowbuf += 1 return strnow def mycmp1(cur): return cur[0] def mycmp2(cur): return cur[0] def solve(array): n = len(array) ans = 0 num = 0 now = 0 o = [] vis = [(0) for i in range(n + n)] pos = [(0) for i in range(n + n)] for i in range(0, n): num += int(array[i]) o.append((i + 1 - num, i)) if num == i + 1: ans += 1 o.sort(key=mycmp1) for i in range(0, n): if i != 0 and o[i][0] != o[i - 1][0]: now += 1 pos[o[i][1]] = now for i in range(0, n): ans += vis[pos[i]] vis[pos[i]] += 1 print(ans) return t = getint() for i in range(0, t): getint() solve(getstr())
IMPORT ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR STRING VAR VAR STRING VAR NUMBER WHILE VAR VAR VAR VAR STRING VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR STRING WHILE VAR VAR STRING VAR VAR STRING VAR NUMBER WHILE VAR VAR VAR VAR STRING VAR VAR STRING VAR VAR VAR VAR NUMBER RETURN VAR FUNC_DEF RETURN VAR NUMBER FUNC_DEF RETURN VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR VAR IF VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$). For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$. Calculate the number of good subarrays of the array $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$)Β β€” the length of the array $a$. The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the number of good subarrays of the array $a$. -----Example----- Input 3 3 120 5 11011 6 600005 Output 3 6 1 -----Note----- The first test case is considered in the statement. In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$. In the third test case there is only one good subarray: $a_{2 \dots 6}$.
from sys import stdin input = lambda: stdin.readline().strip() l = len for _ in range(int(input())): n = int(input()) s = list(map(int, input())) dp = [0] * (9 * n) dp[0] = 1 a = 0 ans = 0 for i in range(n): a += s[i] ans += dp[a - i - 1] dp[a - i - 1] += 1 print(ans)
ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$). For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$. Calculate the number of good subarrays of the array $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$)Β β€” the length of the array $a$. The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the number of good subarrays of the array $a$. -----Example----- Input 3 3 120 5 11011 6 600005 Output 3 6 1 -----Note----- The first test case is considered in the statement. In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$. In the third test case there is only one good subarray: $a_{2 \dots 6}$.
def checkArray(): lengthArray = int(input()) array = str(input()) val = 0 _dict = {(0): 1} res = 0 for x in range(lengthArray): val += int(array[x]) x = val - x - 1 if x not in _dict: _dict[x] = 0 _dict[x] += 1 res += _dict[x] - 1 print(res) case = int(input()) while case > 0: checkArray() case -= 1
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$). For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$. Calculate the number of good subarrays of the array $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$)Β β€” the length of the array $a$. The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the number of good subarrays of the array $a$. -----Example----- Input 3 3 120 5 11011 6 600005 Output 3 6 1 -----Note----- The first test case is considered in the statement. In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$. In the third test case there is only one good subarray: $a_{2 \dots 6}$.
for _ in range(int(input())): n = int(input()) s = input() d = {(0): 1} ans = ext = 0 for i in range(n): ext += int(s[i]) - 1 if ext not in d: d[ext] = 0 ans += d[ext] d[ext] += 1 print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$). For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$. Calculate the number of good subarrays of the array $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$)Β β€” the length of the array $a$. The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the number of good subarrays of the array $a$. -----Example----- Input 3 3 120 5 11011 6 600005 Output 3 6 1 -----Note----- The first test case is considered in the statement. In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$. In the third test case there is only one good subarray: $a_{2 \dots 6}$.
from itertools import accumulate t = int(input()) for _ in range(t): n = int(input()) A = list(str(input())) A = [int(a) for a in A] from itertools import accumulate C = [0] + A C = list(accumulate(C)) d = {} ans = 0 for i in range(n + 1): if C[i] - i in d: ans += d[C[i] - i] d[C[i] - i] += 1 else: d[C[i] - i] = 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$). For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$. Calculate the number of good subarrays of the array $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$)Β β€” the length of the array $a$. The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the number of good subarrays of the array $a$. -----Example----- Input 3 3 120 5 11011 6 600005 Output 3 6 1 -----Note----- The first test case is considered in the statement. In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$. In the third test case there is only one good subarray: $a_{2 \dots 6}$.
def solve(): n = int(input()) a = list(map(int, input())) s = [0] d = {} for i in range(n): s.append(s[-1] + a[i]) for i in range(n + 1): val = s[i] - i if val not in d: d[val] = 0 d[val] += 1 ans = 0 for value in d.values(): ans += value * (value - 1) // 2 print(ans) for _ in range(int(input())): solve()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$). For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$. Calculate the number of good subarrays of the array $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$)Β β€” the length of the array $a$. The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the number of good subarrays of the array $a$. -----Example----- Input 3 3 120 5 11011 6 600005 Output 3 6 1 -----Note----- The first test case is considered in the statement. In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$. In the third test case there is only one good subarray: $a_{2 \dots 6}$.
import sys input = sys.stdin.readline def inp(): return int(input()) def inlt(): return list(map(int, input().split())) def insr(): s = input() return list(s[: len(s) - 1]) def invr(): return map(int, input().split()) def solve(a, n): d = {(0): 1} res, s = 0, 0 for i in range(n): s += int(a[i]) x = s - i - 1 if x not in d: d[x] = 0 d[x] += 1 res += d[x] - 1 return res t = inp() while t > 0: n = inp() s = input() print(solve(s, n)) t -= 1
IMPORT ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR RETURN FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$). For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$. Calculate the number of good subarrays of the array $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$)Β β€” the length of the array $a$. The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the number of good subarrays of the array $a$. -----Example----- Input 3 3 120 5 11011 6 600005 Output 3 6 1 -----Note----- The first test case is considered in the statement. In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$. In the third test case there is only one good subarray: $a_{2 \dots 6}$.
t = int(input()) for c in range(t): n = int(input()) seq = input() p = [0] for i in range(len(seq)): if len(p) == 0: p.append(int(seq[i])) else: p.append(int(seq[i]) + p[-1]) data = {} for i in range(n + 1): if p[i] - i not in data: data[p[i] - i] = 1 else: data[p[i] - i] += 1 count = 0 for key in data: count += data[key] * (data[key] - 1) / 2 print(int(count))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$). For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$. Calculate the number of good subarrays of the array $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$)Β β€” the length of the array $a$. The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the number of good subarrays of the array $a$. -----Example----- Input 3 3 120 5 11011 6 600005 Output 3 6 1 -----Note----- The first test case is considered in the statement. In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$. In the third test case there is only one good subarray: $a_{2 \dots 6}$.
def conv(c): return ord(c) - ord("0") def solve(): n = int(input().strip()) a = list(map(conv, input().strip(""))) umap = {(0): 1} s = 0 ans = 0 for i in range(n): index = i + 1 s += a[i] diff = s - index try: ans += umap[diff] umap[diff] += 1 except: umap[diff] = 1 print(ans) t = int(input().strip()) for _ in range(t): solve()
FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$). For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$. Calculate the number of good subarrays of the array $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$)Β β€” the length of the array $a$. The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the number of good subarrays of the array $a$. -----Example----- Input 3 3 120 5 11011 6 600005 Output 3 6 1 -----Note----- The first test case is considered in the statement. In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$. In the third test case there is only one good subarray: $a_{2 \dots 6}$.
import sys sys.setrecursionlimit(10**5) int1 = lambda x: int(x) - 1 p2D = lambda x: print(*x, sep="\n") def II(): return int(sys.stdin.readline()) def MI(): return map(int, sys.stdin.readline().split()) def LI(): return list(map(int, sys.stdin.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def SI(): return sys.stdin.readline()[:-1] for _ in range(II()): n = II() cs = 0 cnt = [0] * n * 10 cnt[0] = 1 ans = 0 for i, a in enumerate(SI()): cs += int(a) cur = cs - (i + 1) ans += cnt[cur] cnt[cur] += 1 print(ans)
IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR STRING FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP LIST NUMBER VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$). For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$. Calculate the number of good subarrays of the array $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$)Β β€” the length of the array $a$. The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the number of good subarrays of the array $a$. -----Example----- Input 3 3 120 5 11011 6 600005 Output 3 6 1 -----Note----- The first test case is considered in the statement. In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$. In the third test case there is only one good subarray: $a_{2 \dots 6}$.
import sys input = sys.stdin.readline inp, ip = lambda: int(input()), lambda: [int(w) for w in input().split()] for _ in range(inp()): n = inp() x = [int(i) for i in input().strip()] dp = [0] * (n + 1) for i in range(1, n + 1): dp[i] = dp[i - 1] + x[i - 1] for i in range(1, n + 1): dp[i] -= i dt = {} for i in dp: dt[i] = dt.get(i, 0) + 1 ans = 0 for u, v in dt.items(): ans += v * (v - 1) // 2 print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$). For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$. Calculate the number of good subarrays of the array $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$)Β β€” the length of the array $a$. The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the number of good subarrays of the array $a$. -----Example----- Input 3 3 120 5 11011 6 600005 Output 3 6 1 -----Note----- The first test case is considered in the statement. In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$. In the third test case there is only one good subarray: $a_{2 \dots 6}$.
a = input() for i in range(int(a)): x = input() A = list(input()) C = {(0): 1} t = 0 for j in range(len(A)): t = int(A[j]) - 1 + t if t in C: C[t] += 1 else: C[t] = 1 k = 0 for j in C: k += C[j] * (C[j] - 1) / 2 print(int(k))
ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$). For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$. Calculate the number of good subarrays of the array $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$)Β β€” the length of the array $a$. The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the number of good subarrays of the array $a$. -----Example----- Input 3 3 120 5 11011 6 600005 Output 3 6 1 -----Note----- The first test case is considered in the statement. In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$. In the third test case there is only one good subarray: $a_{2 \dots 6}$.
def solve(n, a): d = {} answ = 0 prefix = [0] * (n + 1) prefix[1] = a[0] for i in range(2, n + 1): prefix[i] = a[i - 1] + prefix[i - 1] for i in range(1, n + 1): left_part = i - prefix[i - 1] right_part = i - prefix[i] + 1 if d.get(left_part, -1) == -1: d[left_part] = 1 else: d[left_part] += 1 answ += d.get(right_part, 0) print(answ) t = int(input()) while t: n = int(input()) a = [int(i) for i in input()] solve(n, a) t -= 1
FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$). For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$. Calculate the number of good subarrays of the array $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$)Β β€” the length of the array $a$. The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the number of good subarrays of the array $a$. -----Example----- Input 3 3 120 5 11011 6 600005 Output 3 6 1 -----Note----- The first test case is considered in the statement. In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$. In the third test case there is only one good subarray: $a_{2 \dots 6}$.
for _ in range(int(input())): n = int(input()) s = input() d = {(0): 1} summ = 0 ans = 0 for i in s: summ += int(i) - 1 if d.get(summ, 0) > 0: ans += d.get(summ, 0) d[summ] += 1 else: d[summ] = 1 print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$). For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$. Calculate the number of good subarrays of the array $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$)Β β€” the length of the array $a$. The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the number of good subarrays of the array $a$. -----Example----- Input 3 3 120 5 11011 6 600005 Output 3 6 1 -----Note----- The first test case is considered in the statement. In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$. In the third test case there is only one good subarray: $a_{2 \dots 6}$.
n = int(input()) for i in range(n): m = int(input()) s = input() a = [] for c in s: a.append(int(c)) cum = [] cum.append(0) for j in range(1, len(a) + 1): cum.append(cum[j - 1] + int(a[j - 1])) for j in range(len(cum)): cum[j] = cum[j] - j b = [] cum.sort() cum.append(-1) tmp = 1 for j in range(len(cum) - 1): if cum[j] != cum[j + 1]: b.append(tmp) tmp = 1 else: tmp += 1 summ = 0 for j in range(len(b)): summ += b[j] * (b[j] - 1) / 2 print(int(summ))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$). For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$. Calculate the number of good subarrays of the array $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$)Β β€” the length of the array $a$. The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the number of good subarrays of the array $a$. -----Example----- Input 3 3 120 5 11011 6 600005 Output 3 6 1 -----Note----- The first test case is considered in the statement. In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$. In the third test case there is only one good subarray: $a_{2 \dots 6}$.
def scanInt(): return int(input()) def scanList(): return [i for i in input()] def scanIntList(): return [int(i) for i in scanList()] t = int(input()) for _ in range(t): n = scanInt() a = scanIntList() d = {(1): 0} sum = 0 ans = 0 for i in range(len(a)): sum += a[i] if sum - i not in d.keys(): d[sum - i] = 0 d[sum - i] += 1 for i in d.keys(): x = d[i] ans += x * (x - 1) // 2 print(ans + d[1])
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN VAR VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR IF BIN_OP VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$). For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$. Calculate the number of good subarrays of the array $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$)Β β€” the length of the array $a$. The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the number of good subarrays of the array $a$. -----Example----- Input 3 3 120 5 11011 6 600005 Output 3 6 1 -----Note----- The first test case is considered in the statement. In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$. In the third test case there is only one good subarray: $a_{2 \dots 6}$.
def good_count(arr): prefix = [0] for num in arr: prefix.append(prefix[-1] + num) prefixMinusIdx = [(prefix[i] - i) for i in range(len(prefix))] counter = {} for val in prefixMinusIdx: if counter.get(val): counter[val] += 1 else: counter[val] = 1 ans = 0 for count in counter.values(): ans += count * (count - 1) // 2 return ans t = int(input()) for _ in range(t): n = int(input()) arr = [*map(lambda ch: ord(ch) - ord("0"), input())] print(good_count(arr))
FUNC_DEF ASSIGN VAR LIST NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR VAR IF FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$). For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$. Calculate the number of good subarrays of the array $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$)Β β€” the length of the array $a$. The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the number of good subarrays of the array $a$. -----Example----- Input 3 3 120 5 11011 6 600005 Output 3 6 1 -----Note----- The first test case is considered in the statement. In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$. In the third test case there is only one good subarray: $a_{2 \dots 6}$.
mod = 10**9 + 7 def solve(): n = int(input()) t = list(input()) a = list(map(lambda x: int(x), t)) s = [(0) for i in range(n + 1)] t = 0 for i in range(n + 1): s[i] = n - i if i > 0: s[i] += a[i - 1] + t t += a[i - 1] d = {} ans = 0 for i in range(n + 1): if s[i] in d: ans += d[s[i]] d[s[i]] += 1 else: d[s[i]] = 1 print(ans) t = 1 t = int(input()) for _ in range(t): solve()
ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR IF VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$). For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$. Calculate the number of good subarrays of the array $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$)Β β€” the length of the array $a$. The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the number of good subarrays of the array $a$. -----Example----- Input 3 3 120 5 11011 6 600005 Output 3 6 1 -----Note----- The first test case is considered in the statement. In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$. In the third test case there is only one good subarray: $a_{2 \dots 6}$.
from itertools import accumulate for a in range(int(input())): n = int(input()) num = [0] + list(accumulate([int(a) for a in input()])) dic = {} ans = 0 for b in range(n + 1): val = num[b] - b if val not in dic: dic[val] = 0 dic[val] += 1 for key in dic: ans += dic[key] * (dic[key] - 1) // 2 print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER FOR VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$). For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$. Calculate the number of good subarrays of the array $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$)Β β€” the length of the array $a$. The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the number of good subarrays of the array $a$. -----Example----- Input 3 3 120 5 11011 6 600005 Output 3 6 1 -----Note----- The first test case is considered in the statement. In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$. In the third test case there is only one good subarray: $a_{2 \dots 6}$.
def countsumzero(lst, pdt, zr): prefixsums = [zr] for x in lst: prefixsums.append(prefixsums[-1 * pdt] + x + zr) freq = {} freq2 = {} for y in prefixsums: if y in freq: freq[y + zr] = (pdt + freq[y]) * pdt * pdt freq2[y + zr] = (pdt + freq[y]) * pdt * pdt else: freq[y + zr] = pdt * pdt + zr return sum((zr + v) * (v - pdt + zr) // (2 + zr) for v in freq.values()) for _ in range(int(input())): n = int(input()) lst = (int(i) + -1 for i in list(input())) print(countsumzero(lst, 1, 0))
FUNC_DEF ASSIGN VAR LIST VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR VAR RETURN FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER