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 #include2 #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<