{"id":271711,"date":"2016-01-07T01:38:02","date_gmt":"2016-01-06T22:38:02","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=271711"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=271711","title":{"rendered":"\u041d\u0435\u043c\u043d\u043e\u0433\u043e \u0441\u0430\u0445\u0430\u0440\u0430 \u0432 \u043a\u043e\u043c\u0431\u0438\u043d\u0430\u0442\u043e\u0440\u0438\u043a\u0435"},"content":{"rendered":"<p>       \u0414\u043e\u0431\u0440\u043e\u0433\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u0441\u0443\u0442\u043e\u043a, \u0445\u0430\u0431\u0440!<\/p>\n<p>  \u041a\u0430\u0436\u0434\u044b\u0439 \u0443\u0432\u0430\u0436\u0430\u044e\u0449\u0438\u0439 \u0441\u0435\u0431\u044f \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0441\u0442 \u0437\u043d\u0430\u0435\u0442, \u0447\u0442\u043e \u0433\u043b\u0443\u0431\u043e\u043a\u0438\u0435 \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u043e\u0441\u0442\u0438 \u2014 \u043f\u043b\u043e\u0445\u043e\u0439 \u0441\u0442\u0438\u043b\u044c. \u041d\u043e \u0435\u0441\u0442\u044c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u044b, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u044e\u0442\u0441\u044f \u043a\u0430\u0441\u043a\u0430\u0434\u043e\u043c \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u044b\u0445 \u0446\u0438\u043a\u043b\u043e\u0432 (3 \u0438 \u0431\u043e\u043b\u0435\u0435). \u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u044f \u0445\u043e\u0447\u0443 \u0440\u0430\u0441\u0441\u043a\u0430\u0437\u0430\u0442\u044c, \u043a\u0430\u043a \u043c\u043e\u0436\u043d\u043e \u0441\u043f\u0440\u0430\u0432\u0438\u0442\u044c\u0441\u044f \u0441 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u043e\u0439 \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u044b\u0445 \u0446\u0438\u043a\u043b\u043e\u0432 \u043f\u0440\u0438 \u043f\u0435\u0440\u0435\u0431\u043e\u0440\u0435 \u043a\u043e\u043c\u0431\u0438\u043d\u0430\u0446\u0438\u0439 \u043d\u0430 \u043b\u044e\u0431\u0438\u043c\u043e\u043c \u044f\u0437\u044b\u043a\u0435 D.<br \/>  <a name=\"habracut\"><\/a><br \/>  \u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0441\u0430\u043c\u0443\u044e \u043f\u0440\u043e\u0441\u0442\u0443\u044e \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u044e.<\/p>\n<p>  \u0414\u0430\u043d\u043e: N \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432<br \/>  \u041d\u0443\u0436\u043d\u043e: \u043f\u0435\u0440\u0435\u0431\u0440\u0430\u0442\u044c \u0432\u0441\u0435 \u043d\u0430\u0431\u043e\u0440\u044b \u043f\u043e K \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0431\u0435\u0437 \u043f\u043e\u0432\u0442\u043e\u0440\u0435\u043d\u0438\u0439<\/p>\n<p>  \u0412 \u043a\u043e\u043c\u0431\u0438\u043d\u0430\u0442\u043e\u0440\u0438\u043a\u0435 \u044d\u0442\u043e \u043d\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0440\u0430\u0437\u043c\u0435\u0449\u0435\u043d\u0438\u0435\u043c \u0438\u0437 N \u043f\u043e K.<\/p>\n<p>  \u0421\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u0430\u044f \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430 \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0444\u0443\u043d\u043a\u0446\u0438\u044e std.algorithm.permutations, \u043d\u043e \u044d\u0442\u043e \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u0434\u0440\u0443\u0433\u043e\u0435 \u2014 \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0430 \u043f\u043e \u0440\u0443\u0441\u0441\u043a\u0438.<\/p>\n<p>  \u0420\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u043f\u0435\u0440\u0435\u0431\u043e\u0440 N \u043f\u043e K:  <\/p>\n<pre><code>import std.range; import std.algorithm;  auto partialPermutation(R)( R r, size_t k )     if( isInputRange!R ) {     static struct Result     {         R[] r, orig; \/\/ \u0445\u0440\u0430\u043d\u0438\u043c \u0442\u0435\u043a\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0438 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0435          this( R[] r ) { this.r = r; orig = r.dup; }          bool empty() @property { return r[0].empty; }          void popFront()         {             foreach_reverse( ref x; r )             {                 x.popFront();                 if( !x.empty ) break;             }              \/\/ \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u043c \u043f\u0443\u0441\u0442\u044b\u0435 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u044b             foreach( i, ref x; r[1..$] )             {                 if( x.empty )                     x = orig[i+1];             }         }          auto front() @property { return r.map!(a=&gt;a.front); }     }      auto rr = new R[](k);     rr[] = r;      return Result( rr ); } <\/code><\/pre>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u044d\u0442\u0443 \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0434\u043b\u044f \u043f\u0435\u0440\u0435\u0431\u043e\u0440\u0430 \u0432\u0441\u0435\u0445 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0445 \u043a\u043e\u043c\u0431\u0438\u043d\u0430\u0446\u0438\u0439:  <\/p>\n<pre><code>foreach( v; partialPermutation( iota(6), 3 ) )     writefln( &quot;%d %d %d&quot;, v[0], v[1], v[2] ); <\/code><\/pre>\n<p>  \u041f\u0440\u0438 \u0442\u0430\u043a\u043e\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0432\u0441\u0442\u0440\u0435\u0447\u0430\u044e\u0442\u0441\u044f \u043f\u043e\u0432\u0442\u043e\u0440\u0435\u043d\u0438\u044f, \u044d\u0442\u043e \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u043e \u043b\u0435\u0447\u0438\u0442\u0441\u044f:  <\/p>\n<pre><code>auto uniqPartialPermutation(R)( R r, size_t k )     if( isInputRange!R ) {     bool noDups(T)( T v ) pure     {         foreach( i; 0 .. v.length )             if( v[i+1..$].canFind( v[i] ) ) return false;         return true;     }     return partialPermutation(r,k).filter!(a=&gt;noDups(a)); } <\/code><\/pre>\n<p>  \u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0431\u043e\u043b\u0435\u0435 \u0441\u043b\u043e\u0436\u043d\u044b\u0439 \u043f\u0440\u0438\u043c\u0435\u0440.<\/p>\n<p>  \u0414\u0430\u043d\u043e: N \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0445 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u043e\u0432 \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0445 \u0442\u0438\u043f\u043e\u0432 \u0434\u0430\u043d\u043d\u044b\u0445<br \/>  \u041d\u0443\u0436\u043d\u043e: \u043f\u0435\u0440\u0435\u0431\u0440\u0430\u0442\u044c \u043d\u0430\u0431\u043e\u0440\u044b \u0438\u0437 \u0432\u0441\u0435\u0445 \u043a\u043e\u043c\u0431\u0438\u043d\u0430\u0446\u0438\u0439 \u044d\u0442\u0438\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432<\/p>\n<p>  \u042f\u0437\u044b\u043a D \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u043d\u0430\u043c \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0432\u043d\u0435\u0441\u0442\u0438 \u0432 \u0440\u0430\u0431\u043e\u0447\u0438\u0439 \u043a\u043e\u0434 \u043b\u0438\u0448\u044c \u043f\u0430\u0440\u0443 \u043c\u0430\u043b\u0435\u043d\u044c\u043a\u0438\u0445 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0439<br \/>  \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0436\u0435\u043b\u0430\u0435\u043c\u043e\u0433\u043e \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430:  <\/p>\n<pre><code>auto combinations(R...)( R rngs )     if( allSatisfy!(isInputRange,R) ) {     static struct Result     {         R r, orig; \/\/ \u0445\u0440\u0430\u043d\u0438\u043c \u0442\u0435\u043a\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0438 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0435          this( R r ) { this.r = r; orig = r; }          bool empty() @property { return r[0].empty; }          void popFront()         {             foreach_reverse( ref x; r )             {                 x.popFront();                 if( !x.empty ) break;             }              foreach( i, ref x; r[1..$] )             {                 if( x.empty )                     x = orig[i+1];             }         }          auto front() @property { return getFronts( r ); }      }      return Result( rngs ); } <\/code><\/pre>\n<p>  \u0412\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f <code>getFronts<\/code> \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043a\u043e\u0440\u0442\u0435\u0436 \u043f\u0435\u0440\u0432\u044b\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432:  <\/p>\n<pre><code>auto getFronts(R...)( R r )     if( allSatisfy!(isInputRange,R) ) {     static if( R.length == 1 ) return tuple( r[0].front );     else static if( R.length &gt; 1 )         return tuple( getFronts(r[0]).expand, getFronts(r[1..$]).expand );     else static assert(0, &quot;no ranges - no fronts&quot; ); } <\/code><\/pre>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0442\u0430\u043a:  <\/p>\n<pre><code>foreach( a,b,c; combinations(iota(3),[&quot;yes&quot;,&quot;no&quot;],&quot;xyz&quot;))     writeln( a,&quot;[&quot;,b,&quot;]&quot;,c ); <\/code><\/pre>\n<p>  \u0414\u043b\u044f \u043f\u043e\u043b\u043d\u043e\u0442\u044b \u043a\u0430\u0440\u0442\u0438\u043d\u044b \u0440\u0430\u0441\u0448\u0438\u0440\u0438\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u044c, \u0447\u0442\u043e\u0431\u044b \u043d\u0430\u0448\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u044f <code>combinations<\/code><br \/>  \u043c\u043e\u0433\u043b\u0430 \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0442\u044c \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u044b, \u043d\u043e \u0438 \u043a\u043e\u0440\u0442\u0435\u0436\u0438 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u043e\u0432. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043f\u0435\u0440\u0435\u0438\u043c\u0435\u043d\u0443\u0435\u043c \u0435\u0451<br \/>  \u0432 <code>result<\/code> \u0438 \u043f\u043e\u043c\u0435\u0441\u0442\u0438\u043c \u0432\u043d\u0443\u0442\u0440\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0441 \u0442\u0430\u043a\u0438\u043c \u0436\u0435 \u0438\u043c\u0435\u043d\u0435\u043c, \u043d\u043e \u0431\u0435\u0437 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f \u0441\u0438\u0433\u043d\u0430\u0442\u0443\u0440\u044b \u0438<br \/>  \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u0440\u0430\u0437\u0432\u043e\u0440\u0430\u0447\u0438\u0432\u0430\u043d\u0438\u0435 \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u044b\u0445 \u043a\u043e\u0440\u0442\u0435\u0436\u0435\u0439:<\/p>\n<pre><code>auto combinations(T...)( T tpls ) {     auto result(R...)( R rrr )         if( allSatisfy!(isInputRange,R) )     {         ...         \u0441\u0442\u0430\u0440\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f combinations         ...     }      auto wrapTuples(X...)( X t ) pure     {         static if( X.length == 1 )         {             static if( isTuple!(X[0]) )                 return wrapTuples( t[0].expand );             else                 return tuple( t[0] );         }         else static if( X.length &gt; 1 )             return tuple( wrapTuples(t[0]).expand, wrapTuples(t[1..$]).expand );     }      return result( wrapTuples(tpls).expand ); } <\/code><\/pre>\n<pre><code>auto tt = tuple( hCube!3(iota(2)), iota(3) ); foreach( a, b, c, d, e, f; combinations( tt, [&quot;yes&quot;, &quot;no&quot;, &quot;maybe&quot;], &quot;abc&quot; ) )     writefln( &quot;%s.%s.%s(%s) %s %s&quot;, a, b, c, d, e, f ); <\/code><\/pre>\n<p>  \u0433\u0434\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u044f <code>hCube<\/code> \u043f\u0440\u043e\u0441\u0442\u043e \u0434\u0443\u0431\u043b\u0438\u0440\u0443\u0435\u0442 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u0437\u0430\u0434\u0430\u043d\u043d\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0440\u0430\u0437:  <\/p>\n<pre><code>auto hCube(size_t N,R)( R r ) {     static if( N == 0 ) return tuple();     static if( N == 1 ) return tuple(r);     else return tuple( r, hCube!(N-1)(r).expand ); } <\/code><\/pre>\n<p>  \u041d\u0430 \u044d\u0442\u043e\u043c \u0432\u0441\u0451. \u0421\u0442\u043e\u0438\u0442 \u0434\u0435\u0440\u0436\u0430\u0442\u044c \u0432 \u0433\u043e\u043b\u043e\u0432\u0435 \u043e\u0434\u0438\u043d \u043c\u043e\u043c\u0435\u043d\u0442: \u0443\u043c\u0435\u043d\u044c\u0448\u0435\u043d\u0438\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0430 foreach \u043d\u0435 \u043c\u0435\u043d\u044f\u0435\u0442 \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 \u0432 \u044d\u0442\u043e\u0439 \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u0438. \u041f\u0440\u043e\u0441\u0442\u043e \u0435\u0449\u0451 \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u0441\u0430\u0445\u0430\u0440\u0430.               <\/p>\n<div class=\"clear\"><\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"http:\/\/habrahabr.ru\/post\/274619\/\"> http:\/\/habrahabr.ru\/post\/274619\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>       \u0414\u043e\u0431\u0440\u043e\u0433\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u0441\u0443\u0442\u043e\u043a, \u0445\u0430\u0431\u0440!<\/p>\n<p>  \u041a\u0430\u0436\u0434\u044b\u0439 \u0443\u0432\u0430\u0436\u0430\u044e\u0449\u0438\u0439 \u0441\u0435\u0431\u044f \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0441\u0442 \u0437\u043d\u0430\u0435\u0442, \u0447\u0442\u043e \u0433\u043b\u0443\u0431\u043e\u043a\u0438\u0435 \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u043e\u0441\u0442\u0438 \u2014 \u043f\u043b\u043e\u0445\u043e\u0439 \u0441\u0442\u0438\u043b\u044c. \u041d\u043e \u0435\u0441\u0442\u044c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u044b, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u044e\u0442\u0441\u044f \u043a\u0430\u0441\u043a\u0430\u0434\u043e\u043c \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u044b\u0445 \u0446\u0438\u043a\u043b\u043e\u0432 (3 \u0438 \u0431\u043e\u043b\u0435\u0435). \u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u044f \u0445\u043e\u0447\u0443 \u0440\u0430\u0441\u0441\u043a\u0430\u0437\u0430\u0442\u044c, \u043a\u0430\u043a \u043c\u043e\u0436\u043d\u043e \u0441\u043f\u0440\u0430\u0432\u0438\u0442\u044c\u0441\u044f \u0441 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u043e\u0439 \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u044b\u0445 \u0446\u0438\u043a\u043b\u043e\u0432 \u043f\u0440\u0438 \u043f\u0435\u0440\u0435\u0431\u043e\u0440\u0435 \u043a\u043e\u043c\u0431\u0438\u043d\u0430\u0446\u0438\u0439 \u043d\u0430 \u043b\u044e\u0431\u0438\u043c\u043e\u043c \u044f\u0437\u044b\u043a\u0435 D.  <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-271711","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/271711","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=271711"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/271711\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=271711"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=271711"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=271711"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}