結果

問題 No.376 立方体のN等分 (2)
ユーザー KilisameKilisame
提出日時 2016-06-07 14:51:46
言語 Java21
(openjdk 21)
結果
AC  
実行時間 587 ms / 5,000 ms
コード長 5,396 bytes
コンパイル時間 2,542 ms
コンパイル使用メモリ 79,640 KB
実行使用メモリ 44,300 KB
最終ジャッジ日時 2024-04-24 14:59:40
合計ジャッジ時間 11,218 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 138 ms
41,956 KB
testcase_01 AC 163 ms
42,180 KB
testcase_02 AC 231 ms
42,548 KB
testcase_03 AC 229 ms
42,636 KB
testcase_04 AC 587 ms
42,524 KB
testcase_05 AC 139 ms
41,932 KB
testcase_06 AC 138 ms
42,264 KB
testcase_07 AC 145 ms
42,360 KB
testcase_08 AC 150 ms
42,116 KB
testcase_09 AC 157 ms
42,624 KB
testcase_10 AC 246 ms
44,300 KB
testcase_11 AC 135 ms
42,160 KB
testcase_12 AC 225 ms
42,500 KB
testcase_13 AC 219 ms
42,728 KB
testcase_14 AC 136 ms
42,096 KB
testcase_15 AC 158 ms
42,444 KB
testcase_16 AC 218 ms
43,576 KB
testcase_17 AC 197 ms
42,416 KB
testcase_18 AC 152 ms
42,344 KB
testcase_19 AC 232 ms
42,636 KB
testcase_20 AC 145 ms
42,324 KB
testcase_21 AC 217 ms
43,700 KB
testcase_22 AC 160 ms
42,444 KB
testcase_23 AC 223 ms
43,408 KB
testcase_24 AC 220 ms
43,460 KB
testcase_25 AC 149 ms
42,456 KB
testcase_26 AC 231 ms
43,988 KB
testcase_27 AC 145 ms
42,416 KB
testcase_28 AC 144 ms
42,256 KB
testcase_29 AC 150 ms
42,356 KB
testcase_30 AC 143 ms
42,168 KB
testcase_31 AC 261 ms
42,264 KB
testcase_32 AC 153 ms
42,468 KB
testcase_33 AC 270 ms
42,436 KB
testcase_34 AC 261 ms
42,376 KB
testcase_35 AC 154 ms
42,352 KB
testcase_36 AC 155 ms
42,440 KB
testcase_37 AC 144 ms
41,996 KB
testcase_38 AC 142 ms
41,792 KB
testcase_39 AC 254 ms
42,564 KB
権限があれば一括ダウンロードができます

ソースコード

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.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;
                        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;
                            }
                        }
                    }
                }
                if (minSlice == n - 1) {
                    /* 素数でないのに3乗根より小さい約数がないということは、3乗根より大きい2素数の積で表せる数ということなので、平方根で考える */
                    double root = Math.sqrt(n);
                    for (long i = (long) root; i > 1; i--) {
                        if (n % i == 0) {
                            minSlice = (i - 1) + (n / i - 1);
                        }
                    }
                }
            }
        }
        System.out.println(minSlice + " " + maxSlice);

    }
}
0