結果

問題 No.4 おもりと天秤
ユーザー papinianuspapinianus
提出日時 2016-07-29 14:56:37
言語 PHP
(8.3.4)
結果
AC  
実行時間 100 ms / 5,000 ms
コード長 999 bytes
コンパイル時間 815 ms
コンパイル使用メモリ 18,608 KB
実行使用メモリ 20,968 KB
最終ジャッジ日時 2023-09-08 16:47:34
合計ジャッジ時間 1,896 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 14 ms
18,784 KB
testcase_01 AC 15 ms
18,752 KB
testcase_02 AC 15 ms
18,820 KB
testcase_03 AC 15 ms
18,900 KB
testcase_04 AC 15 ms
18,660 KB
testcase_05 AC 15 ms
18,820 KB
testcase_06 AC 15 ms
18,892 KB
testcase_07 AC 15 ms
18,912 KB
testcase_08 AC 15 ms
18,792 KB
testcase_09 AC 16 ms
18,820 KB
testcase_10 AC 15 ms
18,864 KB
testcase_11 AC 15 ms
18,744 KB
testcase_12 AC 16 ms
18,904 KB
testcase_13 AC 15 ms
18,920 KB
testcase_14 AC 15 ms
18,656 KB
testcase_15 AC 15 ms
18,808 KB
testcase_16 AC 16 ms
18,808 KB
testcase_17 AC 15 ms
18,912 KB
testcase_18 AC 100 ms
20,968 KB
testcase_19 AC 15 ms
18,844 KB
testcase_20 AC 15 ms
18,656 KB
testcase_21 AC 15 ms
18,916 KB
testcase_22 AC 15 ms
18,912 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
No syntax errors detected in Main.php

ソースコード

diff #

<?php
$num = trim(fgets(STDIN));
$nums = explode(" ", trim(fgets(STDIN)));
$sum = array_sum($nums);
if($sum%2 == 0) {
    $halfSum = $sum/2;
    $memo = [];
    rsort($nums);
    echo getCombination($halfSum,0)?"possible":"impossible";
} else {
    echo "impossible";
}
echo PHP_EOL;

function getCombination($target, $index) {
    global $nums;
    global $memo;
    if(!isset($memo[$target][$index])) {
        if($index >= count($nums)) {
            $memo[$target][$index] = false;
        } else if(array_sum(array_slice($nums,$index)) < $target) {
            $memo[$target][$index] = false;
        } else if($nums[$index] == $target) {
            $memo[$target][$index] = true;
        } else if($target > $nums[$index]) {
            $memo[$target][$index] = !getCombination($target-$nums[$index], $index+1)?getCombination($target, $index+1):true;
        } else {
            $memo[$target][$index] = getCombination($target, $index+1);
        }
    }
    return $memo[$target][$index];
}
0