题目链接: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. 【bzoj1877】[SDOI2009]晨跑 费用流

    题目描述 Elaxia最近迷恋上了空手道,他为自己设定了一套健身计划,比如俯卧撑.仰卧起坐等 等,不过到目前为止,他坚持下来的只有晨跑. 现在给出一张学校附近的地图,这张地图中包含N个十字路口和M条街 ...

  2. 【bzoj1076】[SCOI2008]奖励关 期望dp+状态压缩dp

    题目描述 你正在玩你最喜欢的电子游戏,并且刚刚进入一个奖励关.在这个奖励关里,系统将依次随机抛出k次宝物,每次你都可以选择吃或者不吃(必须在抛出下一个宝物之前做出选择,且现在决定不吃的宝物以后也不能再 ...

  3. [洛谷P3979]遥远的国度

    题目大意:有一棵$n$个点的树,每个点有一个点权,有三种操作: $1\;x:$把根变成$x$ $2\;u\;v\;x:$把路径$u->v$上的点权改为$x$ $3\;x:$询问以$x$为根的子树 ...

  4. BZOJ1059:[ZJOI2007]矩阵游戏——题解

    http://www.lydsy.com/JudgeOnline/problem.php?id=1059 https://www.luogu.org/problemnew/show/P1129 小Q是 ...

  5. 项目管理---git----快速使用git笔记(三)------coding.net注册和新建项目(远程仓库)

    我们在第一章已经了解了github和coding.net的区别: github是一个基于git的代码托管平台,付费用户可以建私人仓库,我们一般的免费用户只能使用公共仓库,也就是代码要公开. codin ...

  6. kafka-zk-安装测试初体验

    第一步: 安装 安装工具brew install kafka 会自动安装依赖zookeeper 注:安装配置文件位置 /usr/local/etc/kafka|zookeeper 注: #tickTi ...

  7. redux的一些插件总结(redux-actions,reselect)

    redux本身还是过于简单,实际使用的时候需要配合许多插件. 下面是一些插件与vuex的功能对比 redux-actions <=> vuex的mutation的写法 reselect & ...

  8. Star sky 二维前缀和

    C. Star sky time limit per test 2 seconds memory limit per test 256 megabytes input standard input o ...

  9. Dynamic len(set(a[L:R])) UVA - 12345(这么过分一定要写博客)

    给出一个有n个元素的数组,有以下两种操作:Q x y,求出区间[x,y)内不同元素的个数, M x y,把第x个元素的值修改为y.注意题目中的下标是从0开始的 这题超级超级坑 妈的一个水题找了几个小时 ...

  10. notify()与notifyAll()

    notify() :随机唤醒一个线程. notifyAll():唤醒等待某个锁的所有任务. 在技术上,可能会有多个任务在所创建的任务上处于wait()状态,调用notifyAll()比只调用notif ...