题目链接: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]的更多相关文章

  1. [2019HDU多校第一场][HDU 6580][C. Milk]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6580 题目大意:\(n\times m\)大小的方格上有\(k\)瓶水,喝完每瓶水都需要一定的时间.初 ...

  2. [2019HDU多校第一场][HDU 6578][A. Blank]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6578 题目大意:长度为\(n\)的数组要求分别填入\(\{0,1,2,3\}\)四个数中的任意一个,有 ...

  3. [2019HDU多校第一场][HDU 6588][K. Function]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6588 题目大意:求\(\sum_{i=1}^{n}gcd(\left \lfloor \sqrt[3] ...

  4. [2019HDU多校第一场][HDU 6590][M. Code]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6590 题目大意(来自队友):二维平面上有\(n\)个点,每个点要么是黑色要么是白色,问能否找到一条直线 ...

  5. 2019HDU多校第一场1001 BLANK (DP)(HDU6578)

    2019HDU多校第一场1001 BLANK (DP) 题意:构造一个长度为n(n<=10)的序列,其中的值域为{0,1,2,3}存在m个限制条件,表示为 l r x意义为[L,R]区间里最多能 ...

  6. [2019HDU多校第二场][HDU 6591][A. Another Chess Problem]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6591 题目大意:二维坐标系上,所有满足\(5|2x+y\)的点都被设为障碍物,无法通过.现给出一对点, ...

  7. 2019HDU多校第一场 6582 Path 【最短路+最大流最小割】

    一.题目 Path 二.分析 首先肯定要求最短路,然后如何确定所有的最短路其实有多种方法. 1 根据最短路,那么最短路上的边肯定是可以满足$dist[from] + e.cost = dist[to] ...

  8. 【求出所有最短路+最小割】【多校第一场】【G题】

    题意 A从1要追在N的 B 只能走最短的路 问B最少切断多少条路可以让A不能过来 问B最多切断多少条路A还是能过来 对于1 求出1到N的所有最短路的路径,对其求最小割 对于2 求出长度最小的最短路即可 ...

  9. 2019HDU多校第一场 BLANK DP

    题意:有四种数字,现在有若干个限制条件:每个区间中不同的数字种类必须是多少种,问合法的方案数. 思路: 定义 dp[i][j][k][t] 代表填完前 t 个位置后,{0,1,2,3} 这 4 个数字 ...

随机推荐

  1. sqlserver交换数据行中的指定列

    <!-- 次序上移下移 --> <update id="upOrDown" parameterType="java.util.Map"> ...

  2. 深度学习-mnist手写体识别

    mnist手写体识别 Mnist数据集可以从官网下载,网址: http://yann.lecun.com/exdb/mnist/ 下载下来的数据集被分成两部分:55000行的训练数据集(mnist.t ...

  3. matplotlib笔记1

    散点图-scatter 散点图显示两组数据的值,每个点的坐标位置由变量的值决定由一组不连接的点完成,用于观察两种变量的相关 import numpy as np import matplotlib.p ...

  4. shell习题第24题:杀进程

    [题目要求] 一台机器负载高,top查看到有很多sh的进程,然后top -c查看可以看到对应的进程命令是sh -c /bin/clear.sh 经分析后发现是因为该脚本执行时间太长,导致后续执行时,上 ...

  5. gin shoudBind

    GET 请求 a_b POST aB或者AB //json大小写aB或者AB,form 表单 下划线a_b

  6. IDEA GIT 忽略文件 最佳方式

    前言 转载一篇博客,简单,实用. 原文地址:intellij idea 忽略文件不提交 ps:下面均为转载博客的内容: 在intellij中忽略提交文件,分两种情况, 文件没有纳入版本管理 第一种,文 ...

  7. PAT-1014 Waiting in Line (30 分) 优先队列

    Suppose a bank has N windows open for service. There is a yellow line in front of the windows which ...

  8. [yarn]yarn和npm的对比

    一.简介 NPM是随同NodeJS一起安装的包管理工具,能解决NodeJS代码部署上的很多问题,常见的使用场景有以下几种: 允许用户从NPM服务器下载别人编写的第三方包到本地使用. 允许用户从NPM服 ...

  9. Java ShellSort

    Java ShellSort /** * <html> * <body> * <P> Copyright 1994-2018 JasonInternational ...

  10. MSSql-SP_who分析数据库性能

    https://blog.csdn.net/xiaoxu0123/article/details/5757640 https://www.cnblogs.com/kelelipeng/p/104959 ...