#P16177. [ICPC 2014 NAIPC] Fantastic Problem

[ICPC 2014 NAIPC] Fantastic Problem

题目描述

Andrew, the world-renowned problem writer, has finally decided to retire. However, he first wishes to put out one final masterpiece to seal his legacy and amaze competitors the world over. As you're his favorite pupil, he's asked you to help him proofread it before he submits it to the ICFP (International Committee for Fantastic Problems).

The problem proves to be quite the involved number theory affair, but you get together a solution eventually...only to find that it doesn't pass his data. After wasting hours debugging your code, you realize that it's correct – the data is invalid! Andrew's advanced age seems to have really caught up with him.

In his problem, you're given a sequence of nn integers, V1..nV_{1..n}, with the important guarantee that within every interval of kk consecutive integers (Vi..Vi+k1V_i..V_{i+k-1}, for some starting index ii), any two integers will be coprime (two integers are coprime if they share no common factors besides 1). Note However, this condition is not met in Andrew's data, causing your program to crash!

In an attempt to help your beloved mentor out, you count how many size-kk intervals don't satisfy his problem statement's promise. Unfortunately, your work is not done yet. Having difficulty fixing his data, Andrew makes mm sequential updates, where each update involves selecting some position aa in the number sequence, and changing its value to some value bb. After each change, he wants to know how many invalid size-kk intervals his new sequence contains. As if that wasn't enough, after the mthm^\text{th} update, Andrew decides that the data is good enough, and wants you to solve his problem with the resulting sequence of integers! His problem statement is as follows:

Given a sequence of integers, compute their sum.

输入格式

There will be several test cases in the input. Each test case will begin with a line with three integers, nn (1n100,0001 \leq n \leq 100,000), kk (1kn1 \leq k \leq n) and mm (1m100,0001 \leq m \leq 100,000), where nn is the size of Andrew's list, kk is the size of the consecutive intervals of interest, and mm is the number of changes Andrew makes. In each of the next nn lines will be a single integer vv (1v100,0001 \leq v \leq 100,000), which is a value in Andrew's input. The vv's will be in the order that they appear in Andrew's list. On each of the following mm lines will be a pair of integers aa (1an1 \leq a \leq n) and bb (1b100,0001 \leq b \leq 100,000), indicating that Andrew has changed value VaV_a to be bb. The input will end with a line with three 0s.

输出格式

For each test case, output m+2m+2 integers. The first integer should be the number of size-kk intervals in Andrew's original list which fail the pairwise-coprime constraint. Each of the next mm integers should be the number of size-kk intervals that fail the constraint after each of Andrew's mm changes, in order. The final integer should be the sum of the numbers in the final list. Output each integer on its own line, with no spaces. Do not print any blank lines between outputs.

6 3 4
7
2
3
4
5
6
4 3
5 9
4 10
6 11
0 0 0
2
3
3
3
2
42