bzoj 2301 [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 5 1 5 1
1 5 1 5 2
Sample Output
3
HINT
100%的数据满足:1≤n≤50000,1≤a≤b≤50000,1≤c≤d≤50000,1≤k≤50000
【思路】

【代码】
#include<cstdio>
#include<iostream>
using namespace std; typedef long long ll;
const int N = ; int n;
ll su[N],sz,np[N],mu[N]; void get_mu()
{
int i,j;
mu[]=;
for(int i=;i<N;i++) {
if(!np[i]) {
su[++sz]=i;
mu[i]=-;
}
for(int j=;j<=sz&&i*su[j]<N;j++) {
np[su[j]*i]=;
if(i%su[j]==) mu[i*su[j]]=;
else mu[i*su[j]]=-mu[i];
}
}
for(int i=;i<N;i++)
mu[i]+=mu[i-];
}
ll C(int m,int n,int k)
{
int i,last; ll res=;
n/=k,m/=k;
for(i=;i<=min(n,m);i=last+) {
last=min(n/(n/i),m/(m/i));
res+=(mu[last]-mu[i-])*(m/i)*(n/i);
}
return res;
} int main()
{
get_mu();
int T,a,b,c,d,k;
scanf("%d",&T);
while(T--) {
scanf("%d%d%d%d%d",&a,&b,&c,&d,&k);
printf("%d\n",C(b,d,k)-C(a-,d,k)-C(b,c-,k)+C(a-,c-,k));
}
return ;
}
bzoj 2301 [HAOI2011]Problem b(莫比乌斯反演)的更多相关文章
- 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\) 首先 ...
- BZOJ 2301 [HAOI2011]Problem b ——莫比乌斯反演
分成四块进行计算,这是显而易见的.(雾) 然后考虑计算$\sum_{i=1}^n|sum_{j=1}^m gcd(i,j)=k$ 首先可以把n,m/=k,就变成统计&i<=n,j< ...
- bzoj 2301: [HAOI2011]Problem b mobius反演 RE
http://www.lydsy.com/JudgeOnline/problem.php?id=2301 设f(i)为在区间[1, n]和区间[1, m]中,gcd(x, y) = i的个数. 设F( ...
- BZOJ 2301 [HAOI2011]Problem b (分块 + 莫比乌斯反演)
2301: [HAOI2011]Problem b Time Limit: 50 Sec Memory Limit: 256 MBSubmit: 6519 Solved: 3026[Submit] ...
- BZOJ 2301: [HAOI2011]Problem b (莫比乌斯反演)
2301: [HAOI2011]Problem b Time Limit: 50 Sec Memory Limit: 256 MBSubmit: 436 Solved: 187[Submit][S ...
- BZOJ2301: [HAOI2011]Problem b[莫比乌斯反演 容斥原理]【学习笔记】
2301: [HAOI2011]Problem b Time Limit: 50 Sec Memory Limit: 256 MBSubmit: 4032 Solved: 1817[Submit] ...
- bzoj 2301: [HAOI2011]Problem b
2301: [HAOI2011]Problem b Time Limit: 50 Sec Memory Limit: 256 MB Submit: 3757 Solved: 1671 [Submit] ...
- BZOJ 2301: [HAOI2011]Problem b( 数论 )
和POI某道题是一样的... http://www.cnblogs.com/JSZX11556/p/4686674.html 只需要二维差分一下就行了. 时间复杂度O(MAXN + N^1.5) - ...
随机推荐
- asp.net web api 开发时应当注意的事项
Self referencing when returning chain of objects. This can be solved using a design pattern called t ...
- mac下安装应用及常用快捷键
从网络上下载的应用程序如何安装? 主要分类为两种:(dmg 和 pkg) 1.dmg类型 此类应用程序安装非常简单,只需要双击图标,然后将此应用程序图标直接拖拽到 application图标上即可 ...
- PAT-乙级-1026. 程序运行时间(15)
1026. 程序运行时间(15) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 要获得一个C语言程序的运行时间, ...
- Akka Stream文档翻译:Motivation
动机 Motivation The way we consume services from the internet today includes many instances of streami ...
- Python性能鸡汤
http://pythoner.org/wiki/257/ 毫无疑问:Python程序没有编译型语言高效快速. 甚至Python拥护者们会告诉你Python不适合这些领域. 然而,YouTube已用P ...
- 【leetcode】Longest Substring Without Repeating Characters (middle)
Given a string, find the length of the longest substring without repeating characters. For example, ...
- Codeigniter开发技巧:连接多个数据库(可实现DB读写分离)
在开发中,我们有时候会遇到在同一程序中链接多个数据库的需求,这对Codeigniter框架来说是很简单的,我们只需要在 database.php文件中配置少许参数即可. 默认情况下,CI配置的是链接一 ...
- hdu 3544 Alice's Game 博弈论
博弈论+二分! 后一人会尽量选前一人切小的一块切!! 代码如下: #include<iostream> #include<stdio.h> #define I1(x) scan ...
- 【前端学习】【jQuery选择器】
jQuery选择器 jQuery选择器 本文内容引自于单东林<锋利的jQuery>,未经原作者准许,禁止以商业目的转载发布! 选择器是jQuery的根基,在jQuery中,对事件处 ...
- 全选与反选(dom与jquery比较)
<html> <head> <title>全选或反选(dom)</title> <meta http-equiv="Content-Ty ...