2301: [HAOI2011]Problem b

Time Limit: 50 Sec  Memory Limit: 256 MB
Submit: 6015  Solved: 2741
[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

/*
* @Author: LyuC
* @Date: 2017-10-08 16:54:59
* @Last Modified by: LyuC
* @Last Modified time: 2017-10-08 21:06:44
*/
/*
直接处理会超时,对于6/6=1 6/5=1 6/4=1这样的实际和已合并同类项一次
计算出来,能节约不少时间
*/
#include <bits/stdc++.h> #define LL long long
#define MAXN 50005
using namespace std; int t;
int a, b, c, d, k;
int sum [ MAXN ];
bool check[MAXN];
int mu[MAXN];
int prime[MAXN]; void mobi(){
memset(check,false,sizeof check);
mu[]=;
int tol=;
for(int i=;i<MAXN;i++){
if(!check[i]){
prime[tol++]=i;
mu[i]=-;
}
for(int j=;j<tol;j++){
if(i*prime[j]>MAXN) break;
check[i*prime[j]]=true;
if(i%prime[j]==){
mu[i*prime[j]]=;
break;
}else{
mu[i*prime[j]]=-mu[i];
}
}
}
} inline int Count (int a, int b) {
int s=;
if (a > b) {
swap (a, b);
}
for (int i = , last = ; i <= a; i = last + ) {
last = min( a / (a / i), b / (b / i) );
s += (sum [ last ] - sum [ i - ]) * (a / i) * (b / i);
}
return s;
} inline void init () {
sum [ ] = ;
for (int i = ;i < MAXN; i ++) {
sum [ i ] = sum[ i - ] + mu [ i ];
}
} int main () {
// freopen ("in.txt", "r", stdin);
mobi ();
init ();
scanf ("%d", &t);
while (t -- ) {
scanf ("%d%d%d%d%d", &a, &b, &c, &d, &k);
int res = Count ( b / k, d / k ) - Count ( ( a - ) / k, d / k ) - Count ( b / k, (c - ) / k ) + Count ( ( a - ) / k, ( c - ) / k );
printf ( "%d\n", res );
}
return ;
}

2301: [HAOI2011]Problem b ( 分块+莫比乌斯反演+容斥)的更多相关文章

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

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

  2. 洛谷P2522 [HAOI2011]Problem b (莫比乌斯反演+容斥)

    题意:求$\sum_{i=a}^{b}\sum_{j=c}^{d}[gcd(i,j)==k]$(1<=a,b,c,d,k<=50000). 是洛谷P3455 [POI2007]ZAP-Qu ...

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

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

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

    题意:对于给出的n个询问,每次求有多少个数对(x,y),满足a≤x≤b,c≤y≤d,且gcd(x,y) = k,gcd(x,y)函数为x和y的最大公约数. 1≤n≤50000,1≤a≤b≤50000, ...

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

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

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

  7. bzoj2301 [HAOI2011]Problem b【莫比乌斯反演 分块】

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=2301 很好的一道题.首先把每个询问转化为4个子询问,最后的结果就是这四个子询问的记过加加减减 ...

  8. 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 ...

  9. 洛谷 P2522 [HAOI2011]Problem b (莫比乌斯反演+简单容斥)

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

随机推荐

  1. XtraGrid滚轮翻页

    滚轮翻页与传动的翻页更为方便,经过本人一番探讨与琢磨终于在XtraGrid的GridView中实现了鼠标滚轮翻页. 我新建了一个组件继承原本的GridControl,在组件中添加了一个ImageLis ...

  2. 支持向量机SVM(Support Vector Machine)

    支持向量机(Support Vector Machine)是一种监督式的机器学习方法(supervised machine learning),一般用于二类问题(binary classificati ...

  3. JavaScript遍历对象-总结一

    原生JavaScript 遍历 1.for 循环遍历 let array1 = ['a','b','c']; for (let i = 0;i < array1.length;i++){ con ...

  4. Spring配置中<context:annotation-config> VS <context:component-scan>

    Spring 中在使用注解(Annotation)会涉及到< context:annotation-config> 和 < context:component-scan>配置, ...

  5. 变量的声明和定义以及extern的用法

    变量的声明和定义以及extern的用法                                          变量的声明不同于变量的定义,这一点往往容易让人混淆. l         变量 ...

  6. Tomcat的四种基于HTTP协议的Connector性能比较

    Tomcat从5.5版本开始,支持以下四种Connector的配置分别为: <Connector port="8081" protocol="org.apache. ...

  7. 分享ES6中比较常用又强大的新特性

    前言 es6有很多新东西,但是感觉常用的并不是很多,这里学习记录了一些我自己认为非常常用又强大的新特性. scoping 实用的块级作用域,let x = xxx 可以声明一个块级作用域的局部变量,简 ...

  8. 17.tslib安装以及使用

    1.先在网上下载 tslib-1.4.tar.gz压缩包 2.然后在ubuntu编译: tar xzf tslib-1.4.tar.gz cd tslib ./autogen.sh mkdir tmp ...

  9. clipboard.js 介绍

    这是著名开源项目 clipboard.js 的 README.md,我把它翻译成中文.发出来,方便自己和他人阅读. 项目地址:https://github.com/zenorocha/clipboar ...

  10. MySQL之常用函数

    MySQL有如下常用函数需要掌握: 1.数学类函数 函数名称 作用 ABS(x)   返回x的绝对值                      SQRT(x)   返回x的非负二次方根 MOD(x,Y ...