Hitting a target distance
Statement
A person wrote down, in date order, how far they rode a bicycle on each of N days. The distance ridden on each day from day 1 to day N is written as a natural number. They did not rest for a single day, so no day has a distance of 0.
Looking over the log, the person wanted to know how many stretches of days in a row have distances adding up to exactly M. A stretch here means a run of dates that is not broken, such as day i through day j. A stretch of a single day counts as a stretch. Two stretches count as different if their start date or their end date differs.
Write a program that takes the riding log and the target distance M, and computes the number of stretches of days in a row whose distances add up to exactly M.
For example, suppose the riding log for N=10 days is given by the table below, and M=5.
| Day | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
|---|---|---|---|---|---|---|---|---|---|---|
| Distance | 1 | 2 | 3 | 4 | 2 | 5 | 3 | 1 | 1 | 2 |
Finding every stretch whose distances add up to exactly 5 gives the table below.
| Stretch | 1 | 2 | 3 |
|---|---|---|---|
| Dates | Day 2 through day 3 | Day 6 alone | Day 7 through day 9 |
| Distances added | 2 and 3 | 5 | 3 and 1 and 1 |
| Total distance | 5 | 5 | 5 |
Growing the stretch that starts on day 1 one day at a time gives 1, then 1+2=3, then 1+2+3=6, which runs past 5. Every distance is at least 1, so growing the stretch further only makes the total larger. Once the total goes past M there is no reason to look at that start date any longer. It is enough to push the start date forward by one day and look again. Moving the start date from day 1 to day N one step at a time, and each time moving the end date later only as far as needed, covers every stretch exactly once. So the answer for this case is 3.
Input and Output Examples
Example 1
Input
10 5 1 2 3 4 2 5 3 1 1 2
Output
3
Example 2
Input
3 7 5 3 4
Output
1
Example 3
Input
5 2 7 7 1 7 4
Output
0
Input
The first line holds the number of recorded days N and the target distance M, separated by a space. N is a natural number from 1 to 10,000. M is a natural number from 1 to 300,000,000.
The second line holds the distances ridden from day 1 to day N, separated by spaces, all on one line. Each day's distance is a natural number from 1 to 30,000.
The whole input is at most about 56KB. One line can hold up to 10,000 numbers, so read the input line by line and split each line on whitespace.
Output
Print the number of stretches whose distances add up to exactly M on the first line. If there is no such stretch, print 0.
Requirements
- Count only stretches whose dates run without a break
- A stretch made of a single day counts as a stretch
- Two stretches count as different if their start date or their end date differs
- Every day's distance is at least 1
- If M is larger than the total distance over all N days, the answer is 0
- You must write a program that reads from standard input and writes to standard output
- 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 15 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 15 cases to solve this.
Examples
3 public cases. Judging also uses hidden cases. You have to pass all 15 cases to solve this.
3 7 5 3 4
1
5 2 7 7 1 7 4
0
10 5 1 2 3 4 2 5 3 1 1 2
3
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
Nobody yet. You could be the first.