git reimport
This commit is contained in:
176
archive/2017.04/2017.04.16 - unsorted/TaskB.java
Normal file
176
archive/2017.04/2017.04.16 - unsorted/TaskB.java
Normal file
@@ -0,0 +1,176 @@
|
||||
package chelper;
|
||||
|
||||
import java.math.BigInteger;
|
||||
|
||||
import io.InputReader;
|
||||
import io.OutputWriter;
|
||||
import misc.SimpleSavingChelperSolution;
|
||||
import misc.Stuff;
|
||||
|
||||
|
||||
public class TaskB extends SimpleSavingChelperSolution {
|
||||
|
||||
static class Fraction implements Comparable<Fraction> {
|
||||
public final long a;
|
||||
public final long b;
|
||||
|
||||
public Fraction(long a, long b) {
|
||||
if (b < 0) {
|
||||
a *= -1;
|
||||
b *= -1;
|
||||
}
|
||||
|
||||
if (b == 0) {
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
|
||||
if (a == 0) {
|
||||
this.a = 0;
|
||||
this.b = 1;
|
||||
return;
|
||||
}
|
||||
|
||||
long g = Math.abs(Stuff.gcd(a, b));
|
||||
this.a = a / g;
|
||||
this.b = b / g;
|
||||
}
|
||||
|
||||
public Fraction add(Fraction o) {
|
||||
return new Fraction(a * o.b + o.a * b, b * o.b);
|
||||
}
|
||||
|
||||
public Fraction negate() {
|
||||
return new Fraction(-a, b);
|
||||
}
|
||||
|
||||
public Fraction subtract(Fraction o) {
|
||||
return add(o.negate());
|
||||
}
|
||||
|
||||
public Fraction multiply(Fraction o) {
|
||||
return new Fraction(a * o.a, b * o.b);
|
||||
}
|
||||
|
||||
public Fraction inverse() {
|
||||
return new Fraction(b, a);
|
||||
}
|
||||
|
||||
public Fraction divide(Fraction o) {
|
||||
return multiply(o.inverse());
|
||||
}
|
||||
|
||||
public boolean isDivisibleBy(Fraction o) {
|
||||
Fraction d = divide(o);
|
||||
return d.b == 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(Fraction o) {
|
||||
Fraction d = subtract(o);
|
||||
return Long.compare(d.a, 0);
|
||||
}
|
||||
|
||||
public static Fraction gcd(Fraction a, Fraction b) {
|
||||
if (a.compareTo(b) < 0) {
|
||||
Fraction t = a;
|
||||
a = b;
|
||||
b = t;
|
||||
}
|
||||
|
||||
if (a.isDivisibleBy(b)) {
|
||||
return b;
|
||||
}
|
||||
|
||||
return gcd(a.subtract(b), b);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return a + " " + b;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
|
||||
Fraction fraction = (Fraction) o;
|
||||
|
||||
if (a != fraction.a) return false;
|
||||
return b == fraction.b;
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int result = (int) (a ^ a >>> 32);
|
||||
result = 31 * result + (int) (b ^ b >>> 32);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
public void solve(int testNumber, InputReader in, OutputWriter out) {
|
||||
wrapSolve(testNumber, in, out);
|
||||
}
|
||||
|
||||
public static BigInteger gcd(BigInteger a, BigInteger b) {
|
||||
if (a.equals(BigInteger.ZERO) || b.equals(BigInteger.ZERO)) {
|
||||
return a.max(b);
|
||||
}
|
||||
|
||||
while (!a.mod(b).equals(BigInteger.ZERO)) {
|
||||
a = a.mod(b);
|
||||
BigInteger t = a;
|
||||
a = b;
|
||||
b = t;
|
||||
}
|
||||
|
||||
return b;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void solve(int testNumber) {
|
||||
long M = 100;
|
||||
|
||||
// System.out.println(new Fraction(((long) (Math.random() * M)), ((long) (Math.random() * M))));
|
||||
// System.out.println(new Fraction(((long) (Math.random() * M)), ((long) (Math.random() * M))));
|
||||
// System.out.println(new Fraction(((long) (Math.random() * M)), ((long) (Math.random() * M))));
|
||||
// System.out.println(new Fraction(((long) (Math.random() * M)), ((long) (Math.random() * M))));
|
||||
// System.out.println(new Fraction(((long) (Math.random() * M)), ((long) (Math.random() * M))));
|
||||
// System.out.println(new Fraction(((long) (Math.random() * M)), ((long) (Math.random() * M))));
|
||||
// System.out.println(new Fraction(((long) (Math.random() * M)), ((long) (Math.random() * M))));
|
||||
// System.out.println(new Fraction(((long) (Math.random() * M)), ((long) (Math.random() * M))));
|
||||
|
||||
|
||||
Fraction a = new Fraction(in.nextInt(), in.nextInt());
|
||||
Fraction b = new Fraction(in.nextInt(), in.nextInt());
|
||||
|
||||
// Fraction g = Fraction.gcd(a, b);
|
||||
//
|
||||
// Fraction ans1 = a.multiply(b).divide(g);
|
||||
// out.println(ans1);
|
||||
|
||||
BigInteger aa = BigInteger.valueOf(a.a);
|
||||
BigInteger ab = BigInteger.valueOf(a.b);
|
||||
BigInteger ba = BigInteger.valueOf(b.a);
|
||||
BigInteger bb = BigInteger.valueOf(b.b);
|
||||
|
||||
BigInteger bg = gcd(ab, bb);
|
||||
BigInteger b1 = ab.multiply(bb).divide(bg);
|
||||
BigInteger a1 = b1.divide(ab).multiply(aa);
|
||||
BigInteger a2 = b1.divide(bb).multiply(ba);
|
||||
BigInteger ag = gcd(a1, a2);
|
||||
BigInteger lcm = a1.multiply(a2).divide(ag);
|
||||
|
||||
BigInteger g = gcd(lcm, b1);
|
||||
BigInteger ar = lcm.divide(g);
|
||||
BigInteger br = b1.divide(g);
|
||||
|
||||
out.println(ar + " " + br);
|
||||
|
||||
// if (!ans1.equals(ans2)) {
|
||||
// throw new RuntimeException();
|
||||
// }
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user