HDU 5869 Different GCD subarray Query bit+gcd 2016ICPC Dalian Online Tournament

Source: Internet
Author: User
Tags gcd greatest common divisor


Different GCD subarray Query


Time limit:6000/3000 MS (java/others) Memory limit:65536/65536 K (java/others)
Total submission (s): 828 Accepted Submission (s): 300


problem DescriptionThis was a simple problem. The teacher gives Bob a list of problems about GCD (greatest Common Divisor). After studying some of them, Bob thinks that GCD are so interesting. One day, he comes up and a new problem about GCD. Easy as it looks, Bob cannot figure it out himself. Now he turns to the problem:
  
Given an arrayaOfNPositive integersa 1, a2 ,? an−1, an ; A subarray ofais defined as a continuous interval betweena1 andaN . In other words,AI,ai+1,< Span id= "mathjax-span-56" class= "Mo"? , Aj−1,a j is a subarray ofa, for1≤i≤j≤N . For a query in the form(L,R), tell the number of different GCDs contributed by all subarrays of the Interval [L,R].
 




InputThere is several tests, process till the end of input.
  
For each test, the first line consists of integersNandQ, denoting the length of the array and the number of queries, respectively.NPositive integers is listed in the second line, followed byQLines each containing, integersL,R For a query.

Can assume that

1≤N,Q≤100000

1≤ai≤1000000  




Outputfor each query, output the answer on one line. 




Sample Input5 to 3 4 6 for 5 




Sample Output666 




SourceACM/ICPC Asia Regional Dalian Online


Test instructions: There are n numbers in one array (n<=1e5), each number <=1e6, each sub-sequence in the array can produce an entire subsequence of greatest common divisor, there are Q queries (Q<=1E5), each query consists of two numbers, L,r asked subscript from L, How many different gcd there are in the interval to R;


 
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <cmath>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <set>
#define MM(a,b) memset(a,b,sizeof(a));
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
#define CT continue
#define SC scanf
const int N=1e5+10;
int n,qur,a[N],tree[N],ans[N],pos[10*N];

struct node{
   int l,id;
};

int gcd(int a,int b)
{
   if(b==0) return a;
   else return gcd(b,a%b);
}

int lowbit(int i)
{
   return i&(-i);
}

int add(int pos,int val)
{
    while(pos<=n){
        tree[pos]+=val;
        pos+=lowbit(pos);
    }
}

int query(int r)
{
    int s=0;
    while(r>=1){
        s+=tree[r];
        r-=lowbit(r);
    }
    return s;
}

vector<node> q[N],lgcd[N];

void solve()
{
    for(int i=1;i<=n;i++){
        if(pos[a[i]]!=-1) add(pos[a[i]],-1);
        pos[a[i]]=i;
        add(i,1);
        int val=a[i];
        for(int j=i-1;j>=1;j--){
           int k=gcd(val,a[j]);
           if(pos[k]<j){
              if(pos[k]!=-1) add(pos[k],-1);
              pos[k]=j;
              add(j,1);
           }
           if(k==1) break;
           val=k;
        }
        for(int j=0;j<q[i].size();j++){
            int l=q[i][j].l,id=q[i][j].id;
            ans[id]=query(i)-query(l-1);
        }
    }
}

int main()
{
    while(~SC("%d%d",&n,&qur)){
       MM(tree,0);
       MM(pos,-1);
       for(int i=1;i<=n;i++) {
          SC("%d",&a[i]);
          q[i].clear();
       }
       for(int i=1;i<=qur;i++) {
           int l,r;
           SC("%d%d",&l,&r);
           q[r].push_back((node){l,i});
       }
       solve();
       for(int i=1;i<=qur;i++) printf("%d\n",ans[i]);
    }
    return 0;
}


 Analysis:



Error Analysis: When the game thought of the gcd of each number will not be many, but think of the solution is, first of all, from 1 to I (1<=i<=n) The number of GCD species owned, and then for an interval [l,r], with the type of R-type number L-1,,, But there is a clear error that the GCD in [L,l-1] may reappear in [l,r], so it's definitely wrong.



Correct and answer: for such a question,



1. We can fix the right end point from the 1-n, then swipe forward from I, get a gcd, and then use the bit to maintain its gcd the most right position, in bit +1 (can imagine, fixed the right end, the more on the right, regardless of the interval, as far as possible include )



2. GCD attenuation to 1 for up to Loga time. Nlogn*logn of complexity;



HDU 5869 Different GCD subarray Query bit+gcd 2016ICPC Dalian Online Tournament


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.