The sum of gcd
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1367 Accepted Submission(s): 596
Problem Description
You have an array
A,the length of
A is
n
Let
f(l,r)=∑ri=l∑rj=igcd(ai,ai+1....aj)
Input
There are multiple test cases. The first line of input contains an integer T, indicating the number of test cases. For each test case:
First line has one integers
n
Second line has
n integers
Ai
Third line has one integers
Q,the number of questions
Next there are Q lines,each line has two integers
l,
r
1≤T≤3
1≤n,Q≤104
1≤ai≤109
1≤l<r≤n
Output
For each question,you need to print
f(l,r)
Sample Input
2 5 1 2 3 4 5 3 1 3 2 3 1 4 4 4 2 6 9 3 1 3 2 4 2 3
Sample Output
Author
SXYZ
Source
2015 Multi-University Training Contest 8
Recommend
wange2014
题意:给你一个序列有n个数,有m次询问,每次询问一个区间所有子区间gcd的和
解题思路:线段树,因为对一个数来所,它的gcd是不会超过log2(n)个的,所以可以每个节点记录从最左端向右延伸和从最右端向左延伸所有gcd的种类和个数,同时记录下这个区间的gcd和这个区间的答案
#include
#include
#include
#include
#include
#include
#include