HDU5726 GCD

Give you a sequence of N(N100,000)N(N≤100,000) integers : a1,...,an(0<ai1000,000,000)a1,...,an(0<ai≤1000,000,000). There are Q(Q100,000)Q(Q≤100,000) queries. For each query l,rl,r you have to calculate gcd(al,,al+1,...,ar)gcd(al,,al+1,...,ar) and count the number of pairs(l,r)(1l<rN)(l′,r′)(1≤l<r≤N)such that gcd(al,al+1,...,ar)gcd(al′,al′+1,...,ar′)equal gcd(al,al+1,...,ar)gcd(al,al+1,...,ar).

InputThe first line of input contains a number TT, which stands for the number of test cases you need to solve. 

The first line of each case contains a number NN, denoting the number of integers. 

The second line contains NN integers, a1,...,an(0<ai1000,000,000)a1,...,an(0<ai≤1000,000,000). 

The third line contains a number QQ, denoting the number of queries. 

For the next QQ lines, i-th line contains two number , stand for the li,rili,ri, stand for the i-th queries. 
OutputFor each case, you need to output “Case #:t” at the beginning.(with quotes, tt 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)gcd(al,al+1,...,ar) and the second number stands for the number of pairs(l,r)(l′,r′) such that gcd(al,al+1,...,ar)gcd(al′,al′+1,...,ar′)equal gcd(al,al+1,...,ar)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

我的就是区间取值+暴力查询,因为gcd很少。他们做法是二分+RMQ,我还是觉得我的做法简单

#include<bits/stdc++.h>
using namespace std;
int fun(int x,int y)
{
    return __gcd(x,y);
}
#define fi first
#define se second
const int N=1e5+5;
int n,a[N],l[N],v[N];
vector<pair<pair<int,int>,int> >ans[N];
unordered_map<int,long long>M;
int main()
{
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    int T,ca=0;
    cin>>T;
    while(T--)
    {

        cin>>n;
        for(int i=1; i<=n; i++)cin>>a[i];
        for(int i=1,j; i<=n; i++)
            for(v[i]=a[i],j=l[i]=i; j; j=l[j]-1)
            {
                v[j]=fun(v[j],a[i]);
                while(l[j]>1&&fun(a[i],v[l[j]-1])==fun(a[i],v[j]))l[j]=l[l[j]-1];
                M[v[j]]+=j-l[j]+1;
                ans[i].push_back(make_pair(make_pair(j,l[j]),v[j]));
            }
        int q;
        cin>>q;
        cout<<"Case #"<<++ca<<":\n";
        for(int i=0,l,r,x;i<q;i++)
        {
            cin>>l>>r;
            for(auto X:ans[r])
            {
                if(X.fi.fi>=l&&X.fi.se<=l)
                {
                    x=X.se;
                    break;
                }
            }
            cout<<x<<" "<<M[x]<<"\n";
        }
        M.clear();
        for(int i=1;i<=n;i++)ans[i].clear();
    }
    return 0;
}

 

posted @ 2018-10-17 09:14  暴力都不会的蒟蒻  阅读(258)  评论(0编辑  收藏  举报