結果

問題 No.550 夏休みの思い出(1)
ユーザー Grenache
提出日時 2017-07-28 23:23:38
言語 Java
(openjdk 23)
結果
AC  
実行時間 787 ms / 2,000 ms
コード長 2,473 bytes
コンパイル時間 4,061 ms
コンパイル使用メモリ 81,748 KB
実行使用メモリ 61,632 KB
最終ジャッジ日時 2024-10-11 05:33:11
合計ジャッジ時間 24,596 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 55
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.math.*;
import java.util.*;


public class Main_yukicoder550 {

	private static Scanner sc;
	private static Printer pr;

	private static void solve() {
		long a = sc.nextLong();
		long b = sc.nextLong();
		long c = sc.nextLong();
		long d;
		long e;

		List<Long> ans = new ArrayList<>(3);
		if (c == 0) {
			ans.add(0L);

			d = a;
			e = b;
		} else {
			for (long x = 1; x <= 1_000_000; x++) {
				if (isOK(x, a, b, c)) {
					ans.add(x);
					break;
				}
				if (isOK(-x, a, b, c)) {
					ans.add(-x);
					break;
				}
			}

			e = -c / ans.get(0);
			d = a + ans.get(0);
		}

		BigInteger bd = BigInteger.valueOf(d);
		BigInteger be = BigInteger.valueOf(e);

		BigInteger dd = bd.multiply(bd).subtract(BigInteger.valueOf(4).multiply(be));
		BigInteger[] sqrtdd = sqrt(dd);

		BigInteger ans1 = bd.negate().add(sqrtdd[0]).divide(BigInteger.valueOf(2));
		BigInteger ans2 = bd.negate().subtract(sqrtdd[0]).divide(BigInteger.valueOf(2));

		ans.add(ans1.longValue());
		ans.add(ans2.longValue());

		Collections.sort(ans);

		for (int i = 0; i < 3; i++) {
			if (i > 0) {
				pr.print(' ');
			}
			pr.print(ans.get(i));
		}
		pr.println();
	}

	private static boolean isOK(long x, long a, long b, long c) {
		BigInteger bx = BigInteger.valueOf(x);

		BigInteger f = bx.multiply(bx).multiply(bx).add(bx.multiply(bx).multiply(BigInteger.valueOf(a)).add(bx.multiply(BigInteger.valueOf(b))).add(BigInteger.valueOf(c)));

		return f.compareTo(BigInteger.ZERO) == 0;
	}

	private static BigInteger[] sqrt(BigInteger x) {
		BigInteger number = x;
		BigInteger a = BigInteger.ONE;
		BigInteger b = number.shiftRight(5).add(BigInteger.valueOf(8));
		while (b.compareTo(a) >= 0) {
		    BigInteger mid = a.add(b).shiftRight(1);
		    if (mid.multiply(mid).compareTo(number) > 0) {
		        b = mid.subtract(BigInteger.ONE);
		    } else {
		        a = mid.add(BigInteger.ONE);
		    }
		}
		BigInteger sqrtNumber = a.subtract(BigInteger.ONE);
		BigInteger remainderNumber = number.subtract(sqrtNumber.multiply(sqrtNumber));

		BigInteger[] ret = new BigInteger[2];
		ret[0] = sqrtNumber;
		ret[1] = remainderNumber;

		return ret;
	}

	// ---------------------------------------------------
	public static void main(String[] args) {
		sc = new Scanner(System.in);
		pr = new Printer(System.out);

		solve();

		pr.close();
		sc.close();
	}

	private static class Printer extends PrintWriter {
		Printer(PrintStream out) {
			super(out);
		}
	}
}
0