同余方程

[Time Gate]

https://www.luogu.org/problemnew/show/P1082

[解题思路】

推荐一个不错的写扩欧的博客

https://www.zybuluo.com/samzhang/note/541890

这是一道扩欧模板题

【code】

 1 #include <cstdio>
 2 #include <iostream>
 3 #include <algorithm>
 4 using namespace std;
 5 int a,b,res,ans,x,y; 
 6 inline void exEuclid_Algorithm(int a,int b,int &x,int &y){
 7     if(b!=0){
 8         exEuclid_Algorithm(b,a%b,y,x);
 9         y-=a/b*x;
10     }
11     else {
12         x=1;
13         y=0;
14         return ;
15     }
16 }
17 int main(){
18     //freopen("1082.in","r",stdin);
19     //freopen("1082.out","w",stdout);
20     scanf("%d%d",&a,&b);
21     /*while(res!=1){
22         ans++;
23         res=res+a;
24         if(res>b)res%=b;
25     }
26     printf("%lld\n",ans);*/
27     exEuclid_Algorithm(a,b,x,y);
28     printf("%d\n",(x%b+b)%b);
29     return 0;
30 }

 

posted @ 2019-07-10 00:18  GTR_PaulFrank  阅读(137)  评论(0编辑  收藏  举报