結果

問題 No.376 立方体のN等分 (2)
ユーザー KilisameKilisame
提出日時 2016-06-07 14:13:21
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 4,240 bytes
コンパイル時間 2,248 ms
コンパイル使用メモリ 81,968 KB
実行使用メモリ 56,980 KB
最終ジャッジ日時 2024-04-17 05:38:26
合計ジャッジ時間 9,094 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 144 ms
42,268 KB
testcase_01 AC 136 ms
41,720 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 AC 138 ms
42,072 KB
testcase_06 AC 153 ms
42,560 KB
testcase_07 AC 138 ms
42,180 KB
testcase_08 AC 139 ms
41,780 KB
testcase_09 AC 136 ms
42,164 KB
testcase_10 AC 145 ms
42,444 KB
testcase_11 AC 141 ms
42,160 KB
testcase_12 AC 141 ms
41,760 KB
testcase_13 AC 147 ms
42,180 KB
testcase_14 AC 144 ms
41,772 KB
testcase_15 AC 155 ms
42,728 KB
testcase_16 AC 130 ms
42,184 KB
testcase_17 AC 153 ms
42,260 KB
testcase_18 AC 156 ms
42,656 KB
testcase_19 WA -
testcase_20 AC 152 ms
42,584 KB
testcase_21 AC 135 ms
42,132 KB
testcase_22 AC 136 ms
41,932 KB
testcase_23 AC 142 ms
42,500 KB
testcase_24 AC 150 ms
42,300 KB
testcase_25 AC 159 ms
42,716 KB
testcase_26 AC 144 ms
42,136 KB
testcase_27 AC 146 ms
42,488 KB
testcase_28 AC 144 ms
42,564 KB
testcase_29 AC 140 ms
42,136 KB
testcase_30 AC 146 ms
42,300 KB
testcase_31 WA -
testcase_32 AC 151 ms
42,520 KB
testcase_33 WA -
testcase_34 WA -
testcase_35 AC 153 ms
42,496 KB
testcase_36 AC 153 ms
42,520 KB
testcase_37 AC 146 ms
42,216 KB
testcase_38 AC 148 ms
42,496 KB
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が素数か判定する */
        boolean isPrime = new BigInteger(String.valueOf(n)).isProbablePrime(8);
        if (isPrime) {
            minSlice = n - 1;
        } else {
            double thirdRoot = Math.cbrt(n);
            if (thirdRoot == (long) thirdRoot) {
                /* たまたまNが整数の3乗だった場合は、(thirdRoot-1)*3が答え */
                minSlice = ((long) thirdRoot - 1) * 3;
            } else {
                /* 3乗根が整数にならないのであれば、それより小さい値から順に割っていき、3乗根以下でもっとも大きい約数を求める */
                long divisorUnderThirdRoot = n;
                for (long i = (long) thirdRoot; i > 1; i--) {
                    if (n % i == 0) {
                        divisorUnderThirdRoot = i;
                        break;
                    }
                }
                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);
                    } else {
                        /* 約数側をさらに2数の積で分割する。約数の平方根をとる */
                        double rootOfDivisor = Math.sqrt(divisorUnderThirdRoot);
                        if (rootOfDivisor == (long) rootOfDivisor) {
                            /* 平方根が整数だった場合は、(root-1)*2に(rest-1)を足したものが答え */
                            minSlice = ((long) rootOfDivisor - 1) * 2 + (rest - 1);
                        } else {
                            long divisorOverRoot = divisorUnderThirdRoot;
                            for (long i = (long) rootOfDivisor + 1; i < divisorUnderThirdRoot; i++) {
                                if (divisorUnderThirdRoot % i == 0) {
                                    divisorOverRoot = i;
                                    break;
                                }
                            }
                            long result = divisorUnderThirdRoot / divisorOverRoot;
                            minSlice = (rest - 1) + (divisorOverRoot - 1) + (result - 1);
                        }
                    }
                } else {
                    /* 残った数が素数でないなら、そちらをさらに2数の積の形にする。 */
                    double rootOfRest = Math.sqrt(rest);
                    if (rootOfRest == (long) rootOfRest) {
                        minSlice = ((long) rootOfRest - 1) * 2 + (divisorUnderThirdRoot - 1);
                    } else {
                        long divisorOfRoot = rest;
                        for (long i = (long) rootOfRest; i > 1; i--) {
                            if (rest % i == 0) {
                                divisorOfRoot = i;
                                break;
                            }
                        }
                        long result = rest / divisorOfRoot;
                        minSlice = (divisorOfRoot - 1) + (result - 1) + (divisorUnderThirdRoot - 1);
                    }
                }
            }
        }
        System.out.println(minSlice + " " + maxSlice);

    }
}
0