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())): a = int(input()) n = input() arr = [] for i in n: arr.append(int(i) - 1) ans = 0 dic = {} sum1 = 0 for i in range(a): sum1 += arr[i] if dic.get(sum1, -1) == -1: dic[sum1] = 1 else: ans += dic[sum1] dic[sum1] += 1 if dic.get(0, -1) != -1: ans += dic[0] 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 FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR NUMBER NUMBER 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}$.
def countsumzero(lst): prefixsums = [0] for x in lst: prefixsums.append(prefixsums[-1] + x) freq = {} for y in prefixsums: if y in freq: freq[y] += 1 else: freq[y] = 1 return sum(v * (v - 1) // 2 for v in freq.values()) for _ in range(int(input())): n = int(input()) a = input() lst = [(int(i) - 1) for i in a] print(countsumzero(lst))
FUNC_DEF ASSIGN VAR LIST NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER RETURN FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER 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 FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER 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()) for i in range(t): n = int(input()) l = input() i_l = list(l.strip()) i_li = [int(x) for x in i_l] cur_sum = 0 exist = dict() exist[0] = 0 count = [(0) for j in range(n)] for j in range(n): cur_sum += i_li[j] - 1 if cur_sum in exist: last_j = exist[cur_sum] exist[cur_sum] = j count[j] = count[last_j] + 1 else: exist[cur_sum] = j print(sum(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 FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR 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}$.
for _ in range(int(input())): n = int(input()) s = input() summ = 0 mp = {} ans = 0 mp[0] = 1 for i in range(1, n + 1): summ += int(s[i - 1]) if summ - i in mp: ans += mp[summ - i] mp[summ - i] += 1 else: mp[summ - 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 ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR 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}$.
for _ in range(int(input())): n = int(input()) s = input() l = len(s) prefix_arr = [0] * l prefix_arr[0] = int(s[0]) - 1 for i in range(1, l): prefix_arr[i] = prefix_arr[i - 1] + int(s[i]) - 1 ans = 0 count = 0 d = dict() d[0] = 1 for i in range(l): item = prefix_arr[i] if item in d: ans = ans + d[item] d[item] += 1 else: d[item] = 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 FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP 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}$.
def solve(n, ar): d = {(0): 1} res, s = 0, 0 for i in range(n): s += int(ar[i]) x = s - i - 1 if x not in d: d[x] = 0 d[x] += 1 res += d[x] - 1 print(res) t = int(input()) for _ in range(t): n = int(input()) s = input() solve(n, s)
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 EXPR FUNC_CALL 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 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}$.
for _ in range(int(input())): n = int(input()) s = input() a = [(int(i) - 1) for i in s] pref = [0] for i in a: pref.append(pref[-1] + i) out = 0 h = {} for i in pref: if i not in h: h[i] = 0 h[i] += 1 for i in h: if h[i] > 1: out += h[i] * (h[i] - 1) // 2 print(out)
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 FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR LIST NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR DICT FOR 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}$.
from sys import stdin for case in range(int(stdin.readline())): n = int(stdin.readline()) s = [(int(x) - 1) for x in stdin.readline().strip()] sums = [0] for x in s: sums.append(sums[-1] + x) nums = {} for x in sums: if x in nums: nums[x] += 1 else: nums[x] = 1 total = 0 for x in nums: total += nums[x] * (nums[x] - 1) // 2 print(total)
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 FUNC_CALL VAR ASSIGN VAR LIST NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR DICT FOR 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}$.
t = int(input()) for _ in range(t): n = int(input()) li = list(map(int, input())) prefix = [0] * (n + 1) for i in range(1, n + 1): prefix[i] = li[i - 1] + prefix[i - 1] dic = {} for i in range(n + 1): val = dic.get(prefix[i] - i, -1) if val == -1: dic[prefix[i] - i] = 1 else: dic[prefix[i] - i] = val + 1 ans = 0 for item in dic.values(): temp = item * (item - 1) temp //= 2 ans += temp 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 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 ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER 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}$.
import sys input = sys.stdin.readline for _ in range(int(input())): n = int(input()) s = input() a = [(int(s[i]) - 1) for i in range(n)] data = [0] + a for i in range(1, n + 1): data[i] += data[i - 1] dic = {} for i in range(n + 1): if data[i] not in dic: dic[data[i]] = 0 dic[data[i]] += 1 ans = 0 for d in dic: ans += dic[d] * (dic[d] - 1) // 2 print(ans)
IMPORT 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 ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER 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}$.
for _ in range(int(input())): n = int(input()) s = input() a = [(int(i) - 1) for i in s] c = 0 p = [0] d = {} d[0] = 1 for i in a: c = c + i if c in d: d[c] += 1 else: d[c] = 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 BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER NUMBER FOR VAR VAR ASSIGN VAR BIN_OP 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}$.
T = int(input()) for case in range(T): i = int(input()) ls = [int(x) for x in input()] nums = [(e - 1) for n, e in enumerate(ls)] d = {(0): 1} tot = 0 ret = 0 for n in nums: tot += n if tot in d: ret += d[tot] try: d[tot] += 1 except: d[tot] = 1 print(ret)
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 ASSIGN VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR 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 _ in range(int(input())): n = int(input()) array = [] for c in input(): array.append(int(c)) d = {(0): 1} res = sum = 0 for i, num in enumerate(array): sum += num x = sum - (i + 1) if x not in d: d[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 LIST FOR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP 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}$.
t = int(input()) for _ in range(0, t): n = int(input()) v = input() prefix = [(0) for i in range(0, n + 1)] for i in range(0, n): prefix[i + 1] = int(v[i]) - int("0") + prefix[i] d = {(1): 1} ans = 0 for i in range(0, n): if prefix[i + 1] - i in d.keys(): ans += d[prefix[i + 1] - i] if prefix[i + 1] - i in d.keys(): d[prefix[i + 1] - i] += 1 else: d[prefix[i + 1] - i] = 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 NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING VAR VAR ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR 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}$.
n = int(input()) for _ in range(n): m = int(input()) s = input() ans = 0 d = {(0): 1} p = 0 for c in s: p += ord(c) - ord("0") - 1 cnt = d.setdefault(p, 0) ans += cnt d[p] = 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 NUMBER ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN 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}$.
MAX = 100000 + 1000 t = int(input("")) num = [] for i in range(t): int(input("")) num.append(input("")) for i in num: sol = 0 n = len(i) i = list(i) cnt = {} cnt[0] = 1 sol = 0 s = 0 for j in range(n): s += int(i[j]) ind = j + 1 - s try: cnt[ind] except: cnt[ind] = 0 sol += cnt[ind] cnt[ind] += 1 print(sol)
ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR STRING FOR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR 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 ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR 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 process(arr): arr = list(arr) map = {} count = 0 sum = 0 for i in range(len(arr)): sum += int(arr[i]) if sum - i - 1 == 0: count += 1 if sum - i - 1 in map: count += len(map[sum - i - 1]) map[sum - i - 1].append(i) else: map[sum - i - 1] = [i] print(count) for _ in range(int(input())): n = int(input()) s = input() process(s)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER LIST VAR 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 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 x in range(int(input())): n = int(input()) d = {} m = list(map(int, input())) l = 0 for i in range(n): l += m[i] - 1 if l in d: d[l] += 1 else: d[l] = 1 s = 0 for i in d: s += d[i] * (d[i] - 1) // 2 if i == 0: s += d[i] print(s)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER 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 IF 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}$.
for _ in range(int(input())): n = int(input()) a = list(map(int, input())) s = [0] for x in a: s += (x + s[-1],) b = [i for i in range(n + 1)] c = [(s[i] - b[i]) for i in range(n + 1)] d = {} ans = 0 for x in c[::-1]: d[x] = d.get(x, 0) ans += d[x] d[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 FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER FOR VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR NUMBER 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}$.
for _ in range(int(input())): n = int(input()) a = input() prefix_sum = [0] for i in a: prefix_sum.append(prefix_sum[-1] + int(i)) for i in range(n + 1): prefix_sum[i] = prefix_sum[i] - i count = {} for i in prefix_sum: count[i] = count.get(i, 0) + 1 sum = 0 for i in count: sum += count[i] * (count[i] - 1) // 2 print(sum)
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 FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP 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 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 _ in range(t): n = int(input()) l = list(input()) sumi = 0 dicti = {} dicti[0] = 1 count = 0 for i in range(n): sumi = sumi + int(l[i]) - 1 if dicti.get(sumi, 0) == 0: dicti[sumi] = 1 else: count += dicti[sumi] dicti[sumi] += 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 ASSIGN VAR DICT ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER NUMBER 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}$.
for item in [0] * int(input()): n = int(input()) a = list(map(int, input())) for i in range(n): a[i] = a[i] - 1 hashmap = {} hashmap[0] = 1 count = 0 s = 0 for i in range(n): if a[i] == 0 and i == 0: count += 1 hashmap[0] = 2 else: s += a[i] if s in hashmap: count += hashmap[s] hashmap[s] += 1 if s not in hashmap: hashmap[s] = 1 print(count)
FOR VAR BIN_OP LIST NUMBER 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 FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER NUMBER VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR 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}$.
res1 = [0, 1, 3] for i in range(3, 10**5 + 1): res1.append(res1[-1] + i) def main(case): n = int(input()) nums = input() res = [(0) for _ in range(2 * 10 * n + 1)] cur = 0 for i in nums: a = int(i) cur += a - 1 res[cur + n] += 1 sm = 0 for i in res: if i > 0: sm += res1[i - 1] print(sm + res[n]) t = int(input()) for i in range(t): main(i + 1)
ASSIGN VAR LIST NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP 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}$.
from sys import stdin, stdout t = int(stdin.readline().strip()) for _ in range(t): n = int(stdin.readline().strip()) arr = list(map(int, list(stdin.readline().strip()))) for i in range(len(arr)): arr[i] -= 1 tarr = [0] d = {(0): 1} for i in range(len(arr)): tarr.append(tarr[-1] + arr[i]) if tarr[-1] in d: d[tarr[-1]] += 1 else: d[tarr[-1]] = 1 ans = 0 for i in d: ans += d[i] * (d[i] - 1) // 2 stdout.write(str(ans) + "\n")
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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR DICT NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER NUMBER ASSIGN VAR 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 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}$.
t = int(input()) for _ in range(t): maps = {} l = int(input()) s = input() p = [0] for i, v in enumerate(s): p.append(p[i] + int(v)) for i in range(l + 1): maps[p[i] - i] = maps.get(p[i] - i, 0) + 1 cnt = 0 for v in maps.values(): cnt += v * (v - 1) // 2 print(cnt)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR 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
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}$.
testcases = int(input()) for testcase in range(testcases): n = int(input()) arr = [] strs = input() for i in strs: arr.append(int(i) - 1) dicts = {} prefix = 0 ans = 0 dicts[0] = 1 for i in arr: prefix += i if prefix in dicts: ans += dicts[prefix] if prefix not in dicts: dicts[prefix] = 1 else: dicts[prefix] += 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 LIST ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR IF VAR VAR ASSIGN 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}$.
def ans(arr, n): hashMap = {} curr_sum = 0 ans = 0 for i in range(n): curr_sum += arr[i] if curr_sum == 0: ans += 1 if curr_sum in hashMap: ans += len(hashMap[curr_sum]) hashMap[curr_sum].append(i) else: hashMap[curr_sum] = [i] return ans t = int(input()) for _ in range(t): n = int(input()) arr = [(int(char) - 1) for char in input()] print(ans(arr, 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 VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR LIST 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 BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL 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}$.
t = int(input()) for _ in range(t): n = int(input()) a = input() l = [] for i in a: l.append(int(i)) lsum = [l[0]] for i in range(1, n): lsum.append(lsum[i - 1] + l[i]) dic = dict() dic[0] = 0 for i in range(len(lsum)): lsum[i] = lsum[i] - i - 1 count = 0 lsum.insert(0, 0) for i in range(len(lsum)): if lsum[i] not in dic: dic[lsum[i]] = 0 count += dic[lsum[i]] dic[lsum[i]] += 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 ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER 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}$.
for _ in range(int(input())): N = int(input()) s = list(map(int, list(input()))) presum = [s[0]] for i in range(1, len(s)): presum.append(s[i] + presum[-1]) for i in range(len(presum)): presum[i] -= i + 1 presum.insert(0, 0) dic = {} count = 0 for i in range(len(presum)): if presum[i] not in dic: dic[presum[i]] = 1 else: count += dic[presum[i]] dic[presum[i]] += 1 print(count)
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 FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER 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 choose(n): if n < 2: return 0 return n * (n - 1) // 2 t = int(input()) for _ in range(t): n = int(input()) arr = list(input()) cs = [0] * (n + 1) cs[0] = 0 for i in range(1, n + 1): cs[i] = -1 + cs[i - 1] + int(arr[i - 1]) cs.sort() seen = [] seen2 = [] ans = 0 cur = cs[0] val = 0 for i in range(1, len(cs)): if cur == cs[i]: val += 1 else: ans += choose(val + 1) val = 0 cur = cs[i] ans += choose(val + 1) print(ans)
FUNC_DEF IF VAR NUMBER RETURN NUMBER RETURN 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 FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL 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}$.
testcase = int(input()) for index in range(testcase): size = int(input()) temp = input() arr = [] ans = 0 for i in range(size): arr.append(int(temp[i])) diff = 0 diff_arr = [0] * (size * 10 + 20) for i in range(size): diff_arr[diff] += 1 diff += arr[i] - 1 ans += diff_arr[diff] 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 FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR BIN_OP 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}$.
for sd in range(int(input())): n = int(input()) s = input() p = [int(s[i]) for i in range(n)] p.reverse() p.append(0) p.reverse() f = dict() for i in range(1, n + 1): p[i] += p[i - 1] for i in range(n + 1): p[i] -= i - 1 for i in range(n + 1): c = p[i] if c in f: f[c] += 1 else: f[c] = 1 m = 0 for elem in f: c = f[elem] m += c * (c - 1) print(m // 2)
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 FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP 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 solve(s): pre = [0] * (len(s) + 1) for i in range(len(s)): pre[i + 1] = pre[i] + s[i] counters = dict() balance = 0 ans = 0 counters[0] = 1 for i in range(len(s)): balance -= 1 balance += s[i] ans += counters.get(balance, 0) counters.setdefault(balance, 0) counters[balance] += 1 return ans for _ in range(int(input())): _ = int(input()) s = input() s = [int(d) for d in s] print(solve(s))
FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER RETURN 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 ASSIGN VAR FUNC_CALL VAR VAR 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()) for j in range(t): n = int(input()) s = input() c = 0 L = [0] C = [(0) for i in range(9 * n + 1)] A = [int(s[0])] for i in range(1, len(s)): A.append(A[i - 1] + int(s[i])) for i in range(len(s)): L.append(A[i] - (i + 1)) for i in range(len(L)): k = L[i] c = c + C[k] C[k] = C[k] + 1 print(c)
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 LIST NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER 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 FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR 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}$.
def subarray_sum(s, k, n): curr = 0 m = {} ans = 0 for i in range(n): curr += s[i] if curr == k: ans += 1 if curr - k in m: ans += m[curr - k] m[curr] = 1 + m.get(curr, 0) return ans for _ in range(int(input())): n = int(input()) s = list(input()) for i in range(n): s[i] = int(s[i]) - 1 ans = subarray_sum(s, 0, n) print(ans)
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER RETURN 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 FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER 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()) a = [(int(s) - 1) for s in input()] pref = [0] * (n + 1) for i in range(1, n + 1): pref[i] = pref[i - 1] + a[i - 1] pref = sorted(pref) now = pref[0] tim = 0 ans = 0 for i in pref: if i == now: tim += 1 else: ans += tim * (tim - 1) // 2 tim = 1 now = i ans += tim * (tim - 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 FUNC_CALL VAR VAR NUMBER VAR 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 ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR 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 i in range(int(input())): n = int(input()) s = [int(i) for i in input()] sum = [(0) for i in range(n)] count = 0 sum[0] = s[0] for i in range(1, n): sum[i] = sum[i - 1] + s[i] for i in range(n): sum[i] = sum[i] - i sum.append(1) sum = sorted(sum) x = 0 for i in range(n + 1): x = x + 1 if i == n or sum[i] != sum[i + 1]: count += x * (x - 1) / 2 x = 0 print(int(count))
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 VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR 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 i in range(t): n = int(input()) ls = [0] * (n + 1) s = input() for j in range(1, n + 1): ls[j] = ls[j - 1] + int(s[j - 1]) count = [0] * (n + 1) for j in range(len(ls)): count[j] = ls[j] - j dict = {} for j in range(len(count)): key = count[j] if key not in dict: dict[key] = 1 else: dict[key] += 1 sum = 0 for x in dict.values(): sum += (x - 1) * x / 2 print(int(sum))
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 LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN 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 BIN_OP VAR NUMBER VAR 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}$.
import sys input = sys.stdin.readline for kek in range(int(input())): n = int(input()) a = list(map(int, input()[:-1])) x = 0 s = dict() for i in range(n + 1): if x - i in s: s[x - i] += 1 else: s[x - i] = 1 if i >= n: break x += a[i] ans = 0 for i in s: ans += s[i] * (s[i] - 1) // 2 print(ans)
IMPORT 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 NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR VAR 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 _ in range(int(input())): n = int(input()) l = list(map(int, list(input()))) ans, itr = 0, 0 sums = dict() for i in range(n): back_l = itr - i try: sums[back_l] += 1 except: sums[back_l] = 1 back_l = itr + l[i] - i - 1 if back_l in sums: ans += sums[back_l] itr += l[i] 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 VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER IF VAR VAR VAR 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}$.
def CF(my_list, freq): for item in my_list: freq[item] = freq[item] + 1 if item in freq else 1 return freq for _ in range(int(input())): n = int(input()) a = list(str(input())) for i in range(n): a[i] = int(a[i]) s = 0 suma = [0] * n for i in range(n): s += a[i] suma[i] = s k = 0 for i in range(n): if suma[i] == i + 1: k += 1 for i in range(n): suma[i] -= i d = CF(suma, {}) for i in d: y = d[i] e = y * (y - 1) // 2 k += e print(k)
FUNC_DEF FOR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR NUMBER NUMBER RETURN 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 FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR DICT FOR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER 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()) while t > 0: n = int(input()) s = input() l = [int(i) for i in s] l1 = [] l1.append(l[0]) for i in range(1, len(l)): l1.append(l1[-1] + l[i]) l1 = [0] + l1 d = {} ans = 0 for i in range(len(l1)): if l1[i] - i not in d: d[l1[i] - i] = 1 else: d[l1[i] - i] += 1 for i in d: ans = ans + d[i] * (d[i] - 1) // 2 print(ans) t -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR 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 VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL 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}$.
def solve(): occr = dict() n = int(input()) a = list(map(lambda x: int(x) - 1, input())) ctr = 0 occr[0] = 1 last = 0 for i in a: i += last key = i if key in occr: ctr += occr[key] if i not in occr: occr[i] = 0 occr[i] += 1 last = i print(ctr) for _ in range(int(input())): solve()
FUNC_DEF ASSIGN 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 ASSIGN VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR 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 _collections import defaultdict def fun(arr, n): for i in range(n): arr[i] -= 1 pref = [0] * n pref[0] = arr[0] for i in range(1, n): pref[i] = pref[i - 1] + arr[i] ans = 0 a = defaultdict(lambda: 0) a[0] += 1 for i in range(n): ans += a[pref[i]] a[pref[i]] += 1 return ans for _ in range(int(input())): n = int(input()) arr = [] for i in input(): arr.append(int(i)) print(fun(arr, n))
FUNC_DEF FOR VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER 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 NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL 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())): n = int(input()) arr = [(int(k) - 1) for k in input()] for i in range(1, len(arr)): arr[i] += arr[i - 1] d = {(0): 1} ans = 0 for i in arr: if i in d: d[i] += 1 else: d[i] = 1 ans += d[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 BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN 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 s in [*open(0)][2::2]: a = [1] + len(s) * 8 * [0] t = 0 for x in s[:-1]: t += int(x) - 1 a[t] += 1 print(sum(x * (x - 1) // 2 for x in a))
FOR VAR LIST FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER LIST NUMBER ASSIGN VAR NUMBER FOR VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER 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(z, lst, vl): prefixsums = [z] for x in lst: prefixsums.append(prefixsums[z - vl] + x - z) freq = {} for y in prefixsums: if y not in freq: freq[z + y] = vl + z else: freq[z + y] = freq[y] + vl + z return sum((v * v - v + z) // (vl + vl - 4 * z) for v in freq.values()) def main(): for _ in range(int(input())): n = int(input()) lst = (int(i) - 1 for i in list(input())) print(countsumzero(0, lst, 1)) main()
FUNC_DEF ASSIGN VAR LIST VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR RETURN FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR FUNC_DEF 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 NUMBER VAR NUMBER 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 i in range(t): n = int(input()) s = input() sumarray = [] sum = 0 for j in range(n): sum += int(s[j]) - 1 sumarray.append(sum) count = 0 d = {(0): 1} for j in range(n): if sumarray[j] not in d: d[sumarray[j]] = 0 d[sumarray[j]] += 1 count += d[sumarray[j]] - 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 ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER 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 I = lambda: int(input()) RL = readline = lambda: sys.stdin.readline().strip("\n") RM = readmap = lambda x=int: map(x, readline().split(" ")) for _ in range(I()): n, s = I(), list(map(int, readline())) l, d, count = [0] * n, {(1): 1}, 0 for i in range(n): l[i] = l[i - 1] + s[i] hash0 = l[i] - i d[hash0] = d.get(hash0, 0) + 1 for k, v in d.items(): count += v * (v - 1) >> 1 print(count) quit() for _ in range(I()): s = RL() l = [0] for i in s + "0": if i == "1": l[-1] += 1 else: l.append(0) l.sort(reverse=True) print(sum(l[::2])) quit() for _ in range(I()): n, l = I(), [*RM()] if l[0] + l[1] <= l[-1]: print(1, 2, n) else: print(-1)
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR BIN_OP LIST NUMBER VAR DICT NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER FOR VAR BIN_OP VAR STRING IF VAR STRING VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR LIST FUNC_CALL VAR IF BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER VAR EXPR FUNC_CALL 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()) a = list(int(x) for x in input()) a.insert(0, 0) ans = 0 d = {} d[0] = 1 for i in range(1, n + 1): a[i] += a[i - 1] - 1 if a[i] in d: ans += d[a[i]] d[a[i]] += 1 else: d[a[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 FUNC_CALL VAR VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN 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}$.
[ ( lambda N, n, s, d: ( lambda ss, ds: ( [(ss(0, s[0] + i - 1), ds(s[0], d.get(s[0], 0) + 1)) for i in n], print(sum(d[k] * (d[k] - 1) >> 1 for k in d)), ) )(s.__setitem__, d.__setitem__) )(int(input()), map(int, input()), [0], {(0): 1}) for t in range(int(input())) ]
EXPR FUNC_CALL FUNC_CALL FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER NUMBER VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR LIST NUMBER DICT NUMBER NUMBER VAR FUNC_CALL VAR 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}$.
t = int(input()) while t: t -= 1 n = int(input()) s = input() l = [int(i) for i in s] pre = 0 d = dict() d[0] = 1 for i in range(n): pre += l[i] temp = pre - (i + 1) if temp not in d: d[temp] = 1 else: d[temp] += 1 ans = 0 for i in d: ans += d[i] * (d[i] - 1) // 2 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER 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 x in range(t): n = input() s = input() l = [int(i) for i in s] dp = [(0) for i in range(len(l))] dp[0] = l[0] for i in range(1, len(l)): dp[i] = dp[i - 1] + l[i] dp2 = [(0) for i in range(len(l))] for i in range(len(l)): dp2[i] = dp[i] - i d = {} for i in range(len(l)): if dp2[i] not in d: d[dp2[i]] = 1 else: d[dp2[i]] = d[dp2[i]] + 1 sum1 = 0 for i in d: sum1 = sum1 + d[i] * (d[i] - 1) // 2 for i in range(len(dp)): if dp[i] == i + 1: sum1 = sum1 + 1 print(sum1)
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 FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR NUMBER ASSIGN 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}$.
t = int(input()) for o in range(t): n = int(input()) s = input() l = [] for i in range(n): l.append(int(s[i])) overflow = {} overflow[0] = 1 curSum = 0 ans = 0 for i in range(n): curSum += l[i] if curSum - i - 1 not in overflow: overflow[curSum - i - 1] = 0 overflow[curSum - i - 1] += 1 for key in overflow.keys(): ans += overflow[key] * (overflow[key] - 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 LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL 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() ps = [] summ = 0 for i in s: summ += int(i) - 1 ps.append(summ) ps = [0] + ps map1 = {} for i in ps: if map1.get(i) == None: map1[i] = 1 else: map1[i] += 1 cnt = 0 for i in map1.keys(): if map1[i] >= 2: cnt += map1[i] / 2 * (map1[i] - 1) print(int(cnt))
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 ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR DICT FOR VAR VAR IF FUNC_CALL VAR VAR NONE ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR IF VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR 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(): pref = [0] count = 0 m = {} for i in range(1, n + 1): pref.append(pref[-1] + nums[i - 1]) temp = pref[-1] - i if temp == 0: count += 1 if temp in m: count += m[temp] m[temp] += 1 else: m[temp] = 1 return count t = int(input()) for i in range(t): n = int(input()) nums = list(map(int, list(str(input())))) print(solve())
FUNC_DEF ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN 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 VAR FUNC_CALL VAR FUNC_CALL VAR 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}$.
for _ in range(int(input())): N = int(input()) array = [int(x) for x in list(input())] partialsum = [0] total = 0 for i in array: total += i partialsum.append(total) goodarrays = [] for i in range(len(partialsum)): goodarrays.append(partialsum[i] - i) dictionary = dict() counter = 0 for i in goodarrays: if i in dictionary: counter += dictionary[i] dictionary[i] += 1 else: dictionary[i] = 1 print(counter)
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 FUNC_CALL VAR ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR 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}$.
Q = int(input()) for q in range(Q): N = int(input()) l = list(input()) R = [int(l[0]) - 1] for i in range(1, N): R.append(R[-1] + int(l[i]) - 1) D = {} D[0] = 1 ans = 0 for i in range(N): if R[i] not in D: D[R[i]] = 1 else: ans += D[R[i]] D[R[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 ASSIGN VAR LIST BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER 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}$.
import sys input = sys.stdin.readline t = int(input()) for _ in range(t): y = int(input()) x = input().rstrip() arr = [0] total = 0 for char in x: total += int(char) - 1 arr.append(total) seen = {} segm = [] J = {} for i in range(len(arr)): if arr[i] not in seen: seen[arr[i]] = i else: x = seen[arr[i]] segm.append((x, i)) J[x] = i seen[arr[i]] = i bonus = [] V = set([]) for seg in segm: c = 1 x = seg[1] if x not in V: V.add(x) while x in J: c += 1 x = J[x] V.add(x) bonus.append(c * (c + 1) // 2) print(sum(bonus))
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 FUNC_CALL VAR ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR LIST ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR LIST FOR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR WHILE VAR VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL 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}$.
import sys def answer(n, a): p = [(0) for _ in range(n + 1)] for i in range(1, n + 1): p[i] = p[i - 1] + a[i - 1] pimi = [(0) for _ in range(n + 1)] for i in range(n + 1): pimi[i] = p[i] - i d = dict() res = 0 for i in range(n + 1): if pimi[i] not in d: d[pimi[i]] = 0 d[pimi[i]] += 1 res += d[pimi[i]] - 1 return res def main(): t = int(sys.stdin.readline()) while t: n = int(sys.stdin.readline()) a = [int(c) for c in sys.stdin.readline().rstrip()] print(answer(n, a)) t -= 1 return main()
IMPORT FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR 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 ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER RETURN VAR FUNC_DEF 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 FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN 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}$.
tc = int(input()) while tc: n = int(input()) s = input() pre = [] pre.append(0) my_dic = {} ans = 0 sm = 0 for i in range(1, n + 1): if pre[i - 1] - i + 1 in my_dic.keys(): my_dic[pre[i - 1] - i + 1] = my_dic[pre[i - 1] - i + 1] + 1 else: my_dic[pre[i - 1] - i + 1] = 1 v = int(s[i - 1]) sm += v pre.append(sm) tmp = sm - i if tmp in my_dic.keys(): ans += my_dic[tmp] print(ans) tc -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP 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 FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL 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()) while t: n = int(input()) a = list(input()) for i in range(0, n): a[i] = int(a[i]) temp = [] for i in a: temp.append(i - 1) count = 0 curr = 0 sums = {} for i in range(0, n): curr = curr + temp[i] if curr == 0: count = count + 1 if curr in sums: count = count + sums[curr] sums[curr] = sums[curr] + 1 else: sums[curr] = 1 print(count) t = t - 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP 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 ASSIGN VAR BIN_OP 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}$.
from sys import stdin input = stdin.readline for _ in range(int(input())): n = int(input()) s = input()[:-1] p = [0] for i in s: p.append(p[-1] + int(i)) d = {(0): 1} ans = 0 for i in range(n): ans += d.get(p[i + 1] - i - 1, 0) d[p[i + 1] - i - 1] = d.get(p[i + 1] - i - 1, 0) + 1 print(ans)
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 NUMBER ASSIGN VAR LIST NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL 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 BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER 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}$.
for _ in range(int(input())): n = int(input()) s = input() pref = [0] su = 0 for x in s: su += int(x) pref.append(su) d = {} ans = 0 for i in range(n + 1): if not d.get(pref[i] - i): d[pref[i] - i] = 1 else: ans += d[pref[i] - i] d[pref[i] - 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 ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER VAR VAR BIN_OP VAR VAR VAR 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}$.
for t in range(int(input())): n = int(input()) s = input() l = [] d = {} ans1 = 0 k = 0 c1 = 0 k1 = [] for i in range(len(s)): l.append(int(s[i])) l1 = [l[0]] for i in range(1, len(l)): l1.append(l1[-1] + l[i]) l1 = [0] + l1 c1 += 1 k1.append(c1) for i in range(len(l1)): if l1[i] - i not in d: d[l1[i] - i] = 1 else: k += 1 d[l1[i] - i] += 1 k += 2 for i in d: ans1 = ans1 + d[i] * (d[i] - 1) // 2 print(ans1 + 3 - 7 + 4) k += 1 t -= 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 ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR 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 VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER 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 _ in range(T): n = int(input()) s = input() s = [int(s[i]) for i in range(n)] s.insert(0, 0) su = [] su.append(0) for i in range(1, n + 1): su.append(su[i - 1] + s[i]) mmp = {(0): 1} ans = 0 for i in range(1, n + 1): if i - su[i] not in mmp: mmp[i - su[i]] = 0 ans += mmp[i - su[i]] mmp[i - su[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 FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER VAR VAR BIN_OP VAR VAR VAR 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}$.
from itertools import accumulate t = int(input()) for _ in range(t): n = int(input()) sumi = 0 a = list(map(int, input())) arr = list(accumulate(a)) c = dict() for i in range(n): c[arr[i] - i] = c.get(arr[i] - i, 0) + 1 res = 0 for x in c: cnt = c[x] res += cnt * (cnt - 1) // 2 for i in range(n): if arr[i] == i + 1: res += 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 NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR 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 FOR VAR FUNC_CALL VAR VAR IF VAR VAR 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}$.
t = int(input()) while t > 0: t -= 1 n = int(input()) s = input() ans = 0 pref = [(0) for x in range(n * 10 + 1)] curSUm = 0 pref[0] = 1 for i in range(n): curSUm += ord(s[i]) - ord("0") ans += pref[curSUm - (i + 1)] pref[curSUm - (i + 1)] += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR 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()) temp = input() lst = [] for i in temp: lst.append(int(i)) main = [lst[0]] abtk = lst[0] for i in range(1, n): abtk += lst[i] main.append(abtk) d = {(0): 1} for i in range(n): d[main[i] - i - 1] = d.get(main[i] - i - 1, 0) + 1 ans = 0 for key in d.keys(): x = d[key] 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 ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR DICT NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN 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 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, list(input()))) p = {(0): 1} ans = s = 0 for x in range(n): s += a[x] y = s - x - 1 if y not in p: p[y] = 0 p[y] += 1 ans += p[y] - 1 print(ans) t = int(input()) for x in range(t): solve()
FUNC_DEF 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 NUMBER NUMBER ASSIGN VAR 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 VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR 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}$.
def solution(): for t in range(int(input())): n = int(input()) l = [int(e) for e in input()] ans = 0 d = {(0): 1} idx = 0 for e in l: idx += e - 1 if idx in d: d[idx] += 1 else: d[idx] = 1 ans += d[idx] - 1 print(ans) return solution()
FUNC_DEF 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 DICT NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN 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()) cnt = 0 while cnt < t: cnt += 1 n = int(input()) a = [int(i) for i in input()] ans = 0 s = 0 res = 0 d = dict() d[0] = 1 for i in range(n): s += a[i] if d.get(s - i - 1, 0) == 0: d[s - i - 1] = 1 else: res += d[s - i - 1] d[s - i - 1] += 1 print(res)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER 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 solve(b, m): p_dict = {(0): 1} x = 0 for i in range(m): x += b[i] - 1 if x in p_dict: p_dict[x] += 1 else: p_dict[x] = 1 r = sum([(v * (v - 1) // 2) for k, v in p_dict.items()]) return r t = int(input()) for case in range(t): n = int(input()) a = [int(x) for x in list(input())] print(solve(a, n))
FUNC_DEF 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 NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR FUNC_CALL 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 VAR VAR FUNC_CALL VAR FUNC_CALL 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}$.
t = int(input()) while t != 0: t = t - 1 n = int(input()) s = input() arr = [0] * n for i in range(0, n): arr[i] = int(s[i]) - 1 sum = 0 count = 0 map = {} k = 0 for i in range(0, n): sum = sum + arr[i] if sum == 0: count = count + 1 if map.__contains__(sum) == True: count = count + map[sum] if map.__contains__(sum) == True: k = map[sum] k = k + 1 map[sum] = k else: map[sum] = 1 print(count)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR 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()) while t > 0: t -= 1 n, a = int(input()), input() ans, summ, m = 0, 0, {} for c in a: summ += int(c) - 1 if summ == 0: ans += 1 ans += m.get(summ, 0) m[summ] = m.get(summ, 0) + 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER NUMBER DICT FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR NUMBER 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 iii in range(int(input())): n = int(input()) l = list(map(int, input())) s = 0 d = {(0): 1} c = 0 for i in range(n): s = s + l[i] c = s - 1 - i if c in d: d[c] = d[c] + 1 else: d[c] = 1 c = 0 for i in d.values(): c = c + i * (i - 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 ASSIGN VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP 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()) def goodarrays(s, n): arr = [(int(s[i]) - 1) for i in range(n)] dp, H = {(-1): 0}, {(0): -1} Sum, Count = 0, 0 for i in range(n): Sum += arr[i] if Sum in H: dp[i] = 1 + dp[H[Sum]] else: dp[i] = 0 H[Sum], Count = i, Count + dp[i] return Count for i in range(t): N = int(input()) string = input() print(goodarrays(string, N))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR VAR DICT NUMBER NUMBER DICT NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP NUMBER VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL 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}$.
t = int(input()) for i in range(t): n = int(input()) s = input() data = list(map(int, s)) for i in range(len(data)): data[i] = data[i] - 1 fdata = {data[0]: 1} ndata = [data[0]] ans = 0 if data[0] == 0: ans += 1 for i in range(1, len(data)): ndata.append(ndata[i - 1] + data[i]) if ndata[i - 1] + data[i] == 0: if 0 in fdata: ans += fdata[0] + 1 fdata[0] += 1 else: ans += 1 fdata[0] = 1 continue if ndata[i - 1] + data[i] in fdata: ans += fdata[ndata[i - 1] + data[i]] fdata[ndata[i - 1] + data[i]] += 1 else: fdata[ndata[i - 1] + data[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 FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR DICT VAR NUMBER NUMBER ASSIGN VAR LIST VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER IF NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP 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}$.
from sys import stdin tt = int(stdin.readline()) for loop in range(tt): n = int(stdin.readline()) a = stdin.readline() a = a[:-1] dic = {} now = 0 dic[0] = 1 for i in range(1, n + 1): now = now + int(a[i - 1]) if now - i not in dic: dic[now - i] = 1 else: dic[now - i] += 1 ans = 0 for i in dic: ans += dic[i] * (dic[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 VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR BIN_OP 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 _ in range(t): n = int(input()) a = [int(i) for i in list(input().strip())] a.insert(0, 0) for i in range(1, n + 1): a[i] += a[i - 1] for i in range(n + 1): a[i] = a[i] - i freq = {} count = 0 for i in range(n + 1): x = a[i] y = freq.get(x, 0) count += y freq[x] = y + 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 VAR VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN 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}$.
t = int(input()) for i in range(t): n = input() s = input() dp = {(0): 1} prv = 0 for i in range(0, len(s)): y = prv + int(s[i]) x = i + 1 c = y - x if c in dp: dp[c] += 1 else: dp[c] = 1 prv = y ans = 0 for val in dp.values(): ans += int(val * (val - 1) / 2) print(ans)
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 FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL 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 = input() p = 0 ans = 0 good = [1] + [0] * 9 * n for i in a: p += int(i) - 1 ans += good[p] good[p] += 1 print("%d\n" % 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 BIN_OP LIST NUMBER BIN_OP BIN_OP LIST NUMBER NUMBER VAR FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP STRING 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 = input() s = 0 ans = 0 d = {(0): 0} for i in range(n): s += int(a[i]) - 1 if s in d: d[s] += 1 else: d[s] = 0 ans += d[s] print(ans) for t in range(int(input())): solve()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR VAR 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}$.
for _ in range(int(input())): n = int(input()) a = list(map(int, list(input()))) s = [] now_s = 0 for i in range(n): now_s += a[i] s.append(now_s - i - 1) ans = 0 data = {(0): 1} for i in s: try: data[i] += 1 except: data[i] = 1 ans += data[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 FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN 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 j in range(int(input())): a = int(input()) b = [int(j) for j in input()] su = 0 d = {} d[0] = 1 count = 0 for j in range(a): su = su + b[j] su1 = su - (j + 1) if su1 in d: count += d[su1] d[su1] += 1 else: d[su1] = 1 print(count)
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 DICT ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP 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 i in range(t): n = int(input()) a = [int(i) for i in list(input())] modified_pref = [n + 1] for i in range(len(a)): modified_pref.append(modified_pref[-1] + a[i] - 1) modified_pref.sort() n_eq_pairs = 0 prev = modified_pref[0] combo = 1 for i in range(1, len(modified_pref)): current = modified_pref[i] if prev == current: combo += 1 else: n_eq_pairs += combo * (combo - 1) / 2 combo = 1 prev = current n_eq_pairs += combo * (combo - 1) / 2 print(int(n_eq_pairs))
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 LIST BIN_OP VAR NUMBER FOR VAR FUNC_CALL 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 VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR 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}$.
import sys def rs(): return sys.stdin.readline().rstrip() def ri(): return int(sys.stdin.readline()) def ria(): return list(map(int, sys.stdin.readline().split())) def ws(s): sys.stdout.write(s) sys.stdout.write("\n") def wi(n): sys.stdout.write(str(n)) sys.stdout.write("\n") def wia(a, sep=" "): sys.stdout.write(sep.join([str(x) for x in a])) sys.stdout.write("\n") def solve(n, a): pre = [0] * (n + 1) cnt = {(0): 1} for i in range(n): pre[i + 1] = pre[i] + a[i] d = pre[i + 1] - i - 1 if d not in cnt: cnt[d] = 0 cnt[d] += 1 ans = 0 for k, v in cnt.items(): if v > 0: ans += v * (v - 1) // 2 return ans def main(): for _ in range(ri()): n = ri() a = [int(si) for si in rs()] wi(solve(n, a)) main()
IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL 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 EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING FUNC_DEF STRING EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR IF VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER RETURN VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR 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 sys import stdin input = stdin.readline for _ in range(int(input())): n = int(input()) a = "0" + input().rstrip() d = dict() b = [0] cnt = 0 for x in range(n): b.append(b[-1] + int(a[x + 1])) i = 0 for x in b: if x - i in d.keys(): d[x - i] += 1 else: d[x - i] = 1 i += 1 for x in d.values(): cnt += x * (x - 1) // 2 print(cnt)
ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP STRING FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER 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 T = int(sys.stdin.readline().strip()) for t in range(0, T): n = int(sys.stdin.readline().strip()) a = sys.stdin.readline().strip() b = [(int(a[i]) - 1) for i in range(0, n)] ans = 0 s = [0] * (10 * n + 1) s[0 + n] = 1 c = 0 for i in range(0, n): c = c + b[i] ans = ans + s[c + n] s[c + n] = s[c + n] + 1 print(ans)
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP 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 data in [*open(0)][2::2]: d = {} d[0] = 1 summ = 0 ans = 0 for a in data[0:-1]: c = int(a) - 1 summ += c ans += d.get(summ, 0) if d.get(summ): d[summ] += 1 else: d[summ] = 1 print(ans)
FOR VAR LIST FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL 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 solution(): n = int(input()) s = list(input()) l = [] for i in range(n): l.append(int(s[i])) d = {} d[0] = 1 pre = [0] * (n + 2) for i in range(1, n + 1): pre[i] += pre[i - 1] + l[i - 1] ans = 0 for i in range(1, n + 1): x = pre[i] - i ans += d.get(x, 0) d[x] = d.get(x, 0) + 1 print(ans) t = int(input()) for _ in range(t): solution()
FUNC_DEF 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 EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP 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 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}$.
def solve(n, a): p = [0] for num in a: p.append(p[-1] + num) ans = 0 ls = dict() ls[0] = 1 for r in range(n): if p[r + 1] - r - 1 in ls: ans += ls[p[r + 1] - r - 1] ls[p[r + 1] - r - 1] += 1 else: ls[p[r + 1] - r - 1] = 1 return ans def main(): t = int(input()) ns = [] arrs = [] for _ in range(t): n = int(input()) r = input().strip() a = [int(x) for x in r] ns.append(n) arrs.append(a) for i in range(t): print(solve(ns[i], arrs[i])) main()
FUNC_DEF ASSIGN VAR LIST NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER 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 RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR 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}$.
for _ in range(int(input())): n = int(input()) a = input() d = {(0): 1} res = 0 pre = 0 A = [] for i in range(n): A.append(int(a[i])) A[i] -= 1 pre += A[i] if pre not in d: d[pre] = 0 d[pre] += 1 for value in d.values(): res += (value - 1) * value // 2 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 ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR 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}$.
t = int(input()) while t: n = int(input()) s = list(input()) s = list(map(int, s)) s = list(map(lambda x: x - 1, s)) sumi = 0 seen = {(0): 1} ans = 0 for i in range(n): sumi += s[i] if sumi in seen: ans += seen[sumi] seen[sumi] += 1 else: seen[sumi] = 1 print(ans) t -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR 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 FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL 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}$.
res = "" for _ in range(int(input())): input() a = [int(x) for x in input()] r = 0 p = {(0): 1} w = 0 for i in a: w += i - 1 if w in p: r += p[w] else: p[w] = 0 p[w] += 1 res += f"{r}\n" print(res)
ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER VAR VAR STRING 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()) digits = list(input()) for i in range(n): digits[i] = int(digits[i]) - 1 prefix = {} prefix[0] = 1 so_far = 0 answer = 0 for x in digits: so_far += x if so_far in prefix: answer += prefix[so_far] prefix[so_far] += 1 else: prefix[so_far] = 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 FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR 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 i in range(t): n = int(input()) a = input() ls = {(0): 1} lis = [0] for i in a: lis += [lis[-1] + int(i) - 1] if lis[-1] in ls: ls[lis[-1]] += 1 else: ls[lis[-1]] = 1 nat = 0 for i in ls: i = ls[i] nat += i * (i - 1) / 2 print(int(nat))
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 LIST NUMBER FOR VAR VAR VAR LIST BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR NUMBER NUMBER ASSIGN 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 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 answer(A): n = len(A) A = list(A) for i in range(n): A[i] = int(A[i]) - 1 count = 0 d = {(0): 1} s = 0 for i in range(n): s += int(A[i]) if s in d: count += d[s] d[s] += 1 else: d[s] = 1 return count t = int(input()) for i in range(t): n = int(input()) s = input() print(answer(s))
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN 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 EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Alice and Bob have invented a new game. They have a rooted tree having N nodes (rooted at node 1), and the i^{th} node has the value A_{i} associated with it (1-indexed). The value associated with the root node is 0. They start with a marker at the root node, and a score of 0. Alice and Bob moves alternatively, with Alice moving first. In one move, they can move the pointer from a node to one of its child node, and add the value associated with that child node to the score. If the marker reaches a leaf node, the game ends. Alice wants to minimize the score of the game, whereas Bob wants to maximize the score. They were about to start the game when the Charlie arrives. He looked at the game, and declared the game to be boring. However, Charlie asked them - "What will be the score at the end of the game, if \textbf{each} one of you can skip \textbf{at most} K moves?" Can you help Alice and Bob to answer Charlie? ------ Input Format ------ - The first line of input contains a single integer T, denoting the number of testcases. The description of the T testcases follows. - The first line of each test case contains two space separated integers N and K. - The second line of each test case contains N space separated integers A_{1}, A_{2},..., A_{N}. It is guaranteed that A_{1} = 0. - The following N-1 lines contains two space separated integers x and y, which denotes that there is an edge between nodes x and y. ------ Output Format ------ For each testcase, output in a single line the answer to the Charlie's problem. ------ Constraints ------ $1 ≀ T ≀ 10^{4}$ $1 ≀ N ≀ 10^{5}$ $0 ≀ K ≀ 200$ $-10^{9} ≀ A_{i} ≀ 10^{9}$ $1 ≀ x, y ≀ N$ - The sum of $N$ over all test cases does not exceed $10^{5}$ ----- Sample Input 1 ------ 3 3 1 0 1 2 1 2 1 3 4 0 0 1 2 3 1 2 1 3 2 4 7 0 0 2 1 4 2 7 3 1 2 1 3 2 4 2 5 3 6 3 7 ----- Sample Output 1 ------ 1 2 6 ----- explanation 1 ------ Test Case $1$: Alice can move the marker to node $2$, and hence making score $= 1$. If Alice skips her chance, then Bob can move the marker to node $3$, and hence making score = $2$. Therefore, Alice will not skip her chance. Test Case $2$: If Alice moves the marker to node $2$, then Bob will move the marker to node $4$, making score = $4$. If Alice moves the marker to node $3$, the game will end, and the score will be $2$.
import sys T = int(input()) sys.setrecursionlimit(10000000) class Node: def __init__(self, weight): self.weight = int(weight) self.cost = None self.child = [] self.level = None self.choice = None def setLevels(node, level=0): node.level = level for n in node.child: setLevels(n, level + 1) def setCost(node, cost=0): node.cost = cost + node.weight for n in node.child: setCost(n, node.cost) def findChoice(node): if not node.child: node.choice = node.cost return node.choice options = [findChoice(n) for n in node.child] if node.level % 2: node.choice = max(options) else: node.choice = min(options) return node.choice for _ in range(T): n, k = list(map(int, input().split(" "))) weights = list(map(Node, input().split(" "))) for x in range(n - 1): a, b = sorted([(x - 1) for x in map(int, input().split(" "))]) weights[a].child.append(weights[b]) setLevels(weights[0]) setCost(weights[0]) findChoice(weights[0]) print(weights[0].choice)
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NONE ASSIGN VAR LIST ASSIGN VAR NONE ASSIGN VAR NONE FUNC_DEF NUMBER ASSIGN VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_DEF NUMBER ASSIGN VAR BIN_OP VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR ASSIGN VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER
Alice and Bob have invented a new game. They have a rooted tree having N nodes (rooted at node 1), and the i^{th} node has the value A_{i} associated with it (1-indexed). The value associated with the root node is 0. They start with a marker at the root node, and a score of 0. Alice and Bob moves alternatively, with Alice moving first. In one move, they can move the pointer from a node to one of its child node, and add the value associated with that child node to the score. If the marker reaches a leaf node, the game ends. Alice wants to minimize the score of the game, whereas Bob wants to maximize the score. They were about to start the game when the Charlie arrives. He looked at the game, and declared the game to be boring. However, Charlie asked them - "What will be the score at the end of the game, if \textbf{each} one of you can skip \textbf{at most} K moves?" Can you help Alice and Bob to answer Charlie? ------ Input Format ------ - The first line of input contains a single integer T, denoting the number of testcases. The description of the T testcases follows. - The first line of each test case contains two space separated integers N and K. - The second line of each test case contains N space separated integers A_{1}, A_{2},..., A_{N}. It is guaranteed that A_{1} = 0. - The following N-1 lines contains two space separated integers x and y, which denotes that there is an edge between nodes x and y. ------ Output Format ------ For each testcase, output in a single line the answer to the Charlie's problem. ------ Constraints ------ $1 ≀ T ≀ 10^{4}$ $1 ≀ N ≀ 10^{5}$ $0 ≀ K ≀ 200$ $-10^{9} ≀ A_{i} ≀ 10^{9}$ $1 ≀ x, y ≀ N$ - The sum of $N$ over all test cases does not exceed $10^{5}$ ----- Sample Input 1 ------ 3 3 1 0 1 2 1 2 1 3 4 0 0 1 2 3 1 2 1 3 2 4 7 0 0 2 1 4 2 7 3 1 2 1 3 2 4 2 5 3 6 3 7 ----- Sample Output 1 ------ 1 2 6 ----- explanation 1 ------ Test Case $1$: Alice can move the marker to node $2$, and hence making score $= 1$. If Alice skips her chance, then Bob can move the marker to node $3$, and hence making score = $2$. Therefore, Alice will not skip her chance. Test Case $2$: If Alice moves the marker to node $2$, then Bob will move the marker to node $4$, making score = $4$. If Alice moves the marker to node $3$, the game will end, and the score will be $2$.
from sys import setrecursionlimit, stdin input = stdin.readline setrecursionlimit(10**8) def dfs(p, prev, turn): m1, m2 = float("inf"), -float("inf") count = 0 for i in child[p]: if i == prev: continue x = dfs(i, p, turn ^ 1) m1 = min(m1, x + a[p - 1]) m2 = max(m2, x + a[p - 1]) count += 1 if count == 0: return a[p - 1] elif turn == 0: return m1 else: return m2 for T in range(int(input())): n, k = map(int, input().split()) a = list(map(int, input().split())) child = [[] for i in range(n + 1)] for i in range(n - 1): u, v = map(int, input().split()) child[u].append(v) child[v].append(u) ans = dfs(1, -1, 0) print(ans)
ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR STRING FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER RETURN VAR BIN_OP VAR NUMBER IF VAR NUMBER RETURN VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR