【BZOJ】2301: [HAOI2011]Problem b(莫比乌斯+分块)
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 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
Source
【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 (分块 + 莫比乌斯反演)
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 ...
- 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) - ...
- 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, ...
- 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 第一行一个整数 ...
随机推荐
- NGUI所见即所得之深入剖析UIPanel,UIWidget,UIDrawCall底层原理
NGUI所见即所得之深入剖析UIPanel,UIWidget,UIDrawCall底层原理 By D.S.Qiu 尊重他人的劳动,支持原创,转载请注明出处:http.dsqiu.iteye.com 之 ...
- 【bat】通过for循环复制一张图片100次
代码如下: @echo off set "F=1" cd C:\Users\Thinkpad\Desktop\img for /L %%F IN (1,1,100) DO COPY ...
- java面试题(开发框架)
博客分类: java基础 面试Java多线程编程设计模式 java基础面试题目,以备不时之需 俗话说 细节决定成败. 就算很简单,很小的问题,我们还是要注意一下的. ...
- 用ping让对方电脑堵塞瘫痪
用ping让对方电脑堵塞瘫痪2008-04-27 11:32 定义echo数据包大小. 在默认的情况下windows的ping发送的数据包大小为32byt,我们也可以自己定义它的大小, 但有一个大小的 ...
- Windows7清除图标缓存
以下是批处理文件代码: rem 关闭Windows外壳程序explorer taskkill /f /im explorer.exe rem 清理系统图标缓存数据库 attrib -h -s -r & ...
- Spark SQL and DataFrame Guide(1.4.1)——之DataFrames
Spark SQL是处理结构化数据的Spark模块.它提供了DataFrames这样的编程抽象.同一时候也能够作为分布式SQL查询引擎使用. DataFrames DataFrame是一个带有列名的分 ...
- php 检查该数组有重复值
if (count($array) != count(array_unique($array))) { echo '该数组有重复值'; }
- spring in action小结2
1 @Component注解声明的类都会创建为bean,并且bean的id为首字母小写的类名. 2 解决bean自动装配奇异性问题,可以使用@Qualifier("name")限定 ...
- Atitit.软件仪表盘(4)--db数据库子系统-监测
Atitit.软件仪表盘(4)--db数据库子系统-监测 连接数::: 死锁表列表:死锁基础列表(最近几条记录,时间,sql等) 3.对服务器进行监控,获取CUP,I/O使用情况 4.对数据库进行 ...
- CHero
#ifndef __HERO_H__ #define __HERO_H__ #include "GameFrameHead.h" #include "GameParam. ...