public static function shift($array, $num)
    {
        $num = abs($num) + 1;
        $n = count($array);
        $used = array_fill(0, $n, false);
        $res = [];

        $factorial = self::factorial($n);
        if ($num > $factorial) {
            $num = $num % $factorial;
            if ($num == 0) {
                $num = $factorial - $num;
            }
        }

        for ($i = 1; $i <= $n; $i++) {
            $factorial = self::factorial($n - $i);
            $blockNum = intval(($num - 1) / $factorial + 1);

            $pos = 0;
            for ($j = 1; $j < count($used); $j++) {
                if (!$used[$j]) $pos++;
                if ($blockNum == $pos) break;
            }

            $res[$i - 1] = $j - 1;
            $used[$j] = true;
            $num = intval(($num - 1) % $factorial) + 1;
        }

        return $res;
    }