转发:http://blog.csdn.net/snowy_smile/article/details/52757953
GCD
Time Limit: 9000/4500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 78 Accepted Submission(s): 11
Problem Description
Mr. Frog likes generating numbers! He can generate many numbers from a sequence.
For a given sequence a1,a2,⋯,an Mr. Frog can choose two numbers l and r ( 1≤l≤r≤n ) and calculate the gcd between l-th and r-th number in this sequence g=gcd(al,al+1,⋯,ar) . Asan expert in generating numbers, Mr. Frog wants to know how many distinct numbers can be generated by a sequence.
Mr. Frog likes challenges, so there may be many modifications in this sequence. In the i-th modification, Mr. Frog may change ap to vi . After each modification, you are asked to tell how many distinct numbers can be generated by this sequence immediately!
For a given sequence a1,a2,⋯,an Mr. Frog can choose two numbers l and r ( 1≤l≤r≤n ) and calculate the gcd between l-th and r-th number in this sequence g=gcd(al,al+1,⋯,ar) . Asan expert in generating numbers, Mr. Frog wants to know how many distinct numbers can be generated by a sequence.
Mr. Frog likes challenges, so there may be many modifications in this sequence. In the i-th modification, Mr. Frog may change ap to vi . After each modification, you are asked to tell how many distinct numbers can be generated by this sequence immediately!
Input
The first line contains only one integer T, which indicates the number of test cases.
For each test case, the first line includes two numbers n, q( 1≤n,q≤50000 ). which indicate the length of sequence and the number of modifications.
The second line contains n numbers: a1,a2,⋯,an .
Then q lines, each line contain two numbers, pi,vi(1≤pi≤n,1≤vi≤1000000) .
Test data guarantee that 1<≤ai≤1000000 all the time and the sum of all n and q is less than or equal to 2×105 .
For each test case, the first line includes two numbers n, q( 1≤n,q≤50000 ). which indicate the length of sequence and the number of modifications.
The second line contains n numbers: a1,a2,⋯,an .
Then q lines, each line contain two numbers, pi,vi(1≤pi≤n,1≤vi≤1000000) .
Test data guarantee that 1<≤ai≤1000000 all the time and the sum of all n and q is less than or equal to 2×105 .
Output
For each test case, first output one line "Case #x:", where x is the case number (starting from 1). Then q lines, each line contain only one number, which is the answer to current sequence.
Sample Input
2 3 2 1 2 3 1 3 2 3 3 2 3 3 3 1 1 2 2
Sample Output
Case #1: 3 1 Case #2: 2 3HintFor case 1, after the first operation, 3,2,1 can be generated by the sequence 3, 2, 3. Whereas after the second operation, sequence 3, 3, 3 can generate only 3.
Source
解决一个包含大量数的序列在多次数值更新后能产生的不同区间GCD数量的问题。通过使用线段树和预处理技巧来优化计算过程。

267

被折叠的 条评论
为什么被折叠?



