5124 Problem A 同餘方程-NOIP2012TGD2T1

問題 A: 同餘方程-NOIP2012TGD2T1

時間限制: 1 Sec  內存限制: 128 MB
 

題目描述

求關於x的同餘方程ax≡1(mod b)的最小正整數解。

 

輸入格式

每組輸入數據只有一行,包含兩個正整數a, b,用一個空格隔開。

數據規模:

對於40%的數據,2≤b≤1,000;

對於60%的數據,2≤b≤50,000,000;

對於100%的數據,2≤a, b≤2,000,000,000。

輸出

每組輸出只有一行,包含一個正整數x0,即最小正整數解。輸入數據保證一定有解。

樣例輸入

3 10

樣例輸出

7

經驗總結

基礎題~~

AC代碼

#include<cstdio>
#include<cstring>
using namespace std;
int exGcd(int a,int b,int &x,int &y)
{
	if(b==0)
	{
		x=1;
		y=0;
		return a;
	}
	int g=exGcd(b,a%b,x,y);
	int temp=x;
	x=y;
	y=temp-a/b*y;
	return g;
}
int main()
{
	int a,b;
	while(~scanf("%d %d",&a,&b))
	{
		int x,y;
		int g=exGcd(a,b,x,y);
		printf("%d\n",(x%b/g+b/g)%(b/g));
	}
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章