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. Mybatis 通过扫描 自动生成别名

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" ...

  2. Hibernate4.2.21.Final创建入门的HelloHibernet工程

    1.在hibernate官网下载hibernate-release-4.2.21.Final.zip并解压 2.新建一个java project工程(HelloHibernet)(myeclipes) ...

  3. 【转帖】Sigma水平和缺陷率的对应关系:正态分布中心和1.5标准差偏移

    http://www.pinzhi.org/thread-5395-1-1.html Sigma水平和缺陷率的对应关系:正态分布中心和有1.5个标准差偏移 在过程稳定时,若给出了规范限,过程的平均与标 ...

  4. 让 sphinx 支持中文、日文和韩文

    在国内搜索 sphinx 的话找到的资源好像都是挺久远的,无奈之下只好跑到国外去找了.听起来有点不可思议,但是最近整 sphinx 的时候突然想到 mediawiki 官方有 sphinx 的安装介绍 ...

  5. C#指南,重温基础,展望远方!(12)C#特性

    C# 程序中的类型.成员和其他实体支持使用修饰符来控制其行为的某些方面. 例如,方法的可访问性是由 public.protected.internal 和 private 修饰符控制. C# 整合了这 ...

  6. 温故而知新 $ jquery选择器居然隐藏第二个参数,更进一步限制选择的区域

    $ 选择器的第二个参数 $("[name=" + name + "]", layero); layero 其实也是一个dom对象,譬如一个表单,一个table. ...

  7. 利用国内的源安装 Python第三方库

    我们需要安装一些Python的第三方库,但是使用  pip install + 第三方库  的时候,会出现下载速度慢的问题,当然我们也可以使用国内的源安装. 例如: sudo pip install ...

  8. AutoFac文档3(转载)

    目录 开始 Registering components 控制范围和生命周期 用模块结构化Autofac xml配置 与.net集成 深入理解Autofac 指导 关于 词汇表 服务类型,名称和键 同 ...

  9. tbnet编译

    下载tbnet 下载地址:http://code.taobao.org/p/tb-common-utils/src/trunk/tbnet/ ,它的svn地址为:http://code.taobao. ...

  10. 以byte方式讀取SPD的數據(SMBus Controller在PCH中)

    Reading A Byte of SMBus EEPROM DataThe following steps have to be followed for the System BIOS to re ...