HDU-5726 GCD

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

题目大意:

给你n个数,求l到r区间内的最大公约数ans,并且求任意区间的最大公约数为ans的数量

解题思路:

第一问很裸的RMQ问题,如果裸这个的话我认为是可以线段树搞的,不过洋少跟我说线段树会挂我就没写了。

第二问很蛋疼。我想了很久,看着官方题解想了很久。毕竟智商略低0.0其实很简单,对于从l到r这个区间,如果固定l区间,r区间不断变化的时候,gcd的值在不断下降,而且每次下降应该都不小于一半,那么这个下降速度是很快的。那么只需要枚举左端点l,然后二分l到n的区间,找到最大公约数为gcd的最大区间,然后记录这个gcd的数量,并且把gcd更新,这样时间复杂度能控制为nlog(a)

这道题还是很有意思的,值的推敲。

ps:写的时候sabi了一下,先计算区间个数了,然后计算的RMQ预处理。于是各种各样姿势的TLE,这比赛没法打了,,,,

代码:

#include <map>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

typedef long long LL;

const int maxn = 1e5 + 5;

int n;
map<int, LL> mm;
int a[maxn], dp[maxn][20];

int Scan() {
    int res = 0, flag = 0;
    char ch;
    if((ch = getchar()) == '-') flag = 1;
    else if(ch >= '0' && ch <= '9') res = ch - '0';
    while((ch = getchar()) >= '0' && ch <= '9')
        res = res * 10 + (ch - '0');
    return flag ? -res : res;
}
void Out(int a) {
    if(a < 0) { putchar('-'); a = -a; }
    if(a >= 10) Out(a / 10);
    putchar(a % 10 + '0');
}
int gcd(int x, int y){
    return y ? gcd(y, x % y) : x;
}
void ST(){
    for(int j = 1; j < 18; ++j){
        for(int i = 1; i <= n; ++i){
            if(i + (1 << j) - 1 <= n){
                dp[i][j] = gcd(dp[i][j-1], dp[i + (1 << (j-1))][j-1]);
            }else break;
		}
	}
}
int Find(int l, int r){
    int k = (int)log2((double)(r - l + 1));
    return gcd(dp[l][k], dp[r-(1<<k)+1][k]);
}
int main()
{
    int t, q;
    t = Scan();
    for(int cas = 1; cas <= t; ++cas){
        n = Scan();
        mm.clear();
        for(int i = 1; i <= n; ++i){
            a[i] = Scan();
            dp[i][0] = a[i];
        }

        ST();
        for(int i = 1; i <= n; ++i){
            int j = i, g = a[i];
            while(j <= n){
                int l = j, r = n;
                while(l < r){
                    int mid = (l + r) >> 1;
                    if(Find(l, r) == g) l = mid + 1;
                    else r = mid - 1;
                }
                mm[g] += l - j + 1;
                j = l + 1;
                g = gcd(g, a[j]);
            }
        }

        q = Scan();
        printf("Case #%d:\n", cas);
        while(q--){
            int l, r;
            l = Scan(); r = Scan();

            int ans = Find(l, r);
            printf("%d %I64d\n",ans, mm[ans]);
        }
    }
	return 0;
}


posted @ 2016-07-21 23:13  _Wilbert  阅读(140)  评论(0编辑  收藏  举报