[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 个数字 ...
随机推荐
- [转帖]Postgresql的csv日志设置
Postgresql的csv日志设置 2012年06月16日 09:27:00 weixin_34406796 阅读数 24 原文链接:https://my.oschina.net/Kenyon/ ...
- Windows Terminal Preview v0.7 Release
Windows Terminal Preview v0.7 Release The following key bindings are included by default within this ...
- MongoDB 聚合函数及排序
聚合函数 最大值 $max db.mycol.aggregate([{$group : {_id : "$by_user", num_max : {$max: "$li ...
- golang字符串常用的系统函数
1.统计字符串的长度,按字节len(str) str := "hello北京" fmt.Println("str len=", len(str)) 2.字符串遍 ...
- PHP trait介绍
Trait 自 PHP 5.4.0 起,PHP 实现了一种代码复用的方法,称为 trait. Trait 是为类似 PHP 的单继承语言而准备的一种代码复用机制.Trait 为了减少单继承语言的限制, ...
- js对象 c#对象转换
前台页面 js 创建对象 let t = {}; 数组对象 let c = []; c.push({}) ;// 添加对象 以string格式 传递 JSON JSON.stringify(c); c ...
- Docker 安装mysql5.6
1.首先进入命令行现在mysql5.6镜像 E:\>docker pull mysql:5.6 2.把mysql的配置文件放入到本地,供后期做修改用 文件分别为:mysql.cnf 和 mysq ...
- cmd查找端口占用情况
查找端口占用情况:netstat -ano|findstr 4848 查看使用指定端口的应用程序:tasklist|findstr xxxx,xxxx指的是pid 结束指定进程:taskkill /p ...
- 【转】[STL]vector和deque的内存释放(clear)
vector的clear成员函数可以清除vector中的元素,使其大小减至0.但它却不能减小vector占用的内存. [cpp] view plain copy int main() { vector ...
- LeetCode:626.换座位
题目链接:https://leetcode-cn.com/problems/exchange-seats/ 题目 小美是一所中学的信息科技老师,她有一张 seat 座位表,平时用来储存学生名字和与他们 ...