Knitting
InterviewTime limit1sMemory limit128 MB
Given a starting stitch count, a row total, and a repeating pattern of row change values, compute the sum of all row stitch counts for each project until the 0 0 0 terminator.
- Level
Medium4 of 10
- Topics
- Math, Prefix sum, Simulation
- Solved
- No attempts yet
Problem
Marcia loves to knit, and she wants to know how many stitches a project will take to complete.
Every project starts with a first row containing a given number of stitches. Each later row is obtained from the previous row by adding a value taken from a fixed, repeating pattern. A positive value increases the number of stitches, a negative value decreases it, and leaves it unchanged. After the pattern's last value is used, it repeats from the beginning.
For example, a triangular shawl might start with stitches and add stitches on every following row. Then the rows have , , , stitches, and a project of rows uses stitches in total.
A scarf might use a -row repeating pattern that adds , then subtracts , subtracts , and finally changes nothing. Starting from stitches, the rows have , , , , , and (as the pattern repeats) stitches. A -row project therefore uses stitches.
Given the description of one or more projects, compute the total number of stitches each project takes to complete.
Input
The input describes one or more projects. Each project is given on two lines.
The first line contains three integers:
N M K
where () is the number of stitches in the first row, () is the total number of rows, and () is the number of rows in the repeating pattern.
The second line contains exactly integers, each between and inclusive, describing the pattern: a negative value decreases the stitch count, a positive value increases it, and leaves it unchanged. No project's pattern ever makes a row have or fewer stitches.
The end of input is a line containing three zeros (0 0 0), which is not part of any project.
Output
For each project, print the total number of stitches in the completed project on its own line, in the order the projects are given, with no blank lines between the answers.