結果

問題 No.4 おもりと天秤
ユーザー papinianus
提出日時 2016-07-29 14:56:37
言語 PHP
(843.2)
結果
AC  
実行時間 165 ms / 5,000 ms
コード長 999 bytes
コンパイル時間 1,443 ms
コンパイル使用メモリ 32,144 KB
実行使用メモリ 33,164 KB
最終ジャッジ日時 2024-06-26 09:37:12
合計ジャッジ時間 1,953 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 23
権限があれば一括ダウンロードができます
コンパイルメッセージ
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