GCD
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1654 Accepted Submission(s): 537
Total Submission(s): 1654 Accepted Submission(s): 537
Problem Description
Give you a sequence of
N(N≤100,000)
integers :
a1,...,an(0<ai≤1000,000,000)
. There are
Q(Q≤100,000)
queries. For each query
l,r
you have to calculate
gcd(al,,al+1,...,ar)
and count the number of pairs
(l′,r′)(1≤l<r≤N)
such that
gcd(al′,al′+1,...,ar′)
equal
gcd(al,al+1,...,ar)
.
The first line of input contains a number
T
, which stands for the number of test cases you need to solve.
The first line of each case contains a number N , denoting the number of integers.
The second line contains N integers, a1,...,an(0<ai≤1000,000,000) .
The third line contains a number Q , denoting the number of queries.
For the next Q lines, i-th line contains two number , stand for the li,ri , stand for the i-th queries.
The first line of each case contains a number N , denoting the number of integers.
The second line contains N integers, a1,...,an(0<ai≤1000,000,000) .
The third line contains a number Q , denoting the number of queries.
For the next Q lines, i-th line contains two number , stand for the li,ri , stand for the i-th queries.
Output
For each case, you need to output “Case #:t” at the beginning.(with quotes,
t
means the number of the test case, begin from 1).
For each query, you need to output the two numbers in a line. The first number stands for gcd(al,al+1,...,ar) and the second number stands for the number of pairs (l′,r′) such that gcd(al′,al′+1,...,ar′) equal gcd(al,al+1,...,ar) .
For each query, you need to output the two numbers in a line. The first number stands for gcd(al,al+1,...,ar) and the second number stands for the number of pairs (l′,r′) such that gcd(al′,al′+1,...,ar′) equal gcd(al,al+1,...,ar) .
Sample Input
1 5 1 2 4 6 7 4 1 5 2 4 3 4 4 4
Sample Output
Case #1: 1 8 2 4 2 4 6 1
HIT
2016 Multi-University Training Contest 1
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5726
题目大意:给一个数列,m次询问,每次给一个区间,求区间内数值的gcd和区间gcd值为所求gcd的区间个数
题目分析:枚举左端点,二分右端点,区间gcd用rmq预处理,个数用map存,复杂度为nlognlogn
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5726
题目大意:给一个数列,m次询问,每次给一个区间,求区间内数值的gcd和区间gcd值为所求gcd的区间个数
题目分析:枚举左端点,二分右端点,区间gcd用rmq预处理,个数用map存,复杂度为nlognlogn
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <map>
#define ll long long
using namespace std;
int const MAX = 1e5 + 5;
int a[MAX], n, m;
int g[1 << 18][18];
map <int, ll> mp;
int gcd(int a, int b) {
return b ? gcd(b, a % b) : a;
}
void RMQ()
{
for(int i = 0; i < n; i++)
g[i][0] = a[i];
for(int j = 1; (1 << j) < n; j++) {
for(int i = 0; (i + (1 << j)) <= n; i++) {
g[i][j] = gcd(g[i][j - 1], g[i + (1 << (j - 1))][j - 1]);
}
}
}
int query(int l, int r) {
int p = (int) log2(r - l + 1.0);
return gcd(g[l][p], g[r - (1 << p) + 1][p]);
}
int Bsearch(int pos, int x) {
int l = pos, r = n - 1, mid, ans = pos;
while(l <= r) {
mid = (l + r) >> 1;
if(query(pos, mid) == x) {
ans = mid;
l = mid + 1;
}
else {
r = mid - 1;
}
}
return ans;
}
int main() {
int T;
scanf("%d", &T);
for(int ca = 1; ca <= T; ca++) {
printf("Case #%d:\n", ca);
mp.clear();
scanf("%d", &n);
for(int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
RMQ();
for(int i = 0; i < n; i++) {
int gnum = a[i], l = i, r = i;
while(l < n) {
r = Bsearch(l, gnum);
mp[gnum] += (ll)r - l + 1;
l = r + 1;
if(l < n)
gnum = gcd(gnum, a[l]);
if(gnum == 1) {
mp[1] += (ll) n - l;
break;
}
}
}
scanf("%d", &m);
while(m -- ) {
int l, r;
scanf("%d %d", &l, &r);
int ans = query(l - 1, r - 1);
printf("%d %I64d\n", ans, mp[ans]);
}
}
}