Burst fire
Statement
A marksman is practicing his shooting. Targets numbered 1 through N are lined up in a row, and each target has a score written on it as a natural number between 1 and 1,000.
The marksman boasts a 100% hit rate, so every time he shoots he hits exactly the target he wants. He intends to fire one burst. A burst means hitting the targets at consecutive positions one after another. Position N and position 1 are not consecutive.
The marksman also intends to make the scores of the consecutive targets he hits add up to exactly M. He has K bullets. He also wants to hit only targets whose value is even.
Suppose the marksman hits positions X through Y in order. Write a program that counts every (X, Y) pair that satisfies his goal.
For example, assume the number of targets is N=12. The table below shows the score given to the target at each position. Let M=10 and K=2.
| Position | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Score | 5 | 5 | 4 | 6 | 2 | 2 | 8 | 5 | 3 | 2 | 6 | 2 |
Here you can confirm that only two cases are possible, namely (3, 4) and (6, 7). In other words, you have to count every case that satisfies the following three conditions.
(1) The scores of the consecutive targets must add up to exactly M. (2) He has K bullets. (3) The score values of the consecutive targets he hits must all be even.
Input and Output Examples
Example 1
Input
12, 10, 2, [5, 5, 4, 6, 2, 2, 8, 5, 3, 2, 6, 2]
Output
2
Example 2
Input
15, 8, 3, [1, 2, 3, 4, 4, 5, 8, 8, 8, 2, 4, 4, 2, 1, 3]
Output
5
Example 3
Input
20, 10, 4, [2, 8, 3, 5, 2, 10, 1, 7, 4, 5, 4, 4, 2, 2, 4, 4, 4, 2, 10, 10]
Output
7
Input
First the number of targets N, the wanted sum of target scores M, and the number of bullets K are given. N is a natural number less than or equal to 100,000, M is a natural number less than or equal to 10,000, and K is a natural number less than or equal to 100.
Next comes scores, a 1D array holding the score of each target. The score of each target is a natural number between 1 and 1,000.
Output
Write a program that returns the number of all possible (X, Y) pairs that satisfy the marksman's goal.
Requirements
- You must write a solution(N, M, K, scores) function and return the answer
- Trailing spaces at the end of a line and blank lines at the end of the output are ignored when grading
- The languages you can use are Python and JavaScript
- The memory limit is 256MB. The browser does not measure it, it only states it
- The time limit is set by measuring the setter's reference solution on the same runtime
- On this problem you submit code, not a prompt. That code runs in the browser on this device, and all that goes to the server is a fingerprint of the output and the time it took. Whether it is correct is decided by the server. The run time is a number each device reports, so the code and the device details are posted next to it on the leaderboard.
- The output has to match the expected value down to the character. A space inside a line counts the same as any other character.
- There are 20 cases, and 3 of them show the input together with the expected output. The rest are used only when you submit and are not shown on this page.
- You have to pass all 20 cases to solve this.
Examples
3 public cases. Judging also uses hidden cases. You have to pass all 20 cases to solve this.
12, 10, 2, [5, 5, 4, 6, 2, 2, 8, 5, 3, 2, 6, 2]
2
15, 8, 3, [1, 2, 3, 4, 4, 5, 8, 8, 8, 2, 4, 4, 2, 1, 3]
5
20, 10, 4, [2, 8, 3, 5, 2, 10, 1, 7, 4, 5, 4, 4, 2, 2, 4, 4, 4, 2, 10, 10]
7
Notes
Background that helps with solving. The rules you have to follow are in the input and output specs and the requirements above.
This is problem 2 of practice test 3 in the original.
Solution
Your code runs on this device. Only the fingerprint of the output and the elapsed time go to the server. The server decides whether it is correct.
Submitting requires you to log in. Running against the public cases works without one.
You can test with 3 public cases. Submitting runs the hidden cases too.
People who solved this problem
The time shown is the time spent solving. Input reading time is outside the solver's control, so it is excluded. And since each device reports this value itself, we record which device measured it.
Input read time 105ms (excluded from the ranking)
Log in and you can see the code once you solve this problem.