[2019杭电多校第五场][hdu6624]fraction
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6624
题意为求最小的b满足$a*b^{-1}\equiv x(modp)$.
把式子化简一下:
$a\equiv b*x(modp)$
$a=b*x-p*y$
$\because 0<a<b$
$\therefore 0<b*x-p*y<b$
$0<b*x-p*y\Rightarrow \frac{p}{x}<\frac{b}{y}$
$b*x-p*y<b\Rightarrow \frac{b}{y}<\frac{p}{x-1}$
$\therefore \frac{p}{x}<\frac{b}{y}<\frac{p}{x-1}$
这样题目就转化成求最小的b,y满足上式。
如果当前$[\frac{p}{x},\frac{p}{x-1}]$之间有整数,则$b=\left \lfloor \frac{p}{x} \right \rfloor+1,y=1$。
如果没有则可以辗转相除来递归求解。
例如:p=11,x=7。
$\frac{11}{7}<\frac{b}{y}<\frac{11}{6}$
则化为真分数后在求倒数。
$\frac{6}{5}<\frac{y}{b-y}<\frac{7}{4}$
再次化为真分数后求倒数。
$\frac{4}{3}<\frac{b-y}{2*y-b}<5$
此时[4/3,5]内有整数,则回溯得到答案。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<queue>
using namespace std;
typedef long long ll;
void f(ll a, ll b, ll c, ll d, ll& x, ll& y){
ll q = a / b + ;
if (q <= c / d){
x = q;
y = ;
return;
}
q--;
a -= q * b; c -= q * d;
f(d, c, b, a, y, x);
x += q * y;
return;
}
int main(){
int t;
scanf("%d", &t);
while (t--){
ll b, y, a, p, x;
scanf("%lld%lld", &p, &x);
f(p, x, p, x - , b, y);
a = b * x - y * p;
printf("%lld/%lld\n", a, b);
}
return ;
}
[2019杭电多校第五场][hdu6624]fraction的更多相关文章
- [2019杭电多校第五场][hdu6630]permutation 2
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6630 题意为求出1-n,n个数的全排列中有多少种方案满足第一位为x,第n位为y,且相邻数字绝对值之差不 ...
- [2019杭电多校第五场][hdu6629]string matching(扩展kmp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6629 题意求字符串的每个后缀与原串的最长公共前缀之和. 比赛时搞东搞西的,还搞了个后缀数组...队友一 ...
- [2019杭电多校第五场][hdu6628]permutation 1
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6628 题意为求字典序第k小的差异数组,差异数组p满足p[i]=a[i+1]-a[i]. 头铁的爆搜,因 ...
- [2019杭电多校第五场][hdu6625]three arrays(01字典树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6625 大意为给你两个数组a和b,对应位置异或得到c数组,现在可以将a,b数组从新排序求c数组,使得字典 ...
- 2019杭电多校第五场 discrete logarithm problem
https://vjudge.net/contest/317493#problem/I
- 2018杭电多校第五场1002(暴力DFS【数位】,剪枝)
//never use translation#include<bits/stdc++.h>using namespace std;int k;char a[20];//储存每个数的数值i ...
- 2017杭电多校第五场11Rikka with Competition
Rikka with Competition Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/O ...
- 2017杭电多校第五场Rikka with Subset
Rikka with Subset Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...
- hdu6356 Glad You Came 杭电多校第五场 RMQ ST表(模板)
Glad You Came Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) ...
随机推荐
- 字符串与List互转
List转字符串,用逗号隔开 List<string> list = new List<string>(); list.Add("a"); list.Add ...
- Linux架构--------Rsync守护进程推和拉
一.Rsync基本概述 rsync是一款开源.快速.多功能.可实现全量及增量的本地或远程数据同步备份的优秀工具.rsync软件适用于Unix/linux/Windows等多种操作系统平台. 二.Rsy ...
- 关于让左右2个DIV高度相等
哪个div Height值大,就将其值赋给Height值小的div,从而使2个div高度始终保持一 以下是代码: <!DOCTYPE html><html lang="en ...
- Linux php.ini的安全优化配置
Linux php.ini的安全优化配置 (1) PHP函数禁用找到 disable_functions = 该选项可以设置哪些PHP函数是禁止使用的,PHP中有一些函数的风险性还是相当大的,可以 ...
- HDU 5418 Victor and World (状态压缩dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5418 题目大意:有n个结点m条边(有边权)组成的一张连通图(n <16, m<100000 ...
- OpenCV feature2d
/*M/////////////////////////////////////////////////////////////////////////////////////// // // IMP ...
- es之java操作插入文档
4方式: . 使用json字符串直接创建 . 使用Map集合 . 使用第三方库来序列化 createDocumentBySerialize . 使用内置的帮助器XContentFactory.json ...
- loadrunner常用函数整理
1.int web_reg_save_param("参数名","LB=左边界","RB=右边界",LAST); //注册函数,在参数值出 ...
- Oracle10g 64位 在Windows 2008 Server R2 中的安装 DBconsole无法启动
致谢!本文参考http://www.cnblogs.com/leiOOlei/archive/2013/08/19/3268239.html 背景: 操作系统Windows 2008 Server R ...
- 通过HookNtCreateSection 动态监控驱动sys、动态链接库dll、可执行文件exe加载
[cpp] view plaincopyprint? /* windows2003 x86/x64 window7 x86 windows2008 R2 x64测试通过 */ #include < ...