题目链接: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. uva 10369

    数组开小了  还RE了一遍.......  最小生成树    按费用从小到大排... #include <iostream> #include <algorithm> #inc ...

  2. solr教程,值得刚接触搜索开发人员一看

    http://blog.csdn.net/awj3584/article/details/16963525 Solr调研总结 开发类型 全文检索相关开发 Solr版本 4.2 文件内容 本文介绍sol ...

  3. POJ2526+简单几何

    题意:给定的这些点是否有一个对称中心. PS:我写得有点啰嗦.. 就是把小的x和大的x进行匹配. #include<stdio.h> #include<algorithm> # ...

  4. http://www.ibm.com/developerworks/cn/java/j-lo-hotswapcls/

    http://www.ibm.com/developerworks/cn/java/j-lo-hotswapcls/

  5. seek和tell的用法--获取文件内容大小(字节)

    /*获取文件中存取的数据内容的大小(字节数) ellg() 和 tellp() 这两个成员函数不用传入参数,返回pos_type 类型的值(根据ANSI-C++ 标准) ,就是一个整数,代表当前get ...

  6. [原]数据库中的partitioning和sharding

    1. 如何理解定义 在中文中,partitioning和sharding都有分区的意思.从大的方面来说,这两个词所执行的动作确实也和分区相关.partitioning在很多场合是vertical pa ...

  7. DSP6455 DSP/BIOS中断配置问题(是否需要ECM-事件组合以及实例)

    2013-06-20 21:08:48 中断的配置有两种常用的方式: 一是通过CSL提供的API进行配置,这种方法相对DSP/BIOS偏底层,也比较麻烦:这种方法要求对中断系统的工作方式很清楚. 二是 ...

  8. poj 1459 Power Network(增广路)

    题目:http://poj.org/problem?id=1459 题意:有一些发电站,消耗用户和中间线路,求最大流.. 加一个源点,再加一个汇点.. 其实,过程还是不大理解.. #include & ...

  9. poj 1753 Flip Game 枚举(bfs+状态压缩)

    题目:http://poj.org/problem?id=1753 因为粗心错了好多次……,尤其是把1<<15当成了65535: 参考博客:http://www.cnblogs.com/k ...

  10. JavaScript NodeList和Array

    原文引用脚本之家作者:Jeff Wong,谢谢大神提供资源 在Web前端编程中,我们通常会通过document.getElementsByTagName或者document.getElementsBy ...