Problem b

题目描述

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

输入

第一行一个整数n,接下来n行每行五个整数,分别表示a、b、c、d、k

输出

共n行,每行一个整数表示满足要求的数对(x,y)的个数

100%的数据满足:1≤n≤50000,1≤a≤b≤50000,1≤c≤d≤50000,1≤k≤50000

来源

HAOI2011


solution

首先用类似前缀和的方法

题目求f(b,d)-f(a-1,d)-f(c-1,b)+f(a-1,c-1)

这样子就好求了不少

按照套路反演

那么我们算同一个询问的效率就是O(n)的了

这时发现询问数很多

注意到在一定范围内n/kd,m/kd是相同的,可以一起算

范围是多少呢

假设当前为i,nex=n/(n/i)

很好理解,我要找的是连续最长的一段,都等于n/i

n/(n/i)就是最大的那一个,也就是右端

#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#define maxn 50008
#define ll long long
using namespace std;
int T,n,a,b,c,d,k,mu[maxn];
int pri[maxn],flag[maxn],tot;
void init(){
n=50000;
mu[1]=1;
for(int i=2;i<=n;i++){
if(!flag[i])mu[i]=-1,pri[++tot]=i;
for(int j=1;j<=tot&&pri[j]<=n/i;j++){
flag[i*pri[j]]=1;mu[i*pri[j]]=-mu[i];
if(i%pri[j]==0){
mu[pri[j]*i]=0;break;
}
}
mu[i]+=mu[i-1];
}
}
ll C(int x,int y){
ll ans=0;
if(x<y)swap(x,y);
for(int i=1;i<=y;){
int l=i,r=min(x/(x/i),y/(y/i));
ans=ans+(1LL)*(mu[r]-mu[l-1])*(x/i)*(y/i);
i=r+1;
}
return ans;
}
void work(){
scanf("%d%d%d%d%d",&a,&b,&c,&d,&k);
ll tmp=C(b/k,d/k)-C(d/k,(a-1)/k)-C(b/k,(c-1)/k)+C((a-1)/k,(c-1)/k);
printf("%lld\n",tmp);
}
int main(){
init();
cin>>T;while(T--)work();
return 0;
}

Problem b的更多相关文章

  1. 1199 Problem B: 大小关系

    求有限集传递闭包的 Floyd Warshall 算法(矩阵实现) 其实就三重循环.zzuoj 1199 题 链接 http://acm.zzu.edu.cn:8000/problem.php?id= ...

  2. No-args constructor for class X does not exist. Register an InstanceCreator with Gson for this type to fix this problem.

    Gson解析JSON字符串时出现了下面的错误: No-args constructor for class X does not exist. Register an InstanceCreator ...

  3. C - NP-Hard Problem(二分图判定-染色法)

    C - NP-Hard Problem Crawling in process... Crawling failed Time Limit:2000MS     Memory Limit:262144 ...

  4. Time Consume Problem

    I joined the NodeJS online Course three weeks ago, but now I'm late about 2 weeks. I pay the codesch ...

  5. Programming Contest Problem Types

        Programming Contest Problem Types Hal Burch conducted an analysis over spring break of 1999 and ...

  6. hdu1032 Train Problem II (卡特兰数)

    题意: 给你一个数n,表示有n辆火车,编号从1到n,入站,问你有多少种出站的可能.    (题于文末) 知识点: ps:百度百科的卡特兰数讲的不错,注意看其参考的博客. 卡特兰数(Catalan):前 ...

  7. BZOJ2301: [HAOI2011]Problem b[莫比乌斯反演 容斥原理]【学习笔记】

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

  8. [LeetCode] Water and Jug Problem 水罐问题

    You are given two jugs with capacities x and y litres. There is an infinite amount of water supply a ...

  9. [LeetCode] The Skyline Problem 天际线问题

    A city's skyline is the outer contour of the silhouette formed by all the buildings in that city whe ...

  10. PHP curl报错“Problem (2) in the Chunked-Encoded data”解决方案

    $s = curl_init(); curl_setopt($s, CURLOPT_POST, true); curl_setopt($s, CURLOPT_POSTFIELDS, $queryStr ...

随机推荐

  1. 【赛时总结】◇赛时·VII◇ Atcoder ABC-106

    [赛时·VII] ABC-106 一条比赛时莫名其妙发了半个小时呆的菜鸡&咸鱼得到了自己应有的下场……279th. Rating:1103(+) 终于AK,一次通过…… ◇ 简单总结 ABC还 ...

  2. 一张思维导图带你梳理HashMap相关知识

    HashMap可以说是java中最常见也是最重要的key-value存储结构类,很多程序员可能经常用,但是不一定清楚这个类背后的数据结构和相关操作原理,为了复习HashMap相关的知识,今天花了一天的 ...

  3. mac安装mysql及workbench

    安装mysql 登录MySQL网站 打开网站Download MySQL Community Server,选择下方的dmg文件下载 点击download,此处为8.0.11版本 然后选择no tha ...

  4. (一)、Python的简介与安装

    Python简介 Python的创始人为Guido van Rossum.1989年圣诞节期间,在阿姆斯特丹,Guido为了打发圣诞节的无趣,决心开发一个新的脚本解释程序,作为ABC 语言的一种继承. ...

  5. 神经网络系列学习笔记(一)——神经网络之ANN学习资料汇总

    ANN tutorial: http://adventuresinmachinelearning.com/neural-networks-tutorial/ https://www.cs.toront ...

  6. 笔记-python-functool-@wraps

    笔记-python-functool-@wraps 1.      wraps 经常看到@wraps装饰器,查阅文档学习一下 在了解它之前,先了解一下partial和updata_wrapper这两个 ...

  7. Android 使用Retrofit2.0+OkHttp3.0实现缓存处理+Cookie持久化第三方库

    1.Retrofit+OkHttp的缓存机制 1.1.第一点 在响应请求之后在 data/data/<包名>/cache 下建立一个response 文件夹,保存缓存数据. 1.2.第二点 ...

  8. Android log 引发的血案

    今天调试代码,我打印了一个东西: Log.d("WelcomeActivity", res.str); 结果总是代码执行不到这一行的下一行,程序也没有挂掉.后来,我自己去想各种可能 ...

  9. java.math.BigDecimal cannot be cast to java.lang.String解决方法

    从mysql数据库里取decimal(18,2)封装到Map<String,String>中 BigDecimal b = new BigDecimal(resultMap.get(&qu ...

  10. 关于IOS下日期格式分隔符 - 、 /的问题

    之前我们项目有一个低价日历,服务端下发的时间格式为: "2014-07-21 09:45:12"  然后一直出不了数据,后来发现. IOS下无论chrome.safari还是Uc如 ...