题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2301

  题意:多次询问,求有多少对数满足 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>

 //STATUS:C++_AC_2052MS_2052KB
#include <functional>
#include <algorithm>
#include <iostream>
//#include <ext/rope>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cstring>
#include <cassert>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,102400000")
//using namespace __gnu_cxx;
//define
#define pii pair<int,int>
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define PI acos(-1.0)
//typedef
typedef long long LL;
typedef unsigned long long ULL;
//const
const int N=;
const int INF=0x3f3f3f3f;
const int MOD=,STA=;
const LL LNF=1LL<<;
const double EPS=1e-;
const double OO=1e15;
const int dx[]={-,,,};
const int dy[]={,,,-};
const int day[]={,,,,,,,,,,,,};
//Daily Use ...
inline int sign(double x){return (x>EPS)-(x<-EPS);}
template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
template<class T> inline T lcm(T a,T b,T d){return a/d*b;}
template<class T> inline T Min(T a,T b){return a<b?a:b;}
template<class T> inline T Max(T a,T b){return a>b?a:b;}
template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);}
template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);}
template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));}
template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));}
//End int T,n,a,b,c,d,k;
int isprime[N],mu[N],prime[N],sum[N];
int cnt;
void Mobius(int n)
{
int i,j;
//Init phi[N],prime[N],全局变量初始为0
cnt=;mu[]=;
for(i=;i<=n;i++){
if(!isprime[i]){
prime[cnt++]=i; //prime[i]=1;为素数表
mu[i]=-;
}
for(j=;j<cnt && i*prime[j]<=n;j++){
isprime[i*prime[j]]=;
if(i%prime[j])
mu[i*prime[j]]=-mu[i];
else {mu[i*prime[j]]=;break;}
}
}
} LL solve(int n,int m)
{
int i,j,la;
LL ret=;
if(n>m)swap(n,m);
for(i=,la=;i<=n;i=la+){
la=Min(n/(n/i),m/(m/i));
ret+=(LL)(sum[la]-sum[i-])*(n/i)*(m/i);
}
return ret;
} int main(){
// freopen("in.txt","r",stdin);
int i,j;
LL ans;
Mobius();
for(i=;i<;i++)sum[i]=sum[i-]+mu[i];
scanf("%d",&T);
while(T--)
{
scanf("%d%d%d%d%d",&a,&b,&c,&d,&k);
ans=solve(b/k,d/k)-solve((a-)/k,d/k)
-solve((c-)/k,b/k)+solve((a-)/k,(c-)/k);
printf("%lld\n",ans);
}
return ;
}

Bzoj-2301 [HAOI2011]Problem b 容斥原理,Mobius反演,分块的更多相关文章

  1. bzoj 2301 [HAOI2011]Problem b(莫比乌斯反演+分块优化)

    题意:对于给出的n个询问,每次求有多少个数对(x,y),满足a≤x≤b,c≤y≤d,且gcd(x,y) = k,gcd(x,y)函数为x和y的最大公约数. 1≤n≤50000,1≤a≤b≤50000, ...

  2. BZOJ 2301: [HAOI2011]Problem b (莫比乌斯反演)

    2301: [HAOI2011]Problem b Time Limit: 50 Sec  Memory Limit: 256 MBSubmit: 436  Solved: 187[Submit][S ...

  3. bzoj 2301 [HAOI2011]Problem b(莫比乌斯反演)

    Description 对于给出的n个询问,每次求有多少个数对(x,y),满足a≤x≤b,c≤y≤d,且gcd(x,y) = k,gcd(x,y)函数为x和y的最大公约数. Input 第一行一个整数 ...

  4. BZOJ 2301: [HAOI2011]Problem b 莫比乌斯反演

    2301: [HAOI2011]Problem b Time Limit: 50 Sec  Memory Limit: 256 MBSubmit: 1007  Solved: 415[Submit][ ...

  5. Bzoj 2301: [HAOI2011]Problem b(莫比乌斯反演+除法分块)

    2301: [HAOI2011]Problem b Time Limit: 50 Sec Memory Limit: 256 MB Description 对于给出的n个询问,每次求有多少个数对(x, ...

  6. BZOJ 2301 [HAOI2011]Problem b (分块 + 莫比乌斯反演)

    2301: [HAOI2011]Problem b Time Limit: 50 Sec  Memory Limit: 256 MBSubmit: 6519  Solved: 3026[Submit] ...

  7. bzoj 2301: [HAOI2011]Problem b

    2301: [HAOI2011]Problem b Time Limit: 50 Sec Memory Limit: 256 MB Submit: 3757 Solved: 1671 [Submit] ...

  8. BZOJ 2301: [HAOI2011]Problem b( 数论 )

    和POI某道题是一样的...  http://www.cnblogs.com/JSZX11556/p/4686674.html 只需要二维差分一下就行了. 时间复杂度O(MAXN + N^1.5) - ...

  9. bzoj 2301: [HAOI2011]Problem b mobius反演 RE

    http://www.lydsy.com/JudgeOnline/problem.php?id=2301 设f(i)为在区间[1, n]和区间[1, m]中,gcd(x, y) = i的个数. 设F( ...

随机推荐

  1. Python:树的遍历

    各种遍历顺序如下图所示: 树的最大深度  # class TreeNode(object): # def __init__(self, x): # self.val = x # self.left = ...

  2. Python正则表达式re模块

    re.compile(pattern,flags=0)将正则表达式编译成正则表达式对象.可以使用match()和search()方法进行匹配.对于常用的表达式可以先进行编译,后续可多次使用以提高效率. ...

  3. [洛谷P4312][COCI 2009] OTOCI / 极地旅行社

    题目大意:有$n(n\leqslant3\times10^4)$个点,每个点有点权,$m(m\leqslant3\times10^5)$个操作,操作分三种: $bridge\;x\;y:$询问节点$x ...

  4. Codeforces Round #469 (Div. 2) F. Curfew

    贪心 题目大意,有2个宿管分别从1和n开始检查房间,记录人数不为n的房间个数,然后锁住房间. 没有被锁的房间中的学生可以选择藏在床底,留在原地,或者转移(最远转移d个房间)   然后抄了网上大神的代码 ...

  5. BZOJ1047:[HAOI2007]理想的正方形——题解

    http://www.lydsy.com/JudgeOnline/problem.php?id=1047 https://www.luogu.org/problemnew/show/P2216#sub ...

  6. bzoj1024: [SCOI2009]生日快乐(DFS)

    dfs(x,y,n)表示长为x,宽为y,切n块 每次砍的一定是x/n的倍数或者y/n的倍数 #include<bits/stdc++.h> using namespace std; con ...

  7. 阅读android源码了解 android 加载so的流程

    参考原文:http://bbs.pediy.com/thread-217656.htm Android安全–linker加载so流程,在.init下断点: http://www.blogfshare. ...

  8. JavaScript是如何实现继承的

    JavaScript中的function是万能的,除了用于的函数定义,也可以用于类的定义.JavaScript的继承,说起来也是有点怪,没有public,private等访问控制修饰,也没有imple ...

  9. POJ2349:Arctic Network(二分+最小生成树)

    Arctic Network Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 28311   Accepted: 8570 题 ...

  10. How Many Nines ZOJ - 3950 打表大法好

    If we represent a date in the format YYYY-MM-DD (for example, 2017-04-09), do you know how many 9s w ...