HDU - 5726(56/600)

Give you a sequence of N(N≤100,000)N(N≤100,000) integers : a1,…,an(0

#include
using namespace std;
int inf=0x3f3f3f3f;
int f[100001][30],tu[100001];
int n, m;
int gcd(int q,int w)
{
    if(!w)return q;
    return __gcd(w,q%w);
}
void rmq(int c)
{
    int cc = log2(c)+1;
    for (int a = 1; a <= cc; a++)
    {
        for (int b = 1; b <= c; b++)
        {
            if (b + (1 << a) - 1 <= c)
            {
                f[b][a] = __gcd(f[b][a - 1], f[b + (1 << (a - 1))][a - 1]);
        //      fx[b][a] = min(fx[b][a - 1], fx[b + (1 << (a - 1))][a - 1]);
            }
        }
    }
}
int xunwenda(int zuo, int you)
{
    int k = log2(you - zuo + 1);
    return __gcd(f[zuo][k], f[you - (1 << k) + 1][k]);
}
int jiance(int z,int y,int e)
{
    if(xunwenda(z,y)==e)return 1;
    if(xunwenda(z,y)>e)return 2;
    return 0;
}
int r,t;
int main()
{
    int T;
    cin>>T;
    int u=0;
    while(T--)
    {
        memset(f,0,sizeof(f));
        printf("Case #%d:\n",++u);
        cin>>n;
        for(int a=1;a<=n;a++)scanf("%d",&tu[a]);
        for(int a=1;a<=n;a++)f[a][0]=tu[a];
        rmq(n);
        cin>>m;
        map<long long ,long long >mp;
        for(int a=1;a<=n;a++)
        {
            int zz=a;
            while(zz<=n)
            {
                int cs=xunwenda(a,zz);
                int z=a,y=n;
                int daan=0;
                while(z<=y)
                {
                    int mid=(z+y)/2;
                    int tut=jiance(a,mid,cs);
                    if(tut==1)
                    {
                        z=mid+1;
                        daan=max(daan,mid);
                    }
                    else if(tut==2)
                    {
                        z=mid+1;
                    }
                    else y=mid-1;
                }
                mp[cs]+=daan-zz+1;
                zz=daan+1;
            }
        }
        for(int a=1;a<=m;a++)
        {
            scanf("%d%d",&r,&t);
            printf("%d %lld\n",xunwenda(r,t),mp[xunwenda(r,t)]);
        }
    }
}

你可能感兴趣的:(ACM练习,rmq,二分)