Fantastic Problem
Time limit10sMemory limit256 MB
You count size-k windows where some pair shares a factor, refresh the count after each point update, then print the final sum.
- Level
Hard8 of 10
- Topics
- Segment tree, Number theory, Sliding window
- Solved
- No attempts yet
Problem
Andrew is a problem setter, and he has decided to retire. He wants to put out one last problem before he stops, so he asked you, his student, to proofread it before he sends it to the ICFP (International Committee for Fantastic Problems).
The problem turns out to be a heavy piece of number theory. You put a solution together, and it fails on his data. After hours of debugging you find that your code is right and the data is wrong. Andrew's age has caught up with him.
In his problem you are given a sequence of integers , with the promise that inside every interval of consecutive integers , any two integers are coprime. Two integers are coprime when is their only common factor. Andrew's data breaks the promise, and that is what makes your program crash.
To help your mentor, you count how many intervals of size fail the promise. That is not the end of it. Andrew has trouble fixing the data, so he makes changes one after another, and each change picks a position in the sequence and sets its value to . After every change he wants to know how many invalid intervals of size the new sequence contains. Once the -th change is done, Andrew decides the data is good enough and asks you to solve his problem on the resulting sequence. His problem statement is this: given a sequence of integers, compute their sum.
Input
The input holds several test cases. Each test case begins with a line of three integers (), () and (), where is the length of Andrew's list, is the size of the intervals of interest, and is the number of changes Andrew makes. Each of the next lines holds one integer (), a value in Andrew's list. The values appear in the order they appear in the list. Each of the following lines holds a pair of integers () and (), meaning that Andrew has changed to . The input ends with a line holding three zeros.
Output
For each test case, print integers, each on its own line and with no spaces. The first integer is the number of size- intervals in Andrew's original list that fail the pairwise-coprime constraint. Each of the next integers is the number of size- intervals that fail the constraint after the matching change, in order. The last integer is the sum of the numbers in the final list. Print no blank lines between outputs.