AcWing 1299. 五指山--java版
原题链接
简单
java版
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
while(T-- > 0) {
long n = sc.nextLong();
long d = sc.nextLong();
long x = sc.nextLong();
long y = sc.nextLong();
Long a = new Long();
Long b = new Long();
long gcd = exgcd(n, d, a, b);
if((y - x) % gcd != 0) {
System.out.println("Impossible");
}else {
b.v *= (y - x) / gcd;
n /= gcd;
System.out.println((b.v % n + n) % n);
}
}
sc.close();
}
private static long exgcd(long a, long b, Long x, Long y) {
if(b == 0) {
x.v = 1;
y.v = 0;
return a;
}
long d = exgcd(b, a % b, y, x);
y.v -= a / b * x.v;
return d;
}
}
class Long{
long v;
public Long() {};
public Long(long v) {
this.v=v;
}
}
通过对象实现C++里的引用,赞👍
JAVA为什么要取y