Given the numbers N and K, print all lines of zeros and units of length N, containing exactly K units, in lexicographical order.

Input Format

Two numbers are given: N and K (0≀K≀N, 0≀N≀100)

Output format

It is necessary to print all the lines of zeros and units of length N, containing exactly K units, in lexicographical order. It is guaranteed that the response size does not exceed 10MiB.

Example

Π’Π²ΠΎΠ΄ Π’Ρ‹Π²ΠΎΠ΄ 4 2 0011 0101 0110 1001 1010 1100 

Here is my idea (my idea passes 59 of 62 tests, 3 tests work for more than 1 second)

  var kol : array[1..101] of Byte; n,k : Byte; s : String[120]; procedure vivod(sum:Byte); var i:Byte; begin Write(copy(s,1,n-(sum+k))); // Π²Ρ‹Π²ΠΎΠ΄ Π²Π΅Π΄ΡƒΡ‰ΠΈΡ… Π½ΡƒΠ»Π΅ΠΉ for i := 1 to k do Write('1',copy(s,1,kol[i])); //Π²Ρ‹Π²ΠΎΠ΄ 1 Π° слСдом kol[i] - ΠΊΠΎΠ»-Π²ΠΎ Π½ΡƒΠ»Π΅ΠΉ ΠΈΠ΄ΡƒΡ‰ΠΈΡ… Π·Π° этой Π΅Π΄ΠΈΠ½ΠΈΡ†Π΅ΠΉ WriteLn; end; procedure perebor; var loc,sum,i,id,x:Byte; begin sum:=0; vivod(sum); //Π²Ρ‹Π²ΠΎΠ΄ Π½Π°Ρ‡Π°Π»ΡŒΠ½ΠΎ пСрСстановки (000...1111) while kol[k]<nk do //ΠΊΠΎΠ³Π΄Π° послС послСднСй Π΅Π΄ΠΈΠ½ΠΈΡ†Ρ‹ Π±ΡƒΠ΄Π΅Ρ‚ ΡΡ‚ΠΎΡΡ‚ΡŒ nk Π½ΡƒΠ»Π΅ΠΉ, Ρ‚.Π΅. большС Π½ΡƒΠ»Π΅ΠΉ Π½Π΅ ΠΌΠΎΠΆΠ΅Ρ‚ ΡΡ‚ΠΎΡΡ‚ΡŒ, Π·Π°Π²Π΅Ρ€ΡˆΠΈΡ‚ΡŒ ΠΏΠ΅Ρ€Π΅Π±ΠΎΡ€ begin inc(sum);// ΠΊΠΎΠ»-Π²ΠΎ Π½ΡƒΠ»Π΅ΠΉ ΠΊΠΎΡ‚ΠΎΡ€Ρ‹Ρ… ставится ΠΏΠ΅Ρ€Π²ΠΎΠ½ΠΎΡ‡Π°Π»ΡŒΠ½ΠΎ послС 1 Π΅Π΄ΠΈΠ½ΠΈΡ†Ρ‹ i := 1; fillchar(kol,n,0); //Ρƒ всСх Π΄Ρ€ΡƒΠ³ΠΈΡ… Π΅Π΄ΠΈΠ½ΠΈΡ† ΠΊΠΎΠ»-Π²ΠΎ Π½ΡƒΠ»Π΅ΠΉ 0 kol[1] := sum; // послС ΠΏΠ΅Ρ€Π²ΠΎΠΉ Π΅Π΄ΠΈΠ½ΠΈΡ†Ρ‹ Π΄ΠΎΠ»ΠΆΠ½ΠΎ ΡΡ‚ΠΎΡΡ‚ΡŒ sum Π½ΡƒΠ»Π΅ΠΉ while kol[k]<>sum do // ΠΏΠΎΠΊΠ° sum Π½ΡƒΠ»Π΅ΠΉ Π½Π΅ окаТСтся Ρƒ послСднСй Π΅Π΄ΠΈΠ½ΠΈΡ†Ρ‹ ΠΏΠ΅Ρ€Π΅Π±ΠΈΡ€Π°Ρ‚ΡŒ begin vivod(sum); //вывСсти ΠΎΡ‡Π΅Ρ€Π΅Π΄Π½ΡƒΡŽ пСрСстановку if i=k then // Ссли ΠΌΡ‹ дошли Π΄ΠΎ послСдний(k-ΠΎΠΉ Π΅Π΄ΠΈΠ½ΠΈΡ†Ρ‹) begin x:=kol[k]; //Π·Π°ΠΏΠΎΠΌΠΈΠ½Π°Π΅ΠΌ ΠΊΠΎΠ»-Π²ΠΎ Π΅Π΄ΠΈΠ½ΠΈΡ† стоящих послС послСднСй Π΅Π΄ΠΈΠ½ΠΈΡ†Ρ‹ kol[k]:=0; //обнуляСм Π΅Π³ΠΎ, вСдь ΡƒΠ²Π΅Π»ΠΈΡ‡ΠΈΠ²Π°Ρ‚ΡŒ Π½Π΅ΠΊΡƒΠ΄Π° while kol[i-1]=0 do dec(i); //Π½Π°Ρ…ΠΎΠ΄ΠΈΠΌ Ρ‚Π°ΠΊΡƒΡŽ ΠΏΠ΅Ρ€Π²ΡƒΡŽ Π΅Π΄ΠΈΠ½ΠΈΡ†Ρƒ Ρƒ ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠΉ Π½Π΅ стоят Π½ΡƒΠ»ΠΈ kol[i]:=x; //присваСм Π΅ΠΉ ΠΊΠΎΠ»-Π²ΠΎ ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠ΅ Π±Ρ‹Π»ΠΎ Ρƒ послСднСго элСмСнта dec(i); // ΡƒΠΌΠ΅Π½ΡŒΡˆΠ°Π΅ΠΌ ΡƒΠΊΠ°Π·Π°Ρ‚Π΅Π»ΡŒ Π½Π° ΠΏΡ€Π΅Π΄Ρ‹Π΄ΡƒΡ‰ΠΈΠΉ элСмСнт end; {Ρ‚Π°ΠΊΠΈΠΌ ΠΎΠ±Ρ€Π°Π·ΠΎΠΌ я ΡƒΠΌΠ΅Π½ΡŒΡˆΠ°ΡŽ kol Π½ΡƒΠ»Π΅ΠΉ Ρƒ i Π΅Π΄ΠΈΠ½ΠΈΡ†Ρ‹, ΠΈ ΡƒΠ²Π΅Π»ΠΈΡ‡ΠΈΠ²Π°ΡŽ ΠΊΠΎΠ»-Π²ΠΎ Π½ΡƒΠ»Π΅ΠΉ Ρƒ ΡΠ»Π΅Π΄ΡƒΡŽΡ‰Π΅ΠΉ Π΅Π΄ΠΈΠ½ΠΈΡ†Ρ‹} dec(kol[i]); inc(i); inc(kol[i]); end; vivod(sum); // Π²Ρ‹Π²ΠΎΠΆΡƒ пСрСстановку послСднюю ΠΊΠΎΠ³Π΄Π° массив kol=[0,0,0,..,0,0,sum] end; end; begin Assign(input,'input.txt'); Reset(input); REadLn(n,k); {Ρ‡Ρ‚ΠΎΠ±Ρ‹ Π²Ρ‹Π²ΠΎΠ΄ΠΈΡ‚ΡŒ Π²Π΅Π΄ΡƒΡ‰ΠΈΠ΅ Π½ΡƒΠ»ΠΈ ΠΈΠ»ΠΈ Π½ΡƒΠ»ΠΈ ΠΌΠ΅ΠΆΠ΄Ρƒ Π΅Π΄ΠΈΠ½ΠΈΡ†Π°ΠΌΠΈ Π½Π΅ ΠΈΡΠΏΠΎΠ»ΡŒΠ·ΡƒΡ Ρ†ΠΈΠΊΠ»Ρ‹, Π° Ρ‚ΡƒΠΏΠΎ copy} s:='0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'; close(input); Assign(output,'output.txt'); ReWrite(output); perebor; close(output); end. 
  • Yes, I decide for myself! I can even write my idea and decision! But it is made clumsily. - Eugene536
  • It looks like an Olympiad puzzle. Solution head-on: go through all binary numbers to the maximum with K units at the beginning and NK zeros, and check the number of units in each number, if it is equal to K, enter in response, if not, go further. - Specter
  • Eugene, really write your idea - we will continue to look. PS Thoughts also appeared, but have long passed the exam on this topic :). - Vyacheslav Kirichenko
  • clarified as he could, read! does not pass 3 tests and then only in time - Eugene536
  • 2
    Eugene, I'm sorry, I wrote: give an idea - I will look. But now the Euro 2012 match is coming :). If you have time until Monday, then skip me again your task at freestas@email.ua - I will try to solve it. But now it is clear to me that it is necessary to set a string variable of length N, and then β€œpush” the units to the right, taking into account the digit capacity of K and reaching the end of the line. Although, I feel, there is a universal mathematical algorithm for solving a similar problem. - Vyacheslav Kirichenko

