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

和这题不是差不多的嘛~~【BZOJ】1101: [POI2007]Zap(莫比乌斯+分块)

唯一不同的地方是这题有下界。。

下界除以k的时候取上界,然后分块的时候因为有4个数,所以要分成4块来搞。。

然后就行了。。

#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
using namespace std;
typedef long long ll;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << (#x) << " = " << (x) << endl
#define error(x) (!(x)?puts("error"):0)
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
#define rdm(x, i) for(int i=ihead[x]; i; i=e[i].next) const int N=50005;
int p[N], np[N], cnt, mu[N];
ll sum[N];
void init() {
mu[1]=1;
for2(i, 2, N) {
if(!np[i]) p[++cnt]=i, mu[i]=-1;
for1(j, 1, cnt) {
int t=p[j]*i; if(t>=N) break;
np[t]=1;
if(i%p[j]==0) { mu[t]=0; break; }
mu[t]=-mu[i];
}
}
for2(i, 1, N) sum[i]=sum[i-1]+mu[i];
}
inline ll cal(int a, int b, int c, int d, int k) {
return max(0ll, (ll)((b/k)-((a-1)/k))*(ll)((d/k)-((c-1)/k)));
} int main() {
init();
int n=getint();
while(n--) {
int a=getint(), b=getint(), c=getint(), d=getint(), k=getint();
a=(a+k-1)/k; b/=k; c=(c+k-1)/k; d/=k;
//dbg(a); dbg(b); dbg(c); dbg(d);
int len=min(b, d);
ll ans=0;
int pos;
for(int i=1; i<=len; i=pos+1) {
pos=min(b/(b/i), d/(d/i));
if((a-1)/i!=0) pos=min(pos, (a-1)/((a-1)/i));
if((c-1)/i!=0) pos=min(pos, (c-1)/((c-1)/i));
ans+=(sum[pos]-sum[i-1])*cal(a, b, c, d, i);
}
printf("%lld\n", ans);
}
return 0;
}

  


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 MBSubmit: 1007  Solved: 415[Submit][ ...

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

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

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

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

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

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

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

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

  7. bzoj 2301: [HAOI2011]Problem b

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

  8. BZOJ 2301: [HAOI2011]Problem b( 数论 )

    和POI某道题是一样的...  http://www.cnblogs.com/JSZX11556/p/4686674.html 只需要二维差分一下就行了. 时间复杂度O(MAXN + N^1.5) - ...

  9. 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, ...

  10. 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. 算法笔记_157:算法提高 c++_ch02_01(Java)

    目录 1 问题描述 2 解决方案   1 问题描述 编写一个程序,利用强制类型转换打印元音字母大小写10种形式的ASCII码. 输出的顺序为:大写的字母A,E,I,O,U的ASCII码,小写的字母a, ...

  2. ES6 import 循环加载

    1.示例 (1)a.js import {bar} from './b'; console.log('a.mjs'); console.log(bar); export let foo = 'foo' ...

  3. Commons FileUpLoad 两种上传方式解

    traditional API (传统方式) //上传路径 File file = new File("C:/upload"); //临时文件路径 File tempFile = ...

  4. Bootstrap 的模态框类

    事件类型 描述 show.bs.modal show 方法调用之后立即触发该事件.如果是通过点击某个作为触发器的元素,则此元素可以通过事件的relatedTarget 属性进行访问. shown.bs ...

  5. 转MQTT压力测试之Tsung的使用

    转自:http://www.cnblogs.com/lingyejun/p/7941271.html nTsung测试工具的基本测试命令为 Tsung -f  ~/.tsung/mqtt.xml -l ...

  6. Spring4.* 中整合 Hibernate

    1. Spring 整合 Hibernate 整合什么 ? 1). 有 IOC 容器来管理 Hibernate 的 SessionFactory2). 让 Hibernate 使用上 Spring 的 ...

  7. 解决C# WINFORM程序只允许运行一个实例的几种方法详解

    要实现程序的互斥,通常有下面几种方式,下面用 C# 语言来实现: 方法一: 使用线程互斥变量. 通过定义互斥变量来判断是否已运行实例. 把program.cs文件里的Main()函数改为如下代码: u ...

  8. Chrome 浏览器端口的坑:ERR_UNSAFE_PORT

    nodejs 启动了一个6000端口的服务 本来是打算测试用的,结果一直报以下错误 但我使用 curl 来请求该接口地址是正常的.这就很纳闷了. 经过一番折腾无果之后,百度才知道.这个6000端口是非 ...

  9. [CentOS] CentOS for vsftpd with MySQL Virtual user

    從ubuntu 12.04的安裝手法拿到CentOS來真的有些很大的不同 絕大部分的語法.概念都是差不多的,只是指令上有些差別,跟ubuntu 有不一樣的地方特別拿出來另外說明 要讓vsftpd與my ...

  10. C函数返回字符串

    #include "stdio.h" #define KEY 5; int main() { char password[50] = "123456"; enc ...