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 ...
随机推荐
- SAP标准导出功能 - 删除默认选定格式
我们经常会使用SAP系统的标准功能导出ALV显示的数据,一般会选择电子表格. 选择电子表格之后,需要选择电子表格的具体格式. 选择格式之后点击确定,会弹出保存对话框. 如果在使用这个功能的时候,选择了 ...
- spring-bean(注解方式-管理及依赖注入)
Bean管理(注解方式) 1.添加注解的依赖包:Spring-aop.jar 2.配置spring的XML文件的引入(查官方源码) 3.开启注解的扫描 <context:component-sc ...
- MongoDB模糊查询
模糊查询简介MongoDB查询条件可以使用正则表达式,从而实现模糊查询的功能.模糊查询可以使用$regex操作符或直接使用正则表达式对象. MySQL MongoDB select * from s ...
- 使用select2 宽度自适应
加一个CSS属性:style = "width : 100%"
- 011---Djang的cookie和session
-------------------------------------------------------------cookie与session------------------------- ...
- Poweroj:来自学长的善意:ZQ的杀龙之旅(状压BFS)
传送门:https://www.oj.swust.edu.cn/problem/show/2794 来自学长的善意:ZQ的杀龙之旅 Time Limit: 15000 MS Memory Limit: ...
- CF797E. Array Queries
a is an array of n positive integers, all of which are not greater than n. You have to process q que ...
- 【文件处理】xml 文件 DOM解析
一.Java解析xml.解析xml四种方法.DOM.SAX.JDOM.DOM4j.XPath 此文针对其中的DOM方法具体展开介绍及代码分析 sax.dom是两种对xml文档进行解析的方法(没有具体实 ...
- WPF仿酷狗页面
原文:WPF仿酷狗页面 版权声明:本文为博主原创文章,如需转载请标明转载地址 http://blog.csdn.net/u013981858 https://blog.csdn.net/u013981 ...
- Javascript Step by Step - 04
前言 本篇主要讨论jQuery的常用的若干操作.为了能直观的显示操作的结果,首先建立一个html文件,内容如下: <!DOCTYPE html> <html> <head ...