zoukankan      html  css  js  c++  java
  • 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<<":
    ";
            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]<<"
    ";
            }
            M.clear();
            for(int i=1;i<=n;i++)ans[i].clear();
        }
        return 0;
    }
  • 相关阅读:
    Apple Mac OS X每日一技巧002:如何修改打开文档的默认程序
    Baby听世界03:这就是电影啊??!!
    八卦一下,51JOB人才库搜索上海地区含有.NET关键字的期望收入和对应的人数
    接口和抽象类的区别
    总结virtual override sealed new在方法上的应用
    JQuery学习笔记01JQuery初接触
    《C#妹妹和ObjectiveC阿姨对话录》(03)NSString--再遇狗狗
    Mac新手常见问题
    Baby听世界01:产检
    Apple Mac OS X每日一技巧001:如何添加删除开机启动程序项
  • 原文地址:https://www.cnblogs.com/BobHuang/p/9802035.html
Copyright © 2011-2022 走看看