和POI某道题是一样的...  http://www.cnblogs.com/JSZX11556/p/4686674.html

只需要二维差分一下就行了. 时间复杂度O(MAXN + N^1.5)

------------------------------------------------------------------------------

#include<cstdio>
#include<algorithm>
#include<cstring>
 
using namespace std;
 
const int maxn = 50009;
 
bool F[maxn];
int mu[maxn], p[maxn], pn, D;
 
void Init() {
memset(F, 0, sizeof F);
pn = 0;
for(int i = 2; i < maxn; i++) {
if(!F[i]) {
p[pn++] = i;
mu[i] = -1;
}
for(int j = 0; j < pn && i * p[j] < maxn; j++) {
F[i * p[j]] = true;
if(i % p[j] == 0) {
mu[i * p[j]] = 0;
break;
} else
mu[i * p[j]] = -mu[i];
}
}
mu[0] = 0;
mu[1] = 1;
for(int i = 1; i < maxn; i++)
mu[i] += mu[i - 1];
}
 
int f(int x, int y) {
if((y /= D) > (x /= D)) swap(x, y);
int ret = 0;
for(int L = 1, R; L <= y; L = R + 1) {
R = min(x / (x / L), y / (y / L));
ret += (mu[R] - mu[L - 1]) * (x / L) * (y / L);
}
return ret;
}
 
void Work() {
int T;
scanf("%d", &T);
while(T--) {
int a, b, c, d;
scanf("%d%d%d%d%d", &a, &b, &c, &d, &D);
printf("%d\n", f(b, d) + f(a - 1, c - 1) - f(a - 1, d) - f(b, c - 1));
}
}
 
int main() {
Init();
Work();
return 0;
}

------------------------------------------------------------------------------

2301: [HAOI2011]Problem b

Time Limit: 50 Sec  Memory Limit: 256 MB
Submit: 2475  Solved: 1054
[Submit][Status][Discuss]

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

Source

BZOJ 2301: [HAOI2011]Problem b( 数论 )的更多相关文章

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

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

  2. bzoj 2301: [HAOI2011]Problem b

    2301: [HAOI2011]Problem b Time Limit: 50 Sec Memory Limit: 256 MB Submit: 3757 Solved: 1671 [Submit] ...

  3. BZOJ 2301: [HAOI2011]Problem b 莫比乌斯反演

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

  4. BZOJ 2301 [HAOI2011]Problem b (分块 + 莫比乌斯反演)

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

  5. BZOJ 2301: [HAOI2011]Problem b (莫比乌斯反演)

    2301: [HAOI2011]Problem b Time Limit: 50 Sec  Memory Limit: 256 MBSubmit: 436  Solved: 187[Submit][S ...

  6. 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( ...

  7. 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 第一行一个整数 ...

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

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

  9. 【刷题】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 第一行一个整数 ...

随机推荐

  1. ORA-02287: 此处不同意序号

    ORA-02287: 此处不同意序号 insert into gls_vchitem     (viid,      yr,      km)     select gls_vchitem_seq.n ...

  2. C#编程建言笔记

    方法: 1.方法(静态或实例)JIT编译后,在内存中的代码段上都是一个全局函数,且只存在一份拷贝. 2.方法修饰符:保护级别,静态,虚函数:方法签名:返回值,函数名,参数. 构造器: 1.一个方法只能 ...

  3. jQuery源码笔记——三

    将类数组对象转化为数组对象 javascript中有许多类数组对象,比如HTMLCollection,NodeList,arguments.她们的特点是和数组一样有length属性,并且有0,1,2这 ...

  4. some knowledge t

    NSNumber static 看下面例子  gCount可以在Person 文件中使用  在main 中不行 @property()括号中可以填的属性 国际化 OC中的快捷键操作 operation ...

  5. QF——iOS代理模式

    iOS的代理模式: A要完成某个功能,它可以自己完成,但有时出于一些原因,不方便自己完成.这时A可以委托B来帮其完成此功能,即由B代理完成.但是这个功能不是让B随随便便任其完成.此时,会有一个协议文件 ...

  6. wdcp升级php和mysql

    安装没什么好说的,按照wdcp官方去搞就行了,这里如果是centos系统建议使用rpm安装方式附件如下(这里包含了本文后面用到的三个文件) http://download.csdn.net/detai ...

  7. 精读《javascript高级程序设计》笔记一——基本概念

    语法 严格模式 启用严格模式,在脚本顶部或函数内部上方添加"use strict";语句. 数据类型 typeof typeof返回undifined,boolean,number ...

  8. C#文本文件或其他文件新内容追加

    以txt文本为例,以下代码实现a.txt文件中追加内容 FileStream mystream = new FileStream("C:\\a.txt", FileMode.Ope ...

  9. Prepare Python environment and install selenium.

    1, Install python and selenium. I use python 3.5, the following is the example 1.)    Python downloa ...

  10. 使用C#开发Metro 风格应用的路线图 -- 触屏操作

    原文 http://www.cnblogs.com/icuit/archive/2012/05/01/2478312.html win8支持多点触摸技术,而我们在屏幕上所做的各种操作,也最终转换为输入 ...