# Bzoj-2301 [HAOI2011]Problem b 容斥原理,Mobius反演,分块

题意：多次询问，求有多少对数满足 gcd(x,y)=k, a<=x<=b, c<=y<=d。

对于有下界的区间，容易想到用容斥原理做。然后如果直接用Mobius反演定理做，那么每次询问的复杂度是O(n/k)，如果k=1的话，那么总体就是O(n^2)的复杂度了，会TLE。这样用到了分快优化，注意到 n/i ,在连续的k区间内存在，n/i=n/(i+k)，因此能用分块优化。由于n/d，最多有2*sqrt(n)不相同的数，因此每次询问复杂度2*sqrt(n)+2*sqrt(m)..

详细内容推荐看：<POI XIV Stage.1 Queries Zap>

  1 //STATUS:C++_AC_2052MS_2052KB
2 #include <functional>
3 #include <algorithm>
4 #include <iostream>
5 //#include <ext/rope>
6 #include <fstream>
7 #include <sstream>
8 #include <iomanip>
9 #include <numeric>
10 #include <cstring>
11 #include <cassert>
12 #include <cstdio>
13 #include <string>
14 #include <vector>
15 #include <bitset>
16 #include <queue>
17 #include <stack>
18 #include <cmath>
19 #include <ctime>
20 #include <list>
21 #include <set>
22 #include <map>
23 using namespace std;
25 //using namespace __gnu_cxx;
26 //define
27 #define pii pair<int,int>
28 #define mem(a,b) memset(a,b,sizeof(a))
29 #define lson l,mid,rt<<1
30 #define rson mid+1,r,rt<<1|1
31 #define PI acos(-1.0)
32 //typedef
33 typedef long long LL;
34 typedef unsigned long long ULL;
35 //const
36 const int N=50010;
37 const int INF=0x3f3f3f3f;
38 const int MOD=100000,STA=8000010;
39 const LL LNF=1LL<<60;
40 const double EPS=1e-8;
41 const double OO=1e15;
42 const int dx[4]={-1,0,1,0};
43 const int dy[4]={0,1,0,-1};
44 const int day[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
45 //Daily Use ...
46 inline int sign(double x){return (x>EPS)-(x<-EPS);}
47 template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
48 template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
49 template<class T> inline T lcm(T a,T b,T d){return a/d*b;}
50 template<class T> inline T Min(T a,T b){return a<b?a:b;}
51 template<class T> inline T Max(T a,T b){return a>b?a:b;}
52 template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);}
53 template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);}
54 template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));}
55 template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));}
56 //End
57
58 int T,n,a,b,c,d,k;
59 int isprime[N],mu[N],prime[N],sum[N];
60 int cnt;
61 void Mobius(int n)
62 {
63     int i,j;
64     //Init phi[N],prime[N],全局变量初始为0
65     cnt=0;mu[1]=1;
66     for(i=2;i<=n;i++){
67         if(!isprime[i]){
68             prime[cnt++]=i;  //prime[i]=1;为素数表
69             mu[i]=-1;
70         }
71         for(j=0;j<cnt && i*prime[j]<=n;j++){
72             isprime[i*prime[j]]=1;
73             if(i%prime[j])
74                 mu[i*prime[j]]=-mu[i];
75             else {mu[i*prime[j]]=0;break;}
76         }
77     }
78 }
79
80 LL solve(int n,int m)
81 {
82     int i,j,la;
83     LL ret=0;
84     if(n>m)swap(n,m);
85     for(i=1,la=0;i<=n;i=la+1){
86         la=Min(n/(n/i),m/(m/i));
87         ret+=(LL)(sum[la]-sum[i-1])*(n/i)*(m/i);
88     }
89     return ret;
90 }
91
92 int main(){
93  //   freopen("in.txt","r",stdin);
94     int i,j;
95     LL ans;
96     Mobius(50000);
97     for(i=1;i<50000;i++)sum[i]=sum[i-1]+mu[i];
98     scanf("%d",&T);
99     while(T--)
100     {
101         scanf("%d%d%d%d%d",&a,&b,&c,&d,&k);
102         ans=solve(b/k,d/k)-solve((a-1)/k,d/k)
103             -solve((c-1)/k,b/k)+solve((a-1)/k,(c-1)/k);
104         printf("%lld\n",ans);
105     }
106     return 0;
107 }

posted @ 2013-08-20 02:16  zhsl  阅读(...)  评论(...编辑  收藏