五指山

题目描述

在这里插入图片描述

分析

首先是扩展欧几里得算法的知识
在这里插入图片描述
然后是关于这个题的分析
在这里插入图片描述

代码

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b, ll &x, ll &y)
{
    
    
    if (!b)
    {
    
    
        x = 1, y = 0;
        return a;
    }
    ll d = gcd(b, a % b, y, x);
    y -= a / b * x;
    return d;
}
int main()
{
    
    
    int t;
    cin >> t;
    while (t--)
    {
    
    
        ll n, d, x, y, a, b;
        cin >> n >> d >> x >> y;
        ll g = gcd(n, d, a, b);
        if ((y - x) % g)
            cout << "Impossible" << endl;
        else
        {
    
    
            b *= (y - x) / g;
            n /= g;
            ll res = (b % n + n) % n;
            cout << res << endl;
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_46126537/article/details/113828951
今日推荐