【BZOJ1477】青蛙的约会【扩展欧几里得】

来源:互联网 发布:财政部ppp中心数据 编辑:程序博客网 时间:2024/06/05 19:47

【题目链接】

/* Telekinetic Forest Guard */#include <cstdio>#include <algorithm>using namespace std;typedef long long LL;inline void exgcd(LL &x, LL &y, LL a, LL b) {b ? (exgcd(y, x, b, a % b), y -= a / b * x) : (x = 1, y = 0);}int main() {LL a, b, n, m, L;scanf("%lld%lld%lld%lld%lld", &a, &b, &n, &m, &L);LL q = n - m, p = b - a;while(q < 0) q += L;while(p < 0) p += L;LL d = __gcd(q, L);if(p % d) {printf("Impossible\n");return 0;}LL x, y;exgcd(x, y, q / d, L / d);x = x * p / d % L;if(x < 0) x += L;printf("%lld\n", x);return 0;}


0 0
原创粉丝点击