1 answer 1

 program test; {$APPTYPE CONSOLE} var ones: array of Integer; i: Integer; K: Integer; N: Integer; S: String; input, output: TextFile; procedure shiftones(idx: Integer); begin if (Idx<K-1) and (ones[Idx+1]=ones[Idx]+1) then begin shiftones(Idx+1);//Π΄Π²ΠΈΠ³Π°Π΅ΠΌ индСкс ΡΡ‚Π°Ρ€ΡˆΠ΅ΠΉ Π΅Π΄ΠΈΠ½ΠΈΡ†Ρ‹ ones[Idx]:=Idx;//Π° Ρ‚Π΅ΠΊΡƒΡ‰ΠΈΠΉ индСкс Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅ΠΌ Π½Π° Π½Π°Ρ‡Π°Π»ΠΎ. Π½Π°Ρ‡Π°Π»ΠΎ совпадаСт с индСксом end else ones[Idx]:=ones[Idx]+1;//Π΄Π²ΠΈΠ³Π°Π΅ΠΌ индСкс Ρ‚Π΅ΠΊΡƒΡ‰Π΅ΠΉ Π΅Π΄ΠΈΠ½ΠΈΡ†Ρ‹ end; function CurStr: String; var ii: Integer; begin Result:=S; for ii:=0 to K-1 do Result[N-ones[ii]]:='1';//вписываСм Π΅Π΄ΠΈΠ½ΠΈΡ†Ρ‹ Π² индСксы ΠΏΠΎ массиву (с ΠΊΠΎΠ½Ρ†Π°) end; begin AssignFile(input,'input.txt'); Reset(input); ReadLn(input,N,K); closeFile(input); S:=''; for i:=0 to N-1 do S:=S+'0';//Π‘Ρ‚Ρ€ΠΎΠΊΠ° Π½ΡƒΠ»Π΅ΠΉ SetLength(ones,K); for i:=0 to Length(ones)-1 do ones[i]:=i; //Массив индСксов Π΅Π΄ΠΈΠ½ΠΈΡ† AssignFile(output,'output.txt'); ReWrite(output); while (ones[K-1]<N) do begin WriteLn(output,CurStr); shiftones(0);//Π”Π²ΠΈΠ³Π°Π΅ΠΌ Π΅Π΄ΠΈΠ½ΠΈΡ†Ρ‹ end; closeFile(output); end. 

With N = 22 and K = 11 (the maximum number of lines will be for n with k = n / 2) the time is 0.731, the file size is 16.1MB, i.e. more than you need.

  • cool solution, but it fails 10 tests, a runtime error says! I will think where is the mistake - Eugene536
  • Errors of this program: the case was not taken into account that can be k = 0 and can be n = 0; Thanks for this code! - Eugene536
  • Yes, I did not check the boundary conditions. Well, easy to fix, the main principle is clear. - Yura Ivanov