<?php test(); function test() { $test = ['a','b','c','d']; $N = 3; $r = getSequenses( $N, $test); } function getSequenses( $N, $arr) { if( $L > 32) throw new Exception("Массив длиннее 32 элементов"); $result = []; if( $N != countBits( $i)) continue; // get all permutations $bits = $i; for( $j=0; $bits; $j++) { if( !($bits & 1)) { $bits = $bits >> 1; continue; } $bits = $bits >> 1; } } return $result; } } else { $newitems = $items; $newperms = $perms; permute( $newitems, $newperms, $result); } } return $result; } function countBits( $n) { $count = 0; while( $n) { $count += ( $n & 1); $n = $n >> 1; } return $count; }
Standard input is empty
Array ( [0] => a, b, c [1] => b, a, c [2] => a, c, b [3] => c, a, b [4] => b, c, a [5] => c, b, a [6] => a, b, d [7] => b, a, d [8] => a, d, b [9] => d, a, b [10] => b, d, a [11] => d, b, a [12] => a, c, d [13] => c, a, d [14] => a, d, c [15] => d, a, c [16] => c, d, a [17] => d, c, a [18] => b, c, d [19] => c, b, d [20] => b, d, c [21] => d, b, c [22] => c, d, b [23] => d, c, b )