Bzoj-2820 YY的GCD Mobius反演,分块
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2820
题意:多次询问,求1<=x<=N, 1<=y<=M且gcd(x,y)为质数有多少对。
首先, 
由于这里是多次询问,并且数据很大,显然不能直接求解,需要做如下处理。。
整数的除法是满足结合律的,然后我们设T=p*d,有:
注意到后面部分是可以预处理出来的,那么整个ans就可以用分块处理来求了,设
那么有
,考虑当p|x时,根据莫比菲斯mu(x)的性质,px除以其它非p的质数因数都为0,所以g(px)=mu(x)。当p!|x时,除数为p时为mu(x),否则其它的和为-g(x),因为这里还乘了一个p所以要变反。然后O(n)预处理下就可以了。。
//STATUS:C++_AC_3660MS_274708KB
#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 LL sum[N],g[N];
int isprime[N],mu[N],prime[N];
int cnt;
int T,n,m; void Mobius(int n)
{
int i,j;
//Init isprime[N],mu[N],prime[N],全局变量初始为0
cnt=;mu[]=;
for(i=;i<=n;i++){
if(!isprime[i]){
prime[cnt++]=i;
mu[i]=-;
g[i]=;
}
for(j=;j<cnt && i*prime[j]<=n;j++){
isprime[i*prime[j]]=;
if(i%prime[j]){
mu[i*prime[j]]=-mu[i];
g[i*prime[j]]=mu[i]-g[i];
}
else {
mu[i*prime[j]]=;
g[i*prime[j]]=mu[i];
break;
}
}
}
for(i=;i<=n;i++)sum[i]=sum[i-]+g[i];
} int main(){
// freopen("in.txt","r",stdin);
int i,j,la;
LL ans;
Mobius();
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m); if(n>m)swap(n,m);
ans=;
for(i=;i<=n;i=la+){
la=Min(n/(n/i),m/(m/i));
ans+=(sum[la]-sum[i-])*(n/i)*(m/i);
} printf("%lld\n",ans);
}
return ;
}
Bzoj-2820 YY的GCD Mobius反演,分块的更多相关文章
- [BZOJ 2820] YY的gcd(莫比乌斯反演+数论分块)
[BZOJ 2820] YY的gcd(莫比乌斯反演+数论分块) 题面 给定N, M,求\(1\leq x\leq N, 1\leq y\leq M\)且gcd(x, y)为质数的(x, y)有多少对. ...
- Bzoj 2820: YY的GCD(莫比乌斯反演+除法分块)
2820: YY的GCD Time Limit: 10 Sec Memory Limit: 512 MB Description 神犇YY虐完数论后给傻×kAc出了一题给定N, M,求1<=x& ...
- BZOJ 2820: YY的GCD [莫比乌斯反演]【学习笔记】
2820: YY的GCD Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 1624 Solved: 853[Submit][Status][Discu ...
- bzoj 2820 YY的GCD 莫比乌斯反演
题目大意: 给定N, M,求1<=x<=N, 1<=y<=M且gcd(x, y)为质数的(x, y)有多少对 这里就抄一下别人的推断过程了 后面这个g(x) 算的方法就是在线性 ...
- bzoj 2820 YY的GCD - 莫比乌斯反演 - 线性筛
Description 神犇YY虐完数论后给傻×kAc出了一题给定N, M,求1<=x<=N, 1<=y<=M且gcd(x, y)为质数的(x, y)有多少对kAc这种 傻×必 ...
- BZOJ 2820: YY的GCD 莫比乌斯反演_数学推导_线性筛
Code: #include <cstdio> #include <algorithm> #include <cstring> #include <vecto ...
- BZOJ 2820 YY的GCD ——莫比乌斯反演
我们可以枚举每一个质数,那么答案就是 $\sum_{p}\sum_{d<=n}\mu(d)*\lfloor n / pd \rfloor *\lfloor m / pd \rfloor$ 直接做 ...
- 【莫比乌斯反演】关于Mobius反演与gcd的一些关系与问题简化(bzoj 2301 Problem b&&bzoj 2820 YY的GCD&&BZOJ 3529 数表)
首先我们来看一道题 BZOJ 2301 Problem b Description 对于给出的n个询问,每次求有多少个数对(x,y),满足a≤x≤b,c≤y≤d,且gcd(x,y) = k,gcd( ...
- 【刷题】BZOJ 2820 YY的GCD
Description 神犇YY虐完数论后给傻×kAc出了一题给定N, M,求1<=x<=N, 1<=y<=M且gcd(x, y)为质数的(x, y)有多少对kAc这种傻×必然 ...
随机推荐
- Hardwood Species
http://poj.org/problem?id=2418 #include<cstdio> #include<cstring> #include<string> ...
- IText 中文字体解决方案 生成doc文档
IText生成doc文档需要三个包:iTextAsian.jar,iText-rtf-2.1.4.jar,iText-2.1.4.jar 亲测无误,代码如下: import com.lowagie.t ...
- c缺陷与陷阱笔记-第四章 连接
1.变量的声明. 在不同的源文件中,应该是1个定义+多个声明的形式存在的,并且声明的类型和定义的类型要一样,否则可能会报错. 声明 : extern 类型 变量名 声明并定义: extern 类型 变 ...
- 【mysql的设计与优化专题(1)】ER图,数据建模与数据字典
需求分析是做项目中的极为重要的一环,而作为整个项目中的'血液'--数据,更是重中之重.viso,workbench,phpmyadmin等软件可以帮我们更好的处理数据分析问题. ER图 E-R方法是& ...
- iOS顶部滑动菜单:FDSlideBar 与NinaPagerView
FDSlideBar 是一个顶部滑动菜单,如常见的网易.腾讯新闻等样式.该控件支持自定颜色.字体等多种样式风格.菜单间切换流畅,具有较好的体验性.下部的内容展示经过挣 扎,最后选择了 UITableV ...
- QString和char字符数组之间的转换(QTextCodec.toUnicode方法,特别注意\0的问题)
How can I convert a QString to char* and vice versa ?(trolltech) Answer:In order to convert a QStrin ...
- poj3321Apple Tree(树状数组)
http://poj.org/problem?id=3321 刚一看题以为要建一颗树 看了下讨论说dfs 这里dfs遍历时设的标号很好 一个low一个high 包含了以这一节点为根节点的子树结点的所有 ...
- php简单实现MVC
在PHP中使用MVC越来越流行了,特别是在一些开源的框架当中.MVC足以应对大多数的情况,但还有一些情况是其不太适合的,如比较简单的个人博客,对于只有几百篇文章量级的博客,使用MVC让人觉得有些太复杂 ...
- oracle 字段上下两条记录的相减
SELECT T.ID ,BALANCE,nvl(lag (BALANCE,1) over (order by T.ID ) ,0) FROM AN T ORDER BY T.ID [转]orac ...
- python auto send email
/*************************************************************************** * python auto send emai ...