2016 Multi-University Training Contest 1 1004 hdu 5726 二分+RMQ

来源:互联网 发布:求仁得仁 又何怨乎 编辑:程序博客网 时间:2024/05/17 04:15



链接:戳这里


GCD

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Problem Description
Give you a sequence of N(N≤100,000) integers : a1,...,an(0<ai≤1000,000,000). There are Q(Q≤100,000) queries. For each query l,r you have to calculate gcd(al,,al+1,...,ar) and count the number of pairs(l′,r′)(1≤l<r≤N)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<ai≤1000,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
 


思路:

RMQ统计预处理[l,r]内的gcd值

枚举左端点,随着右端点增大,gcd值越来越小,二分这些变小的节点,统计出线段的gcd值所作的贡献,map累加每个贡献的数量


代码:

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<string>#include<vector>#include<ctime>#include<queue>#include<set>#include<map>#include<stack>#include<iomanip>#include<cmath>#define mst(ss,b) memset((ss),(b),sizeof(ss))#define maxn 0x3f3f3f3f#define MAX 1000100///#pragma comment(linker, "/STACK:102400000,102400000")typedef long long ll;typedef unsigned long long ull;#define INF (1ll<<60)-1using namespace std;ll a[100100];ll mp[100100][65];ll gcd(ll a,ll b){    return b==0?a:gcd(b,a%b);}int n,q;void RMQ_init(){    for(int i=1;i<=n;i++) mp[i][0]=a[i];    int m=log(n)/log(2);    for(int i=1;i<=m;i++){        for(int j=n;j>=1;j--){            mp[j][i]=mp[j][i-1];            if(j+(1<<(i-1))<=n)                mp[j][i]=gcd(mp[j][i],mp[j+(1<<(i-1))][i-1]);        }    }}int query(int l,int r){    int m=log(r-l+1)/log(2);    return gcd(mp[l][m],mp[r-(1<<m)+1][m]);}map<int,ll>V;int main(){    int T;    scanf("%d",&T);    for(int cas=1;cas<=T;cas++){        scanf("%d",&n);        V.clear();        for(int i=1;i<=n;i++) scanf("%I64d",&a[i]);        RMQ_init();        for(int i=1;i<=n;i++){            ll tmp=a[i];            int l=i,r=n,mid,ans=l,O=l;            while(tmp>1){                O=l;r=n;ans=-1;                while(l<=r){                    mid=(l+r)/2;                    if(query(i,mid)<tmp) r=mid-1;                    else {                        l=mid+1;                        ans=mid;                    }                }                if(tmp==1) break;                V[tmp]+=ans-O+1;                l=ans+1;                if(l>n) break;                tmp=query(i,l);            }            if(tmp==1){                V[1]+=n-l+1;            }        }        scanf("%d",&q);        printf("Case #%d:\n",cas);        while(q--){            int l,r;            scanf("%d%d",&l,&r);            ll x=query(l,r);            printf("%I64d %I64d\n",x,V[x]);        }    }    return 0;}


0 0
原创粉丝点击