[2019HDU多校第一场][HDU 6584][G. Meteor]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6584
题目大意:求所有满足\(0<\frac{p}{q}\leq1, gcd(p,q)=1,p\leq n,q\leq n\)的分数中,第\(k\)小的分数
题解:考虑二分答案,并用分数形式记录。假设当前二分的分数为\(\frac{p}{q}\),则小于等于这个分数的个数为
$$\sum_{i=1}^{n}\sum_{j=1}^{\left \lfloor \frac{pi}{q} \right \rfloor}[gcd(i,j)==1]=\sum_{i=1}^{n}\sum_{j=1}^{\left \lfloor \frac{pi}{q} \right \rfloor}\sum_{d|i,d|j}^{ }\mu(d)=\sum_{i=1}^{n}\sum_{d|i}^{ }\mu(d)\left \lfloor \frac{pi}{qd} \right \rfloor=\sum_{d=1}^{n}\mu(d)\sum_{i=1}^{\left \lfloor \frac{n}{d} \right \rfloor}\left \lfloor \frac{pi}{q} \right \rfloor$$
这个式子可以通过预处理莫比乌斯函数的值,然后分块加类欧做到\(O(\sqrt{n}logn)\)的时间复杂度求解
在二分出答案后,只需找到最小的大于等于\(\frac{p}{q}\)的分数即可。官方题解使用了\(Stern-Brocot Tree\),实际上通过枚举分母也可以做到\(O(n)\)求解
我的代码跑了7347ms...和之前的A还有K题一样极限_(:з」∠)_
#include<bits/stdc++.h>
using namespace std;
#define N 1000001
#define LL long long
LL T,n,k,cnt,p[N],mu[N],v[N];
#undef LL
struct Frac
{
#define LL __int128
LL p,q;
void simp()
{
LL d=__gcd(p,q);
p/=d,q/=d;
}
Frac operator +(const Frac &t)const
{
Frac tmp;
tmp.q=q*t.q;
tmp.p=p*t.q+q*t.p;
tmp.simp();
return tmp;
}
Frac operator /(const LL &t)const
{
Frac tmp;
tmp.p=p;
tmp.q=q*t;
tmp.simp();
return tmp;
}
bool operator <(const Frac &t)const
{
return p*t.q<q*t.p;
}
bool operator <=(const Frac &t)const
{
return p*t.q<=q*t.p;
}
#undef LL
};
void pretype()
{
#define LL long long
mu[]=;
for(LL i=;i<N;i++)
{
if(!v[i])p[++cnt]=i,mu[i]=-;
for(LL j=;j<=cnt && i*p[j]<N;j++)
{
v[i*p[j]]=;
if(i%p[j]==)break;
mu[i*p[j]]=-mu[i];
}
}
for(LL i=;i<N;i++)
mu[i]+=mu[i-];
#undef LL
}
#define LL __int128
LL f(LL a,LL b,LL c,LL n)
{
LL m=(a*n+b)/c;
if(!a || !m)return ;
if(a>=c || b>=c)return n*(n+)/*(a/c)+(b/c)*(n+)+f(a%c,b%c,c,n);
return n*m-f(c,c-b-,a,m-);
}
LL check(Frac k)
{
LL res=;
for(LL i=;i<=n;i++)
{
LL j=n/(n/i);
res+=(mu[j]-mu[i-])*f(k.p,,k.q,n/i);
i=j;
}
return res;
}
LL ceil(LL x,LL y){return (x-)/y+;}
#undef LL
void Find(Frac k)
{
#define LL long long
Frac ans={,};
for(LL i=;i<=n;i++)
ans=min(ans,{ceil(k.p*i,k.q),i});
ans.simp();
printf("%lld/%lld\n",(LL)ans.p,(LL)ans.q);
}
void init()
{
scanf("%lld%lld",&n,&k);
Frac l={,n},r={,},mid;
while(l+(Frac){,n*n}<=r)
{
mid=(l+r)/;
if(check(mid)<k)l=mid;
else r=mid;
}
Find(l);
}
int main()
{
//freopen("test.in","r",stdin);
//freopen("test.out","w",stdout);
pretype();
scanf("%lld",&T);
while(T--)init();
return ;
}
[2019HDU多校第一场][HDU 6584][G. Meteor]的更多相关文章
- [2019HDU多校第一场][HDU 6580][C. Milk]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6580 题目大意:\(n\times m\)大小的方格上有\(k\)瓶水,喝完每瓶水都需要一定的时间.初 ...
- [2019HDU多校第一场][HDU 6578][A. Blank]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6578 题目大意:长度为\(n\)的数组要求分别填入\(\{0,1,2,3\}\)四个数中的任意一个,有 ...
- [2019HDU多校第一场][HDU 6588][K. Function]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6588 题目大意:求\(\sum_{i=1}^{n}gcd(\left \lfloor \sqrt[3] ...
- [2019HDU多校第一场][HDU 6590][M. Code]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6590 题目大意(来自队友):二维平面上有\(n\)个点,每个点要么是黑色要么是白色,问能否找到一条直线 ...
- 2019HDU多校第一场1001 BLANK (DP)(HDU6578)
2019HDU多校第一场1001 BLANK (DP) 题意:构造一个长度为n(n<=10)的序列,其中的值域为{0,1,2,3}存在m个限制条件,表示为 l r x意义为[L,R]区间里最多能 ...
- [2019HDU多校第二场][HDU 6591][A. Another Chess Problem]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6591 题目大意:二维坐标系上,所有满足\(5|2x+y\)的点都被设为障碍物,无法通过.现给出一对点, ...
- 2019HDU多校第一场 6582 Path 【最短路+最大流最小割】
一.题目 Path 二.分析 首先肯定要求最短路,然后如何确定所有的最短路其实有多种方法. 1 根据最短路,那么最短路上的边肯定是可以满足$dist[from] + e.cost = dist[to] ...
- 【求出所有最短路+最小割】【多校第一场】【G题】
题意 A从1要追在N的 B 只能走最短的路 问B最少切断多少条路可以让A不能过来 问B最多切断多少条路A还是能过来 对于1 求出1到N的所有最短路的路径,对其求最小割 对于2 求出长度最小的最短路即可 ...
- 2019HDU多校第一场 BLANK DP
题意:有四种数字,现在有若干个限制条件:每个区间中不同的数字种类必须是多少种,问合法的方案数. 思路: 定义 dp[i][j][k][t] 代表填完前 t 个位置后,{0,1,2,3} 这 4 个数字 ...
随机推荐
- Django mysql-client
sudo apt-get install libmysqlclient-dev 报错,mysqlclient 没有安装 :实际已经安装,这是因为mysql安装时没有安装好 https://www.cn ...
- python学习-45 模块
模块 -----模块包括三种: ····python标准库 ····第三方模块 ····应用程序自定义模块 -------应用程序自定义模块 1.建立两个py文件,一个是定义函数用的cal.py de ...
- 『Linux』第二节: 安装Linux系统
一. 准备工具 1. centOS系统下载 http://isoredirect.centos.org/centos/7/isos/x86_64/CentOS-7-x86_64-DVD-1810.is ...
- CSPS2019游记
Day1: T1:格雷码?看一眼感觉是结论题,但是没头绪推不出来,硬刚40min想到$\oplus$切了. 但是没写unsigned挂了五分... T2:全场爆切人均50的题,就我一个写挂了35pts ...
- (十三)Hibernate中的多表操作(3):单向多对多
多对多的处理方式是,有一张中间表,中间表保存两个多方之间的关系.首先来看实际应用场景:在之前开发的系统中,应用了基于角色的控制访问,也就是RBAC模型,一个用户可能存在多种角色,一种角色也可能有多个用 ...
- (十五)struts2之注解
一.作用 以用来替换struts.xml配置文件 使用前提 :必须引入struts2-convention-plugin-2.3.14.jar 这个jar包 二.参数 @Action来代替<ac ...
- Entity Framewrok Migration 重置
转载自:https://weblog.west-wind.com/posts/2016/jan/13/resetting-entity-framework-migrations-to-a-clean- ...
- Linux增加虚拟内存
Docker容器启动Mysql镜像报错,提示无法分配内存,报错信息如下: 由此我们看到Swap为0,考虑适当增加swap. Linux开启swap空间有好几种方法,在这里只介绍比较常用的两种. 使用交 ...
- 关于ECharts甘特图的实现
对于使用ECharts图表的步骤,每种图表都是一致的,相信大家也都了解 此处只分享甘特图的option,代码如下: option: { title: { text: '项目实施进度表', left: ...
- 定位之z-index
我们已经知道固定定位(fixed)和绝对定位(absolute)可以让盒子浮起来 相对定位(relactive)虽然不能让盒子浮起来,但也是可以让图层浮起来 那么既然大家都可以浮起来,就会存在一个问题 ...