HDU 5726 GCD (RMQ + 二分)

GCD

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1654    Accepted Submission(s): 537


Problem Description
Give you a sequence of N(N100,000) integers : a1,...,an(0<ai1000,000,000) . There are Q(Q100,000) queries. For each query l,r you have to calculate gcd(al,,al+1,...,ar) and count the number of pairs (l,r)(1l<rN) such that gcd(al,al+1,...,ar) equal gcd(al,al+1,...,ar) .
 
Input
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<ai1000,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) .
 
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
 
Author
HIT
 
Source
2016 Multi-University Training Contest 1

题目链接: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]);
        }
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值