其他分享
首页 > 其他分享> > P - 区间与其他数互质数的个数 HDU - 4777

P - 区间与其他数互质数的个数 HDU - 4777

作者:互联网

Rabbit Kingdom

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3360    Accepted Submission(s): 1135


题目链接

http://acm.hdu.edu.cn/showproblem.php?pid=4777

Problem Description

  Long long ago, there was an ancient rabbit kingdom in the forest. Every rabbit in this kingdom was not cute but totally pugnacious, so the kingdom was in chaos in season and out of season.
  n rabbits were numbered form 1 to n. All rabbits' weight is an integer. For some unknown reason, two rabbits would fight each other if and only if their weight is NOT co-prime.
  Now the king had arranged the n rabbits in a line ordered by their numbers. The king planned to send some rabbits into prison. He wanted to know that, if he sent all rabbits between the i-th one and the j-th one(including the i-th one and the j-th one) into prison, how many rabbits in the prison would not fight with others.
  Please note that a rabbit would not fight with himself.  

 

Input

  The input consists of several test cases.
  The first line of each test case contains two integer n, m, indicating the number of rabbits and the queries.
  The following line contains n integers, and the i-th integer Wi indicates the weight of the i-th rabbit.
  Then m lines follow. Each line represents a query. It contains two integers L and R, meaning the king wanted to ask about the situation that if he sent all rabbits from the L-th one to the R-th one into prison.
  (1 <= n, m, Wi <= 200000, 1 <= L <= R <= n)
  The input ends with n = 0 and m = 0.  

 

Output

  For every query, output one line indicating the answer.  

 

Sample Input

3 2 2 1 4 1 2 1 3 6 4 3 6 1 2 5 3 1 3 4 6 4 4 2 6 0 0

Sample Output

2 1 1 3 1 2

Hint

  In the second case, the answer of the 4-th query is 2, because only 1 and 5 is co-prime with other numbers in the interval [2,6] .  

 

Source

2013 Asia Hangzhou Regional Contest  

题意

给你一个区间,然后求每个区间内与其他数都互质的数有多少个。  

题解

树链剖分专题中乱入一道树状数组的题目,然后就开始想啊想啊,最终放弃思考,瞥了一眼题解,想起了HH的项链,然后觉得相似,就做啊做啊,最后发现有不可弥补的漏洞,继续看题解,原来和我想法差那么远呀。   不知道大家有没有遇见过这样一道题;一个序列,每个位置i都有一个依赖位置Y[i],且i和Y[i]互相依赖(即i依赖Y[i],则Y[i]依赖i),现给你任意多个区间(l,r),求有多少点依赖点也在这个区间里。 如果没做过没关系,因为我接下来会将两题连续细细道来。 可以先不看橙字部分。。。   开始正经讲题解啦 :   第一步也是关键的一步,想不到就没办法:就是求每个位置i,前面第一个与它不互质的数的位置L[i],以及后面第一个不互质数的位置R[i]。至于这个怎么求,等下再讲。   有了这个数组之后,问题转化为求一个区间(l,r)内L[i]和R[i]都不在这个区间的数有多少个。   如果这么想,每个位置i会有两个影响值,就不好做了,那么我们反过来求有多少i是不合法的。 不合法的个数=L[i]>=l的个数+R[i]<=r的个数-l<=L[i]&&R[i]<=r的个数。   然后我们将所有询问区间按照右端点从小到大排序,保证扫描时右端点ll递增。   为了不那么抽象,我还是先讲做法再讲原理吧:   一开始ll设为0,表示没有一个位置被更新。 每个询问区间我们定义为(li,ri)     1.依次枚举询问区间把未更新的区间(即ll+1 ~ ri) 依次更新,对于每个位置j(ll+1<=j<=ri),来说这样枚举的一个非常重要的性质:剩下的询问区间r[i]>=j(即如果这个区间包含l[j]那么一定包含j)   2.这样我们是不是把l[j]加上1即可(我称这种模型为点对贡献模型,也就是橙色字体的那题)当然要是r[j]<=ri,l[j]就没有用了,这时我们对于j,枚举所有r[x]==j的点,令x位置+1,l[x]位置-1,(即区间包含x,一定包含r[x],且包含l[x]也不会对x有影响)   3.统计答案直接求sum(li,ri),即li到ri的和。   这样题目就算做完了,这种题目第一次做感觉很晕,建议研究一下: 1.为什要保证右端点单增 2.怎样统计点对答案 3.可不可以换成左端点单增   还有最后一个东西,那就算怎么求l[i]和r[i]呢?我们可以求出每个数的质因子,然后用一个last数组存每个质因子上一次在哪里出现,对于每个点i,枚举所有它的质因子zj,l[i]=max(l[i],last[zj]),r[i]同理。  

