#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int gcd(ll a, ll b, ll &x, ll &y){
if(b == 0){
x = 1;
y = 0;
return a;
}
ll d = gcd(b, a % b, y, x);
y = y - (a / b) * x;
return d;
}
int main(){
int T;
cin >> T;
ll N, S, K;
ll x, y;
ll g;
while(T--){
cin >> N >> S >> K;
g = gcd(-K, N, x, y);
if(S % g != 0)
cout << -1 << endl;
else{
ll ans = x * (S / g);
if(g < 0)
g = -g;
ll n = N / g;
while(ans < 0)
ans += n;
cout << ans % n << endl;
}
}
return 0;
}//exgcd
exgcd
最新推荐文章于 2024-07-14 10:46:47 发布