【bzoj2301】[HAOI2011]Problem b 莫比乌斯反演
Description
对于给出的n个询问,每次求有多少个数对(x,y),满足a≤x≤b,c≤y≤d,且gcd(x,y) = k,gcd(x,y)函数为x和y的最大公约数。
Input
第一行一个整数n,接下来n行每行五个整数,分别表示a、b、c、d、k
Output
共n行,每行一个整数表示满足要求的数对(x,y)的个数
Sample Input
2
2 5 1 5 1
1 5 1 5 2
Sample Output
14
3
HINT
100%的数据满足:1≤n≤50000,1≤a≤b≤50000,1≤c≤d≤50000,1≤k≤50000
题解
同bzoj1101
区间加减
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<cstdio> #define N 50007
using namespace std;
inline int read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if (ch=='-') f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=(x<<)+(x<<)+ch-'';ch=getchar();}
return x*f;
} int n,m,T;
int tot,pri[N],mu[N],sum[N];
bool flag[N]; void init_mu()
{
mu[]=;
for (int i=;i<=;i++)
{
if (!flag[i]) pri[++tot]=i,mu[i]=-;
for (int j=;j<=tot&&pri[j]*i<=;j++)
{
flag[pri[j]*i]=;
if (i%pri[j]==){mu[i*pri[j]]=;break;}
else mu[i*pri[j]]=-mu[i];
}
}
for (int i=;i<=;i++)
sum[i]=sum[i-]+mu[i];
}
int solve(int n,int m)
{
if (n>m) swap(n,m);
int ans=,ps;
for (int i=;i<=n;i=ps+)
{
ps=min(n/(n/i),m/(m/i));
ans+=(sum[ps]-sum[i-])*(n/i)*(m/i);
}
return ans;
}
int main()
{
init_mu();
T=read();
while(T--)
{
int a=read(),b=read(),c=read(),d=read(),k=read();
a=(a-)/k,b=b/k,c=(c-)/k,d=d/k;
printf("%d\n",solve(b,d)+solve(a,c)-solve(a,d)-solve(c,b));
}
}
【bzoj2301】[HAOI2011]Problem b 莫比乌斯反演的更多相关文章
- BZOJ2301: [HAOI2011]Problem b[莫比乌斯反演 容斥原理]【学习笔记】
2301: [HAOI2011]Problem b Time Limit: 50 Sec Memory Limit: 256 MBSubmit: 4032 Solved: 1817[Submit] ...
- BZOJ2301: [HAOI2011]Problem b 莫比乌斯反演
分析:对于给出的n个询问,每次求有多少个数对(x,y),满足a≤x≤b,c≤y≤d,且gcd(x,y) = k,gcd(x,y)函数为x和y的最大公约数. 然后对于求这样单个的gcd(x,y)=k的, ...
- [bzoj2301][HAOI2011]Problem B —— 莫比乌斯反演+容斥原理
题意 给定a, b, c, d, k,求出: \[\sum_{i=a}^b\sum_{j=c}^d[gcd(i, j) = k]\] 题解 为方便表述,我们设 \[calc(\alpha, \beta ...
- BZOJ2301:[HAOI2011]Problem b(莫比乌斯反演,容斥)
Description 对于给出的n个询问,每次求有多少个数对(x,y),满足a≤x≤b,c≤y≤d,且gcd(x,y) = k,gcd(x,y)函数为x和y的最大公约数. Input 第一行一个整数 ...
- [BZOJ1101&BZOJ2301][POI2007]Zap [HAOI2011]Problem b|莫比乌斯反演
对于给定的整数a,b和d,有多少正整数对x,y,满足x<=a,y<=b,并且gcd(x,y)=d. 我们可以令F[n]=使得n|(x,y)的数对(x,y)个数 这个很容易得到,只需要让x, ...
- P2522 [HAOI2011]Problem b (莫比乌斯反演)
题目 P2522 [HAOI2011]Problem b 解析: 具体推导过程同P3455 [POI2007]ZAP-Queries 不同的是,这个题求的是\(\sum_{i=a}^b\sum_{j= ...
- 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: 1007 Solved: 415[Submit][ ...
- BZOJ.2301.[HAOI2011]Problem B(莫比乌斯反演 容斥)
[Update] 我好像现在都看不懂我当时在写什么了=-= \(Description\) 求\(\sum_{i=a}^b\sum_{j=c}^d[(i,j)=k]\) \(Solution\) 首先 ...
- [POI2007]ZAP-Queries && [HAOI2011]Problem b 莫比乌斯反演
1,[POI2007]ZAP-Queries ---题面---题解: 首先列出式子:$$ans = \sum_{i = 1}^{n}\sum_{j = 1}^{m}[gcd(i, j) == d]$$ ...
随机推荐
- 使用Process组件访问本地进程
实现效果; 知识运用: Process组件的StartInfo属性 //获取或设置要传递给Process的Start方法的属性 public ProcessStartInfo StartInfo {g ...
- python_92_面向对象初体验
class dog: def __init__(self,name): self.name=name def bulk(self): print('%s汪汪汪!'%self.name) d1=dog( ...
- 《剑指offer》56 数组中只出现一次的数字
题目描述 一个整型数组里除了两个数字之外,其他的数字都出现了两次.请写程序找出这两个只出现一次的数字. 在线练习:https://www.nowcoder.com/practice/e02fdb5 ...
- java基础—基础语法2
一.语句
- 【转】C++后台开发应该读的书
转载自http://www.cnblogs.com/balloonwj/articles/9094905.html 作者 左雪菲 根据我的经验来谈一谈,先介绍一下我的情况,坐标上海,后台开发(也带团队 ...
- 一个4年工作经验的java程序员的困惑,怎样才能能为一个架构师,请教大神
一个4年工作经验的java程序员的困惑,怎样才能能为一个架构师 LZ本人想往架构师发展, 业余时间也会看一些书籍, 但是感觉没有头绪, 有些书看了,也没有地方实践 我做了4年的java开发, 在一个公 ...
- 私有DockerHub搭建
docker简介 一个开源的应用容器引擎,可以用来打包程序,可以包入依赖环境,这样只需要提供docker image即可,类似于虚拟机,但是更轻量级. 几个概念: Paas,platform as a ...
- 树莓派开发板入门学习笔记2:[转]树莓派系统在VM中能做什么
问"树莓派系统在VM中能做什么"不如问"树莓派能做什么":(参考:树莓派实验室) 普通难度的DIY 较高难度的DIY 用树莓派打造一个家庭影院 给树莓派安装摄像 ...
- python2与python3的bytes问题
>>> s = '编程' >>> print s 编程 >>> s '\xe7\xbc\x96\xe7\xa8\x8b' >>> ...
- LeetCode(303)Range Sum Query - Immutable
题目 Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclus ...