結果

問題 No.376 立方体のN等分 (2)
ユーザー KilisameKilisame
提出日時 2016-06-07 14:41:27
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 4,459 bytes
コンパイル時間 2,268 ms
コンパイル使用メモリ 86,128 KB
実行使用メモリ 63,368 KB
最終ジャッジ日時 2024-04-17 05:40:23
合計ジャッジ時間 14,630 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 144 ms
42,300 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 AC 149 ms
42,224 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 AC 151 ms
42,172 KB
testcase_37 AC 143 ms
42,180 KB
testcase_38 WA -
testcase_39 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.math.BigInteger;
import java.util.Scanner;

public class Main{

    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        String line = null;
        while (cin.hasNext()) {
            line = cin.nextLine();
        }
        cin.close();
        divided(Long.parseLong(line));
    }

    private static void divided(long n) {
        long maxSlice = n - 1;
        long minSlice = n - 1;
        /* nが素数か判定する */
        boolean isPrime = new BigInteger(String.valueOf(n)).isProbablePrime(15);
        if (isPrime) {
            minSlice = n - 1;
        } else {
            double thirdRoot = Math.sqrt(n);
            System.out.println("thirdRoot=" + thirdRoot);
            /* 3乗根が整数にならないのであれば、それより小さい値から順に割っていき、3乗根以下でもっとも大きい約数を求める */
            long divisorUnderThirdRoot = n;
            for (long i = (long) thirdRoot; i > 1; i--) {
                if (n % i == 0) {
                    divisorUnderThirdRoot = i;
                    long rest = n / divisorUnderThirdRoot;
                    /* 割った残りの値が素数かを判定する */
                    boolean isPrimeOfRest = new BigInteger(String.valueOf(rest)).isProbablePrime(8);
                    if (isPrimeOfRest) {
                        /* 残りの数が素数なのであれば、約数側がさらに2数の積にならないかを確認する */
                        boolean isPrimeOfDivisor = new BigInteger(String.valueOf(divisorUnderThirdRoot)).isProbablePrime(8);
                        if (isPrimeOfDivisor) {
                            /* Nが素数×素数の形になってしまった場合、それ以上の分割は無理 */
                            minSlice = (divisorUnderThirdRoot - 1) + (rest - 1);
                            break;
                        } else {
                            /* 約数側をさらに2数の積で分割する。約数の平方根をとる */
                            double rootOfDivisor = Math.sqrt(divisorUnderThirdRoot);
                            if (rootOfDivisor == (long) rootOfDivisor) {
                                /* 平方根が整数だった場合は、(root-1)*2に(rest-1)を足したものが答え */
                                minSlice = ((long) rootOfDivisor - 1) * 2 + (rest - 1);
                                break;
                            } else {
                                long divisorOverRoot = divisorUnderThirdRoot;
                                for (long j = (long) rootOfDivisor; j > 1; j--) {
                                    if (divisorUnderThirdRoot % j == 0) {
                                        divisorOverRoot = j;
                                        break;
                                    }
                                }
                                long result = divisorUnderThirdRoot / divisorOverRoot;
                                minSlice = (rest - 1) + (divisorOverRoot - 1) + (result - 1);
                                break;
                            }
                        }
                    } else {
                        /* 残った数が素数でないなら、そちらをさらに2数の積の形にする。 */
                        double rootOfRest = Math.sqrt(rest);
                        long tempMinSlice;
                        if (rootOfRest == (long) rootOfRest) {
                            tempMinSlice = ((long) rootOfRest - 1) * 2 + (divisorUnderThirdRoot - 1);
                        } else {
                            long divisorOfRoot = rest;
                            for (long j = (long) rootOfRest; j > 1; j--) {
                                if (rest % j == 0) {
                                    divisorOfRoot = j;
                                    break;
                                }
                            }
                            long result = rest / divisorOfRoot;
                            tempMinSlice = (divisorOfRoot - 1) + (result - 1) + (divisorUnderThirdRoot - 1);
                        }
                        if (tempMinSlice < minSlice) {
                            minSlice = tempMinSlice;
                        }
                    }
                }
            }
        }
        System.out.println(minSlice + " " + maxSlice);

    }
}
0