代码:

  1 #include<bits/stdc++.h>
  2 using namespace std;
  3 #define ll long long
  4 #define N 200050
  5 const int maxn=200000;
  6 int n,m,P_num;
  7 int prime[N],f[N],last[N],ans[N],c[N];
  8 int L[N],R[N];
  9 vector<int> V[N];
 10 struct Query
 11 {
 12   int l,r,id;
 13   bool operator <(const Query&b)const
 14   {return r<b.r;}
 15 }que[N];
 16 template<typename T>void read(T&x)
 17 {
 18   ll k=0; char c=getchar();
 19   x=0;
 20   while(!isdigit(c)&&c!=EOF)k^=c=='-',c=getchar();
 21   if (c==EOF)exit(0);
 22   while(isdigit(c))x=x*10+c-'0',c=getchar();
 23   x=k?-x:x;
 24 }
 25 void read_char(char &c)
 26 {while(!isalpha(c=getchar())&&c!=EOF);}
 27 void update(int x,int tt){while(x<=maxn){c[x]+=tt;x+=x&-x;}}
 28 int query(int x){int ans=0;while(x){ans+=c[x];x-=x&-x;}return ans;}
 29 void init()
 30 {
 31   for(int i=2;i<=maxn;i++)
 32     {
 33       if (f[i]==0)
 34     {
 35       prime[++P_num]=i;
 36       int k=i;
 37       while(k+i<=maxn)f[k+i]=1,k+=i;
 38     }
 39     }
 40 }
 41 void add(int i,int zs)
 42 {
 43   R[last[zs]]=min(R[last[zs]],i);
 44   L[i]=max(L[i],last[zs]);
 45   last[zs]=i;
 46 }
 47 void work()
 48 {
 49   read(n); read(m);
 50   if (n==0)exit(0);
 51   for(int i=1;i<=n;i++)
 52     {
 53       int x;
 54       L[i]=0; R[i]=n+1;
 55       read(x);
 56       for(int j=1;j<=P_num&&x>1&&f[x]==1;j++)
 57     if (x%prime[j]==0)
 58       {
 59         add(i,prime[j]);
 60         while(x%prime[j]==0)x/=prime[j];
 61       }
 62       if (x>1)add(i,x);
 63     }
 64   for(int i=1;i<=n;i++) V[R[i]].push_back(i);
 65   for(int i=1;i<=m;i++)
 66     {
 67       read(que[i].l); read(que[i].r);
 68       que[i].id=i;
 69     }
 70   sort(que+1,que+m+1);
 71   int r=0;
 72   for(int i=1;i<=m;i++)
 73     {
 74       for(int j=r+1;j<=que[i].r;j++)
 75     {
 76       if (L[j])update(L[j],1);
 77       for(int k=0;k<V[j].size();k++)
 78         {
 79           if (L[V[j][k]])update(L[V[j][k]],-1);
 80           update(V[j][k],1);
 81         }
 82     }
 83       r=que[i].r;
 84       ans[que[i].id]=que[i].r-que[i].l+1;
 85       ans[que[i].id]-=query(que[i].r)-query(que[i].l-1);
 86     }
 87   for(int i=1;i<=m;i++)printf("%d\n",ans[i]);
 88 }
 89 void clear()
 90 {
 91   for(int i=1;i<=maxn;i++)V[i].clear();
 92   memset(last,0,sizeof(last));
 93   memset(c,0,sizeof(c));
 94 }
 95 int main()
 96 {
 97 #ifndef ONLINE_JUDGE
 98   freopen("aa.in","r",stdin);
 99 #endif
100   init();
101   while(1)
102     {
103       clear();
104       work();
105     }
106 }
View Code

 

 

标签:prime,HDU,rabbits,int,互质数,ll,4777,th,区间
来源: https://www.cnblogs.com/mmmqqdd/p/10799621.html