Problem b
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
来源
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的更多相关文章
- 1199 Problem B: 大小关系
求有限集传递闭包的 Floyd Warshall 算法(矩阵实现) 其实就三重循环.zzuoj 1199 题 链接 http://acm.zzu.edu.cn:8000/problem.php?id= ...
- 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 ...
- C - NP-Hard Problem(二分图判定-染色法)
C - NP-Hard Problem Crawling in process... Crawling failed Time Limit:2000MS Memory Limit:262144 ...
- Time Consume Problem
I joined the NodeJS online Course three weeks ago, but now I'm late about 2 weeks. I pay the codesch ...
- Programming Contest Problem Types
Programming Contest Problem Types Hal Burch conducted an analysis over spring break of 1999 and ...
- hdu1032 Train Problem II (卡特兰数)
题意: 给你一个数n,表示有n辆火车,编号从1到n,入站,问你有多少种出站的可能. (题于文末) 知识点: ps:百度百科的卡特兰数讲的不错,注意看其参考的博客. 卡特兰数(Catalan):前 ...
- BZOJ2301: [HAOI2011]Problem b[莫比乌斯反演 容斥原理]【学习笔记】
2301: [HAOI2011]Problem b Time Limit: 50 Sec Memory Limit: 256 MBSubmit: 4032 Solved: 1817[Submit] ...
- [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 ...
- [LeetCode] The Skyline Problem 天际线问题
A city's skyline is the outer contour of the silhouette formed by all the buildings in that city whe ...
- PHP curl报错“Problem (2) in the Chunked-Encoded data”解决方案
$s = curl_init(); curl_setopt($s, CURLOPT_POST, true); curl_setopt($s, CURLOPT_POSTFIELDS, $queryStr ...
随机推荐
- CSS之美化页面
CSS之美化页面 <span></span> 标签 <span>行内定义一个区域 就是说一行可以被<span>划分多个小区域,从而实现某种特定效果.&l ...
- xml的应用与dtd约束
1.xml的应用 *不同的系统之间的传输数据(qq消息传输) *用来表示生活中有关系的数据(省市区的包含关系) *经常用在文件配置 **比如现在连接数据库,肯定知道数据库的名称和密码及用户名. ...
- yarn 无法下载node-sass
指定node-sass的下载源 yarn config set sass-binary-site http://npm.taobao.org/mirrors/node-sass
- 学习python第十二天,函数4 生成器generator和迭代器Iterator
在Python中,这种一边循环一边计算的机制,称为生成器:generator 要创建一个generator,有很多种方法.第一种方法很简单,只要把一个列表生成式的[]改成(),就创建了一个genera ...
- Go实现mqtt服务
package main import ( "os" "log" "github.com/eclipse/paho.mqtt.golang" ...
- 笔记-python-module-logging.循环日志、多进程日志
笔记-python-module-logging.循环日志.多进程日志 1. logging循环日志 循环日志分为按大小切分和按时间切分,对应实现类如下. 1.1. RotatingFil ...
- 【数据库】MySQL 从安装到命令
一, MySQL 的安装于配置 我是通过百度云盘的方式下载的.建议登录百度云终端,然后点击下面的链接,选择要安装的版本,解压安装. http://www.h2ero.cn/pan/share/17cd ...
- Hive数据导入导出的n种方式
Tutorial-LoadingData Hive加载数据的6种方式 #格式 load data [local] inpath '/op/datas/xxx.txt' [overwrite] into ...
- Spring.net Ioc 依赖注入
控制反转 (Inversion of Control,英文缩写为IoC)是一个重要的面向对象编程的法则来削减计算机程序的耦合问题,也是轻量级的Spring框架的核心. 控制反转一般分为两种类型,依赖注 ...
- Kotlin 二分法算法游戏--猜价格
本人最新想学习算法,算法是提高程序性能的关键! 程序就是数据结构和算法! 写了一个二分法的游戏,供大家参考: 当然,语言基于kotlin import java.util.* /** * Create ...