HDU 5726-GCD(暴力+map)

来源:互联网 发布:2016cpa电影网站源码 编辑:程序博客网 时间:2024/06/04 17:44

GCD

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


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
151 2 4 6 741 52 43 44 4
 

Sample Output
Case #1:1 82 42 46 1
 

Author
HIT
 

Source
2016 Multi-University Training Contest 1
 

Recommend
wange2014   |   We have carefully selected several similar problems for you:  5808 5807 5806 5805 5804

题目意思:

有一串N个数字,Q个查询给出一组区间,求区间内各个数的GCD以及共有多少个区间的书的GCD与给定区间的GCD相同。

解题思路:



下面的代码是标程,根据题解说说自己的理解。
每输入一个数a[i]就进行一次处理:依次计算以a为结尾的子序列的GCD,就是说a所在的位置是区间的最右端点,
定义vector<pair<int,int> > g[maxn];其中first:gcd;second:区间左端点。
即first=GCD(a[second],a[i]),g[i][j]中i表示输入的第i个数,j表示当前处理第i个数时保存的第j组不同GCD。
定义map<ll,ll>m;m[i]=j,表示GCD为i的区间共有j个。
所以保存记录map的时候,m[g[i][j].first]+=(j==g[i].size()-1?i+1:g[i][j+1].second)-g[i][j].second;
对单个的a[i]GCD为本身进行了特判,其它的就是利用下一个的区间左端减去当前区间左端的差值作为当前新增的GCD个数。
扫map之前找GCD的查询的时,固定右端点,查询左端点。
#include <iostream>#include <algorithm>#include <cstring>#include <cstdio>#include <map>#include <vector>#include <cstdlib>#define maxn 100010#define ll long longusing namespace std;map<ll,ll>m;vector<pair<int,int> > g[maxn];//first:gcd;second:区间左端点int gcd(int a,int b)//求ab两数的最大公约数{    return b?gcd(b,a%b):a;}ll a[maxn];int main(){    ios::sync_with_stdio(false);    cin.tie(0);    int t,ca=0;    cin>>t;    while(t--)    {        m.clear();        cout<<"Case #"<<++ca<<":"<<endl;        int n,q,l,r,i,j;        cin>>n;        for(int i=0; i<=n; ++i)            g[i].clear();//不清空会超内存        //预处理出所有的以L开头的左区间的gcd(al,al+1,...,ar):        //对于每个数字,记录从开始到当前数字的区间的子区间中以当前数字为结尾区间的gcd        //相同gcd区间合并        for(i=1; i<=n; ++i)        {            int last=0;            cin>>a[i];            for(j=0; j<g[i-1].size(); ++j)            {                int tmp=gcd(a[i],g[i-1][j].first);                if(tmp==last) continue;//上一个出现过该gcd                last=tmp;                g[i].push_back(make_pair(tmp,g[i-1][j].second));            }            if(last!=a[i])//当前a与自己的gcd在上面的循环中没有出现过                g[i].push_back(make_pair(a[i],i));            //用一个map来记录,gcd值为key的有多少个            for(j=0; j<g[i].size(); ++j)            {                m[g[i][j].first]+=(j==g[i].size()-1?i+1:g[i][j+1].second)-g[i][j].second;                // cout<<g[i][j].first<<" "<<m[g[i][j].first]<<endl;            }        }        cin>>q;        for(i=0; i<q; ++i)        {            cin>>l>>r;            for(j=0; j<g[r].size(); ++j)//对于每个询问只需要查询对应gcd(al,al+1,...,ar)为多少                if(g[r][j].second>l) break;            cout<<g[r][j-1].first<<" "<<m[g[r][j-1].first]<<endl;        }    }    return 0;}/**151 2 4 6 741 52 43 44 4**/


0 0