当前位置:网站首页>【洛谷】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)。
边栏推荐
猜你喜欢
目标检测中mAP计算以及源码解析
Take you do interface test from zero to the first case summary
Likou Brush Question Record 3.1-----977. Square of ordered array
9.1-----24. Swap the nodes in the linked list in pairs
全文翻译:Multimodal Neural Networks: RGB-D for Segmantic Segmentation and Object Detection
点击div内部默认文本被选中
【AspNetCore】实现JWT(使用Microsoft.AspNetCore.Authentication.JwtBearer)
Pytest+request+Allure实现接口自动化框架
终于有人把灰度发布架构设计讲明白了
企业面临的五大数据安全挑战
随机推荐
20220526动态规划:不同路径
Pytest+request+Allure实现接口自动化框架
全志通过fastboot烧写boot.img
快速乘写法
OpenLORIS-Object Datasets
gpio子系统和pinctrl子系统(上)
Solve the Final Fantasy 13-2 Clock Puzzle with DFS
基于NLP的智能问答系统核心技术
[TensorRT] 对UNet进行推理加速
自动化测试框架总结
2022 China Eye Expo, China Beijing International Children and Adolescent Eye Health Industry Exhibition
ROS 、SLAM 学习 error整理
二分搜索法和二叉搜索树
高性能 MySQL(十二):分区表
数仓第一篇:基础架构
为什么应用程序依赖关系映射对于云迁移至关重要
数字 07 verilog仿真实例
Open3D 均匀采样
20220525动态规划:跳跃游戏
online schema change and create index