hdu 5726(二分)

GCD

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

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
/*
难点在于计数,关键要发现gcd的递减性
对于1到n的(l,r)
对于固定的l
gcd(l,r)>=gcd(l,r+1)
对于固定的r
gcd(l,r)<=gcd(l+1,r)
因此可以不用逐一地进行计数
方法为:
枚举每一个左区间,对满足gcd(l,ri)的右区间进行二分查找
跳跃着进行计数
*/
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <iostream>
#include <map>
#include <vector>
#define scan1(x)     scanf("%d",&x)
#define scan2(x,y)   scanf("%d%d",&x,&y)
#define scan3(x,y,z) scanf("%d%d%d",&x,&y,&z)
using namespace std;
typedef long long LL;
const int inf=0x3f3f3f3f;
const int Max=1e6+10;
int dp[Max][30];
map<int,LL> vis;
int A[Max];
int gcd(int x,int y)
{
    if(x<y) swap(x,y);
    return (y==0?x:gcd(y,x%y));
}
void RMQ_init(int n)
{
    for(int i=1; i<=n; i++) dp[i][0]=A[i];
    for(int j=1; (1<<j)<=n; j++)
    {
        for(int i=1; i+(1<<j)-1<=n; i++)
        {
            dp[i][j]=gcd(dp[i][j-1],dp[i+(1<<(j-1))][j-1]);
        }
    }
}
int RMQ(int l,int r)
{
    int k=0;
    while((1<<(k+1))<=r-l+1) k++;
    return gcd(dp[l][k],dp[r-(1<<k)+1][k]);
}
void Init()
{
   vis.clear();
}
int L[Max],R[Max];
int main()
{
    int T,ca=1;
    for(scan1(T); T; T--)
    {
        int n,m,num;
        scan1(n);
        for(int i=1; i<=n; i++) scan1(A[i]);
        RMQ_init(n);
        Init();
        scan1(m);
        for(int i=1; i<=m; i++)
        {
            scan2(L[i],R[i]);
            num=RMQ(L[i],R[i]);
            vis.insert(make_pair(num,0));
        }
        int l,r,mid,d1,d2,ans,nex;
        for(int i=1; i<=n; i++)
        {
            nex=i;
            while(nex<=n)
            {
                d1=RMQ(i,nex);
                l=nex;r=n;
                while(l<=r)
                {
                    mid=(l+r)>>1;
                    d2=RMQ(i,mid);
                    if(d2>=d1) l=mid+1,ans=mid;
                    else r=mid-1;
                }
                if(vis.find(d1)!=vis.end())
                vis[d1]+=(ans-nex)+1;
                nex=r+1;
            }
        }
        printf("Case #%d:\n",ca++);
        for(int i=1; i<=m; i++)
        {
            ans=RMQ(L[i],R[i]);
            printf("%d %lld\n",ans,vis[ans]);
        }
    }
    return 0;
}
时间: 2024-10-07 05:24:51

hdu 5726(二分)的相关文章

HDU 5726 GCD 区间GCD=k的个数

GCD Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2742    Accepted Submission(s): 980 Problem Description Give you a sequence of N(N≤100,000) integers : a1,...,an(0<ai≤1000,000,000). There ar

hdu 2255 二分图带权匹配 模板题

模板+注解在 http://blog.csdn.net/u011026968/article/details/38276945 hdu 2255 代码: //KM×î´ó×îСƥÅä #include <cstdio> #include <cstring> #include <algorithm> #include <iostream> using namespace std; #define INF 0x0fffffff const int MAXN

Hdu 2389 二分匹配

题目链接 Rain on your Parade Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 655350/165535 K (Java/Others)Total Submission(s): 2644    Accepted Submission(s): 823 Problem Description You’re giving a party in the garden of your villa by the sea. T

hdu 4737 二分或暴力

http://acm.hdu.edu.cn/showproblem.php?pid=4737 Problem Description There are n numbers in a array, as a0, a1 ... , an-1, and another number m. We define a function f(i, j) = ai|ai+1|ai+2| ... | aj . Where "|" is the bit-OR operation. (i <= j)

HDU 3656 二分+dlx判定

Fire station Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1308    Accepted Submission(s): 434 Problem Description A city's map can be seen as a two dimensional plane. There are N houses in

HDU 5726 GCD (RMQ + 二分)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5726 给你n个数,q个询问,每个询问问你有多少对l r的gcd(a[l] , ... , a[r]) 等于的gcd(a[l'] ,..., a[r']). 先用RMQ预处理gcd,dp[i][j] 表示从i开始2^j个数的gcd. 然后用map存取某个gcd所对应的l r的数量. 我们可以在询问前进行预处理,先枚举i,以i为左端点的gcd(a[i],..., a[r])的种类数不会超过log2(n)

hdu 5726 GCD 倍增+ 二分

题目链接 给n个数, 定义一个运算f[l,r] = gcd(al, al+1,....ar). 然后给你m个询问, 每次询问给出l, r. 求出f[l, r]的值以及有多少对l', r' 使得f[l, r] = f[l', r']. 第一个很简单, 用倍增的思想就可以了. 然后是第二个, 我们枚举每一个左端点i, 显然f[i, j]是只降不增的. 那么我们可以二分找到所有使得f[i, j]下降的值j. 因为gcd每次至少变为原来的二分之一, 而ai最大为1e9. 所以最多只有log2(1e9)个

HDU 5726 GCD(DP)

[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5726 [题目大意] 给出数列An,对于询问的区间[L,R],求出区间内数的GCD值,并且求出GCD值与其相等的区间总数 [题解] 首先,固定一个区间的右端点,利用GCD的递减性质,可以求出GCD相等的区间左端点的范围,将其范围的左右端点保存下来,同时,对于每个新产生的区间,以其GCD值为下标的MAP值+1,最后对于每个询问,在其右端点保存的范围中查找,获得其GCD值,同时在MAP中获取该GCD值

HDU 5652 二分加搜索 http://acm.split.hdu.edu.cn/showproblem.php?pid=5652

Problem Description A long time ago there are no himalayas between India and China, the both cultures are frequently exchanged and are kept in sync at that time, but eventually himalayas rise up. With that at first the communation started to reduce a