http://www.lydsy.com/JudgeOnline/problem.php?id=2301

题意:给a,b,c,d,k,求gcd(x,y)==k的个数(a<=x<=b,c<=y<=d)

思路:假设F(a,b)代表gcd(x,y)==k 的个数(1<=x<=a,1<=y<=b)

那么这是满足区间加减的

ans=F(b,d)-F(b,c)-F(a,d)+F(a,c)

剩下的就和Zap一样了

 #include<algorithm>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<iostream>
int mul[],p[],mark[],sum[];
int read(){
char ch=getchar();int t=,f=;
while (ch<''||ch>''){if (ch=='') f=-;ch=getchar();}
while (''<=ch&&ch<=''){t=t*+ch-'';ch=getchar();}
return t*f;
}
void init(){
mul[]=;
for (int i=;i<=;i++){
if (!mark[i]){
p[++p[]]=i;
mul[i]=-;
}
for (int j=;j<=p[]&&p[j]*i<=;j++){
mark[i*p[j]]=;
if (i%p[j]) mul[p[j]*i]=mul[i]*(-);
else{
mul[p[j]*i]=;
break;
}
}
}
sum[]=;
for (int i=;i<=;i++) sum[i]=sum[i-]+mul[i];
}
int cal(int a,int b){
if (a>b) std::swap(a,b);
int res=;
for (int i=,j;i<=a;i=j+){
j=std::min(a/(a/i),b/(b/i));
res+=(a/i)*(b/i)*(sum[j]-sum[i-]);
}
return res;
}
int main(){
int T=read();
init();
while (T--){
int a=read(),b=read(),c=read(),d=read(),k=read();
a--;c--;
printf("%d\n",std::max(,cal(b/k,d/k)+cal(a/k,c/k)-cal(b/k,c/k)-cal(a/k,d/k)));
}
}

BZOJ 2301 Problem B(莫比乌斯反演)的更多相关文章

  1. BZOJ 2301 Problem b(莫比乌斯反演+分块优化)

    Description 对于给出的n个询问,每次求有多少个数对(x,y),满足a≤x≤b,c≤y≤d,且gcd(x,y) = k,gcd(x,y)函数为x和y的最大公约数. Input 第一行一个整数 ...

  2. BZOJ 2301 Problem b (莫比乌斯反演+容斥)

    这道题和 HDU-1695不同的是,a,c不一定是1了.还是莫比乌斯的套路,加上容斥求结果. 设\(F(n,m,k)\)为满足\(gcd(i,j)=k(1\leq i\leq n,1\leq j\le ...

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

  4. [BZOJ 2301] [HAOI 2011] Problem b (莫比乌斯反演)(有证明)

    [BZOJ 2301] [HAOI 2011] Problem b (莫比乌斯反演)(有证明) 题面 T组询问,每次给出a,b,c,d,k,求\(\sum _{i=a}^b\sum _{j=c}^d[ ...

  5. Bzoj 2301: [HAOI2011]Problem b(莫比乌斯反演+除法分块)

    2301: [HAOI2011]Problem b Time Limit: 50 Sec Memory Limit: 256 MB Description 对于给出的n个询问,每次求有多少个数对(x, ...

  6. BZOJ 2301 Problem b(莫比乌斯反演+分块优化)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=37166 题意:对于给出的n个询问,每次求有多少个数对(x,y),满 ...

  7. BZOJ.2301.[HAOI2011]Problem B(莫比乌斯反演 容斥)

    [Update] 我好像现在都看不懂我当时在写什么了=-= \(Description\) 求\(\sum_{i=a}^b\sum_{j=c}^d[(i,j)=k]\) \(Solution\) 首先 ...

  8. BZOJ 2301 [HAOI2011]Problem b ——莫比乌斯反演

    分成四块进行计算,这是显而易见的.(雾) 然后考虑计算$\sum_{i=1}^n|sum_{j=1}^m gcd(i,j)=k$ 首先可以把n,m/=k,就变成统计&i<=n,j< ...

  9. BZOJ2301: [HAOI2011]Problem b[莫比乌斯反演 容斥原理]【学习笔记】

    2301: [HAOI2011]Problem b Time Limit: 50 Sec  Memory Limit: 256 MBSubmit: 4032  Solved: 1817[Submit] ...

随机推荐

  1. (转)CentOS搭建Nagios监控

    A.Nagios服务端1.安装软件包 yum install -y httpd 2.下载nagios wget http://syslab.comsenz.com/downloads/linux/na ...

  2. LeetCode_Search a 2D Matrix

    Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...

  3. MVC 自定义错误处理

    1. Application_Error namespace Libaray.Web{ public class MvcApplication : System.Web.HttpApplication ...

  4. OpenWRT 编译 error GNU libiconv not in use but included iconv.h is from...

    OpenWRT 编译 error GNU libiconv not in use but included iconv.h is from... 编译的时候碰到一个常见的错误,但是却在一个陌生的地方爆 ...

  5. IE6 max-width max-height 不起作用 解决其兼容性问题

     .catelist dl dd ul li img {width: expression(this.width > 228 ? '228px': true); max-width:228px; ...

  6. Objective-C中NSString和NSMutableString的基本用法

    int main(int argc, const char * argv[]) { @autoreleasepool { //----------------NSString------------- ...

  7. ora-28056 (Writing audit records to Windows Event Log failed)

    系统:windows xp oracle 版本 SQL> select * from v$version; BANNER ------------------------------------ ...

  8. Linux如何生成列表

    如何生成列表: 方法一:{1..100} 方法二:`seq [起始数 [步进长度]] 结束数` 1,...,100 declare -i SUM=0    integer    -x

  9. pyqt listwidget下面创建多张图片

    def Photosvisi(self): i=0 self.lists.setIconSize(QtCore.QSize(70,70))#设置显示图片大小 self.lists.setResizeM ...

  10. [跟我学Spring学习笔记][DI配置与使用]

    DI 依赖和依赖注入 定义 传统的依赖一般指"类之间的关系",那先让我们复习一下类之间的关系: 泛化:表示类与类之间的继承关系.接口与接口之间的继承关系: 实现:表示类对接口的实现 ...