PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#PS0000143. Đếm đoạn có đúng K số lẻ (Count Number of Nice Subarrays)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Đếm đoạn có đúng K số lẻ (Count Number of Nice Subarrays)

Count Number of Nice Subarrays

Source: LeetCode

Version: Phuoc Hung OJ Extended

Problem Statement

Given positive integers a1,…,ana_1,\ldots,a_n and an integer KK, a nonempty contiguous subarray is nice if it contains exactly KK odd elements. Count all nice subarrays, distinguishing different endpoint pairs.

Input

  • Line 1: integer nn, the array length and integer KK, the required number of odd entries.
  • Line 2: nn integers a1,…,ana_1,\ldots,a_n in order.

Output

Print the number of qualifying intervals, or 0 if there are none.

Subtasks

  • Subtask 1 (20 points): 1≤n≤10001\le n\le 1000; 1≤i≤n1\le i\le n; 1≤ai≤1051\le a_i\le 10^5; 1≤K≤n1\le K\le n.
  • Subtask 2 (30 points): 1≤n≤500001\le n\le 50000; 1≤i≤n1\le i\le n; 1≤ai≤1051\le a_i\le 10^5; 1≤K≤n1\le K\le n; Every aia_i is odd.
  • Subtask 3 (50 points): 1≤n≤500001\le n\le 50000; 1≤i≤n1\le i\le n; 1≤ai≤1051\le a_i\le 10^5; 1≤K≤n1\le K\le n.

Example 1

Input

5 3
1 1 2 1 1

Output

2

Explanation

Intervals [1,4][1,4] and [2,5][2,5] contain exactly three odd values. The entire array has four odds; no other interval has three.

Example 2

Input

3 1
2 4 6

Output

0

Explanation

All values are even, so no interval contains one odd value.

Example 3

Input

10 2
2 2 2 1 2 2 1 2 2 2

Output

16

Explanation

The odd entries are at positions 4 and 7. There are four left endpoints (1 through 4) and four right endpoints (7 through 10), giving 4⋅4=164\cdot4=16.