結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 146 ms
42,376 KB
testcase_01 AC 145 ms
42,316 KB
testcase_02 AC 216 ms
42,584 KB
testcase_03 AC 210 ms
42,648 KB
testcase_04 AC 541 ms
42,688 KB
testcase_05 AC 152 ms
42,172 KB
testcase_06 AC 139 ms
42,428 KB
testcase_07 AC 152 ms
42,492 KB
testcase_08 AC 143 ms
41,996 KB
testcase_09 AC 174 ms
42,692 KB
testcase_10 AC 242 ms
43,820 KB
testcase_11 AC 136 ms
42,000 KB
testcase_12 AC 222 ms
42,700 KB
testcase_13 AC 212 ms
42,500 KB
testcase_14 AC 141 ms
42,068 KB
testcase_15 AC 154 ms
42,572 KB
testcase_16 AC 214 ms
43,284 KB
testcase_17 AC 201 ms
42,644 KB
testcase_18 AC 155 ms
42,396 KB
testcase_19 AC 233 ms
42,552 KB
testcase_20 AC 146 ms
42,124 KB
testcase_21 AC 212 ms
43,492 KB
testcase_22 AC 153 ms
42,688 KB
testcase_23 AC 210 ms
43,232 KB
testcase_24 AC 214 ms
43,444 KB
testcase_25 AC 159 ms
42,648 KB
testcase_26 AC 241 ms
44,104 KB
testcase_27 AC 153 ms
42,420 KB
testcase_28 AC 144 ms
42,184 KB
testcase_29 AC 150 ms
42,580 KB
testcase_30 AC 144 ms
42,304 KB
testcase_31 WA -
testcase_32 AC 150 ms
42,588 KB
testcase_33 WA -
testcase_34 WA -
testcase_35 AC 152 ms
42,412 KB
testcase_36 AC 134 ms
42,048 KB
testcase_37 AC 145 ms
42,432 KB
testcase_38 AC 139 ms
42,236 KB
testcase_39 AC 240 ms
42,616 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(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;
                        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