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;
}

 

转载于:https://www.cnblogs.com/BobHuang/p/9802035.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值