您的位置:首页 > 其它

【NOIP2012】同余方程

2016-11-15 17:25 204 查看
【codevs 1200】

1200 同余方程 2012年NOIP全国联赛提高组

时间限制: 1 s

空间限制: 128000 KB

题目等级 : 钻石 Diamond

题解

题目描述 Description

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

输入描述 Input Description

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

输出描述 Output Description

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

样例输入 Sample Input

3 10

样例输出 Sample Output

7

数据范围及提示 Data Size & Hint

【数据范围】

对于 40% 的数据, 2 ≤b≤ 1,000 ;

对于 60% 的数据, 2 ≤b≤ 50,000,000

对于 100% 的数据, 2 ≤a, b≤ 2,000,000,000

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int ans = 0;
int a,b,x,y,s;

void cal(int a,int b)
{
if(b == 0)  x = 1,y = 0,ans = a;
else
{
cal(b,a % b);
s = x,x = y,y = s - a / b * y;
return;
}
}

int main()
{
scanf("%d %d",&a,&b);
cal(a,b);
while(x < 0)    x += b;
printf("%d\n",x / ans);
return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: