当前位置:网站首页>【洛谷】P2613 【模板】有理数取余
【洛谷】P2613 【模板】有理数取余
2022-08-09 02:41:00 【记录算法题解】
题目地址:
https://www.luogu.com.cn/problem/P2613
题目描述:
给出一个有理数 c = a b c=\frac{a}{b} c=ba,求 c m o d 19260817 c \bmod 19260817 cmod19260817的值。
这个值被定义为 b x ≡ a ( m o d 19260817 ) bx\equiv a\pmod{19260817} bx≡a(mod19260817)的解。
输入格式:
一共两行。
第一行,一个整数 a a a。
第二行,一个整数 b b b。
输出格式:
一个整数,代表求余后的结果。如果无解,输出Angry!。
数据范围:
对于所有数据,保证 0 ≤ a ≤ 1 0 10001 0\leq a \leq 10^{10001} 0≤a≤1010001, 1 ≤ b ≤ 1 0 10001 1 \leq b \leq 10^{10001} 1≤b≤1010001,且 a , b a, b a,b不同时是 19260817 19260817 19260817的倍数。
令 p = 19260817 p=19260817 p=19260817,可验证 p p p是素数。那么即是要求 ( x , y ) (x,y) (x,y)使得 b x + p y = a bx+py=a bx+py=a的解,并且使得 x x x取最小正整数,参考https://blog.csdn.net/qq_46105170/article/details/126221151。代码如下:
#include <iostream>
using namespace std;
const int P = 19260817;
int a, b;
void fast_read(int &x) {
char ch;
while (!isdigit(ch = getchar()));
for (; isdigit(ch); ch = getchar()) {
x = x * 10 + ch - '0';
x %= P;
}
}
int exgcd(int a, int b, int &x, int &y) {
if (!b) {
x = 1, y = 0;
return a;
}
int d = exgcd(b, a % b, y, x);
y -= a / b * x;
return d;
}
int main() {
fast_read(a);
fast_read(b);
int x, y;
int d = exgcd(b, P, x, y);
if (a % d) puts("Angry!");
else printf("%ld\n", ((long) x * a / d % P + P) % P);
}
时空复杂度 O ( log b ) O(\log b) O(logb)。
边栏推荐
- 点击div内部默认文本被选中
- Postman interface test [official website] latest version installation and use tutorial
- 数仓第二篇: 数据模型(维度建模)
- DSP28379学习笔记 (一)——GPIO基本操作
- 工作小计 rtcp的length和网络字节序
- 科大讯飞笔试题复盘
- 【izpack】使用izpack为你的程序提供安装程序封装
- Likou Brush Question Record 4.1-----209. The sub-array with the smallest length
- 【Untitled】
- 【AspNetCore】实现JWT(使用Microsoft.AspNetCore.Authentication.JwtBearer)
猜你喜欢

不会吧!不会吧!居然还有人不知道重绘以及回流

Jenkins配置钉钉通知

数仓第二篇: 数据模型(维度建模)

HMS Core分析服务智能运营6.5.1版本上线

数字 07 verilog仿真实例

What aspects should we start with for interface security testing?

Inheritance

"Lonely Walking on the Moon": Two choices of Duguyue, let a "middleman" become a big hero

【Jenkins 学习笔记】玩转持续集成与持续交付

Pytest+request+Allure实现接口自动化框架
随机推荐
Jenkins配置钉钉通知
工作小计 rtcp的length和网络字节序
Open3D 计算点云的均值(质心)与协方差
通过安装VNC服务器x11vnc(或vnc4server)和配置x11vnc.service实现远程通过VNC-Viewer访问VNC服务器。
【云计算】XaaS最全介绍(按24字母合集):AaaS、BaaS、CaaS、DaaS、EaaS、FaaS、GaaS、HaaS、IDaaS…
Flume (四) --------- Flume 企业开发案例
JS 将对象拆开拼接成 URL
Maya engine modeling
最近看到很多人想自学或者报班但是不清楚如何选择,我今天就和大家说说
A40i gxl3680 ts_print报错:tslib: Selected device is not a touchscreen (must support ABS and KEY event
ROS2错误:不支持OpenGL 1.5 GLRenderSystem:: ci initialiseContext在C: \ \ ws \构建……
为什么应用程序依赖关系映射对于云迁移至关重要
Rotate the neon circle
数仓第二篇: 数据模型(维度建模)
2022年自然语言处理校招社招实习必备知识点盘点分享
中国SSD产业突围有多难?除了技术“瓶颈”还有哪里挑战?
使网络安全威胁风险更高和成本更高的五个趋势
What are the most popular automated testing tools in 2022?The most complete and most detailed of the entire network is here
Likou Brush Question Record 6.1-----203. Remove linked list elements
18.flink Table/Sql API之 catlog