NOIP2012同余方程

  

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<iostream> 
 2 #include<cstdio>
 3 #include<cstdlib>
 4 #include<cmath>
 5 #include<algorithm>
 6 #include<ctime>
 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<<x;*/
37     printf("%d ",((x%b)+b)%b);
38     return 0;
39 }

 

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值