This page is still under construction.

Parts of this page are still being built. What you see may change.

Fantastic Problem

Time limit10sMemory limit256 MB

Summary
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 nn integers V1,V2,…,VnV_1, V_2, \ldots, V_n, with the promise that inside every interval of kk consecutive integers Vi,Vi+1,…,Vi+k−1V_i, V_{i+1}, \ldots, V_{i+k-1}, any two integers are coprime. Two integers are coprime when 11 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 kk fail the promise. That is not the end of it. Andrew has trouble fixing the data, so he makes mm changes one after another, and each change picks a position aa in the sequence and sets its value to bb. After every change he wants to know how many invalid intervals of size kk the new sequence contains. Once the mm-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 nn (1≤n≤1000001 \le n \le 100000), kk (1≤k≤n1 \le k \le n) and mm (1≤m≤1000001 \le m \le 100000), where nn is the length of Andrew's list, kk is the size of the intervals of interest, and mm is the number of changes Andrew makes. Each of the next nn lines holds one integer vv (1≤v≤1000001 \le v \le 100000), a value in Andrew's list. The values appear in the order they appear in the list. Each of the following mm lines holds a pair of integers aa (1≤a≤n1 \le a \le n) and bb (1≤b≤1000001 \le b \le 100000), meaning that Andrew has changed VaV_a to bb. The input ends with a line holding three zeros.

Output

For each test case, print m+2m + 2 integers, each on its own line and with no spaces. The first integer is the number of size-kk intervals in Andrew's original list that fail the pairwise-coprime constraint. Each of the next mm integers is the number of size-kk 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.

Examples2

  1. Example 1

    Input
    6 3 4
    7
    2
    3
    4
    5
    6
    4 3
    5 9
    4 10
    6 11
    0 0 0
    
    Expected output
    2
    3
    3
    3
    2
    42
    
  2. Example 2

    Input
    5 5 2
    2
    3
    5
    7
    11
    3 4
    1 1
    0 0 0
    
    Expected output
    0
    1
    0
    26