Ad blocks
Statement
A broadcaster lined up N ad videos in the order they will air. Each ad, numbered 1 through N, has its length written on it as a natural number of seconds.
The scheduler wants to group a few ads that come in a row in this order into one ad block. By the scheduling rule of the broadcaster, though, a group works as an ad block only when the lengths of its ads add up to a multiple of M. A block made of one ad alone is allowed. Two blocks count as different if their first ad or their last ad differs.
Write a program that takes the ad lengths and the reference number M, and computes the number of runs that work as an ad block.
For example, suppose the lengths of N=5 ads are given by the table below, and M=3.
| Number | 1 | 2 | 3 | 4 | 5 |
|---|---|---|---|---|---|
| Length | 1 | 2 | 3 | 1 | 2 |
Finding every run whose lengths add up to a multiple of 3 gives the table below.
| Block | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
|---|---|---|---|---|---|---|---|
| Ads grouped | 1 to 2 | 1 to 3 | 1 to 5 | 2 to 4 | 3 alone | 3 to 5 | 4 to 5 |
| Total length | 3 | 6 | 9 | 6 | 3 | 6 | 3 |
N is up to 1,000,000, so adding up every run one by one is not an option. If the remainder of the total length from ad 1 to each ad divided by M is written down, then any run lying between two places with the same remainder has a total that is a multiple of M. The table below holds those remainders for the example above.
| Last ad | None | 1 | 2 | 3 | 4 | 5 |
|---|---|---|---|---|---|---|
| Total from ad 1 | 0 | 1 | 3 | 6 | 7 | 9 |
| Remainder after dividing by 3 | 0 | 1 | 0 | 0 | 1 | 0 |
Four places have remainder 0 and two places have remainder 1. Adding up the ways to pick two places with the same remainder gives 6 + 1 = 7. So the answer for this case is 7.
Input and Output Examples
Example 1
Input
5 3 1 2 3 1 2
Output
7
Example 2
Input
2 2 44494498 236225094
Output
3
Example 3
Input
5 2 865967068 74128013 807610317 131868032 375553997
Output
7
Input
The first line holds the number of ads N and the reference number M, separated by a space. N is a natural number from 1 to 1,000,000. M is a natural number from 1 to 1,000.
The second line holds the lengths of ads 1 through N, separated by spaces, all on one line. Each ad length is a natural number from 1 to 1,000,000,000.
The whole input is at most about 3.9MB. One line can hold up to 1,000,000 numbers, so read the input line by line and split each line on whitespace.
Output
Print the number of runs that work as an ad block on the first line. The answer can grow as large as 500,000,500,000. That size does not fit in a 32-bit integer, so take care over the type you use.
Requirements
- Count only runs that are not broken in the order the ads are lined up
- A run made of one ad alone counts as a run
- Two runs count as different if their first ad or their last ad differs
- If M is 1, every run is an ad block
- The answer can go past the range of a 32-bit integer
- N is up to 1,000,000, so working out the total of every run one by one runs out of time
- 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 512MB. 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.
2 2 44494498 236225094
3
5 2 865967068 74128013 807610317 131868032 375553997
7
5 3 1 2 3 1 2
7
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.
Log in and you can see the code once you solve this problem.