比赛 |
201712练习 |
评测结果 |
AAAAAAAAAA |
题目名称 |
同余方程 |
最终得分 |
100 |
用户昵称 |
胡嘉兴 |
运行时间 |
0.003 s |
代码语言 |
C++ |
内存使用 |
0.31 MiB |
提交时间 |
2017-12-23 11:00:14 |
显示代码纯文本
#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll gcd(ll a, ll b)
{
return b == 0 ? a : gcd(b, a%b);
}
ll extended_gcd(ll a, ll b, ll &x, ll &y)
{
if(!b)
{
x = 1;
y = 0;
return a;
}
ll ret = extended_gcd(b, a%b, x, y);
ll tmp = x;
x = y;
y = tmp-a/b*y;
return ret;
}
int main()
{
ll a, b, t, x, y, z;
freopen("mod.in","r",stdin);
freopen("mod.out","w",stdout);
scanf("%lld%lld", &a, &b);
z = extended_gcd(a, -1*b, x, y);
x /= z;
y /= z;
t = b/gcd(a, b);
printf("%lld\n", (x%t+t)%t);
return 0;
}