题目链接: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反演,分块的更多相关文章

  1. [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)有多少对. ...

  2. Bzoj 2820: YY的GCD(莫比乌斯反演+除法分块)

    2820: YY的GCD Time Limit: 10 Sec Memory Limit: 512 MB Description 神犇YY虐完数论后给傻×kAc出了一题给定N, M,求1<=x& ...

  3. BZOJ 2820: YY的GCD [莫比乌斯反演]【学习笔记】

    2820: YY的GCD Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 1624  Solved: 853[Submit][Status][Discu ...

  4. bzoj 2820 YY的GCD 莫比乌斯反演

    题目大意: 给定N, M,求1<=x<=N, 1<=y<=M且gcd(x, y)为质数的(x, y)有多少对 这里就抄一下别人的推断过程了 后面这个g(x) 算的方法就是在线性 ...

  5. bzoj 2820 YY的GCD - 莫比乌斯反演 - 线性筛

    Description 神犇YY虐完数论后给傻×kAc出了一题给定N, M,求1<=x<=N, 1<=y<=M且gcd(x, y)为质数的(x, y)有多少对kAc这种 傻×必 ...

  6. BZOJ 2820: YY的GCD 莫比乌斯反演_数学推导_线性筛

    Code: #include <cstdio> #include <algorithm> #include <cstring> #include <vecto ...

  7. BZOJ 2820 YY的GCD ——莫比乌斯反演

    我们可以枚举每一个质数,那么答案就是 $\sum_{p}\sum_{d<=n}\mu(d)*\lfloor n / pd \rfloor *\lfloor m / pd \rfloor$ 直接做 ...

  8. 【莫比乌斯反演】关于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( ...

  9. 【刷题】BZOJ 2820 YY的GCD

    Description 神犇YY虐完数论后给傻×kAc出了一题给定N, M,求1<=x<=N, 1<=y<=M且gcd(x, y)为质数的(x, y)有多少对kAc这种傻×必然 ...

随机推荐

  1. 跨平台Unicode与UTF8互转代码

    参考来源:http://blog.csdn.net/flying8127/article/details/1598521 在原来原基础上,将代码整理,并加强安全性. 并按照WindowsAPI设计, ...

  2. Head First 设计模式笔记:单例模式

    单例模式 确保一个类只有一个实例,并提供一个全局访问点. 类图: Singleton static uniqueInstance //其他属性... static getInstance() //其他 ...

  3. live555源码研究(四)------UserAuthenticationDatabase类

    一.UserAuthenticationDatabase类作用 1,用户/密码管理 2,鉴权管理 二.类UserAuthenticationDatabase继承关系图                 ...

  4. 【Oracle连接字符串】【Oracle Net Manager 服务命名配置】【PL/SQL 登陆数据库】

    连接数据库的几个重要参数: 1. 登陆用户名:user: 2. 登录密码:password: 3. 存放数据库的服务器地址(server_ip)和端口(server_port): 4. 数据库名(db ...

  5. 200. Number of Islands

    题目: Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is s ...

  6. Linux 下shell显示-bash-4.1$不显示用户名路径的解决方法

    Linux CentOS下shell显示-bash-4.1$不显示用户名路径的解决方法   问题描述:   CentOS下新增一个用户,登录进去之后shell脚本的信息如下:     而不是我们经常看 ...

  7. CruiseControl.net

    CruiseControl.net 使用CruiseControl.NET进行自动化构建总结 http://blog.csdn.net/chenbin520/article/details/10112 ...

  8. poj 2503 Babelfish (查找 map)

    题目:http://poj.org/problem?id=2503 不知道为什么 poj  的 数据好像不是100000,跟周赛的不一样 2000MS的代码: #include <iostrea ...

  9. RazorEngine(未解决,留底)

    TemplateServiceConfiguration templateConfig = new TemplateServiceConfiguration { BaseTemplateType = ...

  10. Repeater的ItemCommand事件(LinkButton)

    Repeater的ItemCommand事件,就是在里面加一个超链接的按钮,所有按钮都指向同一个事件,就是ItemCommand事件. 至于如何区分是点击的什么按钮,还有传的值,需要用到LinkBut ...