Bzoj-2301 [HAOI2011]Problem b 容斥原理,Mobius反演,分块
题目链接: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反演,分块的更多相关文章
- 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, ...
- BZOJ 2301: [HAOI2011]Problem b (莫比乌斯反演)
2301: [HAOI2011]Problem b Time Limit: 50 Sec Memory Limit: 256 MBSubmit: 436 Solved: 187[Submit][S ...
- 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 第一行一个整数 ...
- BZOJ 2301: [HAOI2011]Problem b 莫比乌斯反演
2301: [HAOI2011]Problem b Time Limit: 50 Sec Memory Limit: 256 MBSubmit: 1007 Solved: 415[Submit][ ...
- Bzoj 2301: [HAOI2011]Problem b(莫比乌斯反演+除法分块)
2301: [HAOI2011]Problem b Time Limit: 50 Sec Memory Limit: 256 MB Description 对于给出的n个询问,每次求有多少个数对(x, ...
- BZOJ 2301 [HAOI2011]Problem b (分块 + 莫比乌斯反演)
2301: [HAOI2011]Problem b Time Limit: 50 Sec Memory Limit: 256 MBSubmit: 6519 Solved: 3026[Submit] ...
- bzoj 2301: [HAOI2011]Problem b
2301: [HAOI2011]Problem b Time Limit: 50 Sec Memory Limit: 256 MB Submit: 3757 Solved: 1671 [Submit] ...
- BZOJ 2301: [HAOI2011]Problem b( 数论 )
和POI某道题是一样的... http://www.cnblogs.com/JSZX11556/p/4686674.html 只需要二维差分一下就行了. 时间复杂度O(MAXN + N^1.5) - ...
- 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( ...
随机推荐
- bzoj1024: [SCOI2009]生日快乐(DFS)
dfs(x,y,n)表示长为x,宽为y,切n块 每次砍的一定是x/n的倍数或者y/n的倍数 #include<bits/stdc++.h> using namespace std; con ...
- Linux之socket套接字编程20160704
介绍套接字之前,我们先看一下传输层的协议TCP与UDP: TCP协议与UDP协议的区别 首先咱们弄清楚,TCP协议和UCP协议与TCP/IP协议的联系,很多人犯糊涂了,一直都是说TCP/IP协议与UD ...
- angular 前台代码分层方法
原代码: 现在将 findAll的get请求部分抽取成 服务,服务就是 $http.get 其实就是 ang内置的服务,其实就是可能会公用的方法,即可能被多个控制器调用的方法 比如这里认为 get请求 ...
- java使用POI操作XWPFDocument中的XWPFParagraph(段落)对象的属性略解
我用的是office word 2016版 创建文本对象 XWPFDocument docxDocument = new XWPFDocument(); 创建段落对象 XWPFParagraph pa ...
- httpClient需要的jar包
- select2 全拼以及首字母
转自:https://blog.csdn.net/kanhuadeng/article/details/78475317 具体实现方法为: 首先需要在网上下载select2的源码,并引入到项目中,具体 ...
- linux-柔性数组
柔性数组:在C99定义的结构体中,最后一个元素允许是未知大小(或者是0)的数组,它不占用结构体任何内存,这个数组叫做柔性数组,柔性数组前必须至少有一个其它成员. 使用方式: #include<s ...
- bzoj [POI2005]Kos-Dicing 二分+网络流
[POI2005]Kos-Dicing Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 1835 Solved: 661[Submit][Status][ ...
- HDU 3487 Splay tree
Play with Chain Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- postman ubuntu 14.04 安装
https://jingyan.baidu.com/album/e3c78d649735d63c4c85f5e5.html?picindex=1 实测有效---