博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
NOIP2012同余方程
阅读量:7217 次
发布时间:2019-06-29

本文共 1197 字,大约阅读时间需要 3 分钟。

  

P1029 - 【NOIP2012】同余方程

Description

求关于 x 的同余方程 ax ≡ 1 (mod b)的最小正整数解。

Input

输入只有一行,包含两个正整数 a, b,用一个空格隔开。

Output

输出只有一行,包含一个正整数X0,即最小正整数解。输入数据保证一定有解。

Sample Input

3 10

Sample Output

7

Hint

【数据范围】

对于 40%的数据,2 ≤b≤ 1,000;
对于 60%的数据,2 ≤b≤ 50,000,000;
对于 100%的数据,2 ≤a, b≤ 2,000,000,000。

Source

NOIP2012,数论,拓展欧几里得,欧拉函数

 
 
扩展欧几里得
1 #include
2 #include
3 #include
4 #include
5 #include
6 #include
7 using namespace std; 8 int gi() { 9 int res=0,f=1;10 char ch=getchar();11 while((ch<'0'||ch>'9')&&ch!='-') ch=getchar();12 if(ch=='-') ch=getchar(),f=-1;13 while(ch>='0'&&ch<='9') res=res*10+ch-'0',ch=getchar();14 return res*f;15 }16 int cal_gcd(int a,int b,int& x,int& y) {17 if(!b) {18 x=1,y=0;19 return a;20 }21 int d=cal_gcd(b,a%b,x,y);22 int tmp=x;23 x=y;24 y=tmp-a/b*y;25 return d;26 }27 int main() {28 freopen("mod.in","r",stdin);29 freopen("mod.out","w",stdout);30 int a=gi(),b=gi(),x,y;31 int z=cal_gcd(a,b,x,y);32 x/=z;33 /*if(x>0) while(x>0) x-=b;34 else if(x<0) while(x<0) x+=b;35 if(x<=0) x+=b;36 cout<

 

转载于:https://www.cnblogs.com/ypz999/p/6657110.html

你可能感兴趣的文章
Spring aop 切不进去原因。。
查看>>
PHP获取客户端IP
查看>>
php开发APP接口-封装通信接口改进版
查看>>
Android系统性能演变历程
查看>>
OSChina 周三乱弹 —— 打醒精神去瞌睡
查看>>
SSH 密钥登录linux
查看>>
你必须掌握的 21 个 Java 核心技术!
查看>>
告诉你WHT中文站是什么?
查看>>
4、Juniper SSG520 PPTP映射到ROS后MAC无法连接解决方法
查看>>
利用批处理文件来建立一个记录3389登陆者信息
查看>>
Linux 系统下双机HA的实现
查看>>
02_swarm mode key concepts
查看>>
Eclipse打包插件Fat Jar 解压打包
查看>>
Apache Shiro 使用手册
查看>>
CentOS mini 6.5 安装DB2 Express-C 问题处理记录
查看>>
DirectByteBuffer
查看>>
Docker Compose文件详解 V2
查看>>
Memcached的原理与应用(未完)
查看>>
基于 Confluence 6 数据中心的 SAML 单点登录设置你的身份提供者
查看>>
mysql总结
查看>>