⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 usaco 2_1_5 hamming codes 题解_leokan的blog.mht

📁 美国USACO题库源程序
💻 MHT
📖 第 1 页 / 共 5 页
字号:
<DIV class=3Ddesc></DIV>
<DIV id=3Dtabline></DIV>
<DIV id=3Dtab><A href=3D"http://hi.baidu.com/leokan">=D6=F7=D2=B3</A><A =
class=3Don=20
href=3D"http://hi.baidu.com/leokan/blog">=B2=A9=BF=CD</A><A=20
href=3D"http://hi.baidu.com/leokan/album">=CF=E0=B2=E1</A><SPAN>|</SPAN><=
A=20
href=3D"http://hi.baidu.com/leokan/profile">=B8=F6=C8=CB=B5=B5=B0=B8</A> =
<SPAN>|</SPAN><A=20
href=3D"http://hi.baidu.com/leokan/friends">=BA=C3=D3=D1</A> =
<SPAN>|</SPAN><A=20
href=3D"http://hi.baidu.com/leokan/modify/spbasic/0">=C9=E8=D6=C3</A> =
</DIV></DIV>
<DIV class=3Dstage>
<DIV class=3Dstagepad>
<DIV style=3D"WIDTH: 100%">
<TABLE class=3Dmodth cellSpacing=3D0 cellPadding=3D0 width=3D"100%" =
border=3D0>
  <TBODY>
  <TR>
    <TD class=3Dmodtl width=3D7>&nbsp;</TD>
    <TD class=3Dmodtc noWrap>
      <DIV class=3Dmodhead><SPAN =
class=3Dmodtit>=B2=E9=BF=B4=CE=C4=D5=C2</SPAN></DIV></TD>
    <TD class=3Dmodtc noWrap align=3Dright>
      <DIV class=3Dmodopt><A class=3Dmodact=20
      href=3D"http://hi.baidu.com/leokan/creat/blog/"><IMG=20
      src=3D"http://img.baidu.com/hi/img/ico_postnew.gif" =
align=3DabsMiddle=20
      border=3D0>=D0=B4=D0=C2=CE=C4=D5=C2</A></DIV></TD>
    <TD class=3Dmodtr width=3D7>&nbsp;</TD></TR></TBODY></TABLE>
<DIV class=3Dmodbox id=3Dm_blog>
<DIV class=3Dtit>USACO 2.1.5 Hamming Codes =CC=E2=BD=E2</DIV>
<DIV class=3Ddate>2008=C4=EA01=D4=C228=C8=D5 =D0=C7=C6=DA=D2=BB =
13:25</DIV>
<TABLE style=3D"TABLE-LAYOUT: fixed">
  <TBODY>
  <TR>
    <TD>
      <DIV class=3Dcnt>
      <H2>USACO 2.1.5 Hamming Codes</H2>
      <DIV class=3Dt_msgfont>Hamming Codes<BR>Rob Kolstad <BR>Given N, =
B, and D:=20
      Find a set of N codewords (1 &lt;=3D N &lt;=3D 64), each of length =
B bits (1=20
      &lt;=3D B &lt;=3D 8), such that each of the codewords is at least =
Hamming=20
      distance of D (1 &lt;=3D D &lt;=3D 7) away from each of the other =
codewords.=20
      The Hamming distance between a pair of codewords is the number of =
binary=20
      bits that differ in their binary notation. Consider the two =
codewords=20
      0x554 and 0x234 and their differences (0x554 means the hexadecimal =
number=20
      with hex digits 5, 5, and 4): <BR><BR>&nbsp;&nbsp; =
&nbsp;&nbsp;&nbsp;=20
      0x554 =3D 0101 0101 0100<BR>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; 0x234 =
=3D 0010=20
      0011 0100<BR>Bit differences: xxx&nbsp;&nbsp; xx<BR><BR>Since five =
bits=20
      were different, the Hamming distance is 5. <BR><BR>PROGRAM NAME:=20
      hamming<BR>INPUT FORMAT<BR>N, B, D on a single line <BR><BR>SAMPLE =
INPUT=20
      (file hamming.in)<BR>16 7 3<BR><BR>OUTPUT FORMAT<BR>N codewords, =
sorted,=20
      in decimal, ten per line. In the case of multiple solutions, your =
program=20
      should output the solution which, if interpreted as a base 2^B =
integer,=20
      would have the least value. <BR><BR>SAMPLE OUTPUT (file =
hamming.out)<BR>0=20
      7 25 30 42 45 51 52 75 76<BR>82 85 97 102 120 =
127<BR><BR><BR><BR>Hamming=20
      Codes <BR>=BA=A3=C3=F7=C2=EB<BR><BR>Rob Kolstad<BR><BR>=D2=EB by =
Felicia Crazy<BR><BR>=B8=F8=B3=F6 N=A3=ACB =BA=CD=20
      D=A3=BA=D5=D2=B3=F6 N =B8=F6=B1=E0=C2=EB=A3=A81 &lt;=3D N &lt;=3D =
64=A3=A9=A3=AC=C3=BF=B8=F6=B1=E0=C2=EB=D3=D0 B =CE=BB=A3=A81 &lt;=3D B =
&lt;=3D =
8=A3=A9=A3=AC=CA=B9=B5=C3=C1=BD=C1=BD=B1=E0=C2=EB=D6=AE=BC=E4=D6=C1=C9=D9=
=D3=D0 D=20
      =
=B8=F6=B5=A5=CE=BB=B5=C4=A1=B0=BA=A3=C3=F7=BE=E0=C0=EB=A1=B1=A3=A81 =
&lt;=3D D &lt;=3D =
7=A3=A9=A1=A3=A1=B0=BA=A3=C3=F7=BE=E0=C0=EB=A1=B1=CA=C7=D6=B8=B6=D4=D3=DA=
=C1=BD=B8=F6=B1=E0=C2=EB=A3=AC=CB=FB=C3=C7=B5=C4=B6=FE=BD=F8=D6=C6=B1=ED=CA=
=BE=B7=A8=D6=D0=B5=C4=B2=BB=CD=AC=B6=FE=BD=F8=D6=C6=CE=BB=B5=C4=CA=FD=C4=BF=
=A1=A3=BF=B4=CF=C2=C3=E6=B5=C4=C1=BD=B8=F6=B1=E0=C2=EB=20
      0x554 =BA=CD 0x234 =D6=AE=BC=E4=B5=C4=C7=F8=B1=F0=A3=A80x554 =
=B1=ED=CA=BE=D2=BB=B8=F6=CA=AE=C1=F9=BD=F8=D6=C6=CA=FD=A3=AC=C3=BF=B8=F6=CE=
=BB=C9=CF=B7=D6=B1=F0=CA=C7 5=A3=AC5=A3=AC4=A3=A9=A3=BA =
<BR><BR>&nbsp;&nbsp;=20
      &nbsp;&nbsp;&nbsp; 0x554 =3D 0101 0101 0100<BR>&nbsp;&nbsp;=20
      &nbsp;&nbsp;&nbsp; 0x234 =3D 0010 0011 0100<BR>&nbsp;&nbsp; =
=B2=BB=CD=AC=B5=C4=B6=FE=BD=F8=D6=C6=CE=BB:=20
      xxx&nbsp;&nbsp; =
xx<BR><BR>=D2=F2=CE=AA=D3=D0=CE=E5=B8=F6=CE=BB=B2=BB=CD=AC=A3=AC=CB=F9=D2=
=D4=A1=B0=BA=A3=C3=F7=BE=E0=C0=EB=A1=B1=CA=C7 5=A1=A3 <BR><BR>PROGRAM =
NAME:=20
      hamming<BR>INPUT FORMAT<BR>=D2=BB=D0=D0=A3=AC=B0=FC=C0=A8 N, B, =
D=A1=A3 <BR><BR>SAMPLE INPUT (file=20
      hamming.in)<BR>16 7 3<BR><BR>OUTPUT FORMAT<BR>N =
=B8=F6=B1=E0=C2=EB=A3=A8=D3=C3=CA=AE=BD=F8=D6=C6=B1=ED=CA=BE=A3=A9=A3=AC=D2=
=AA<SPAN=20
      class=3Dt_tag=20
      =
href=3D"tag.php?name=3D%C5%C5%D0%F2">=C5=C5=D0=F2</SPAN>=A3=AC=CA=AE=B8=F6=
=D2=BB=D0=D0=A1=A3=C8=E7=B9=FB=D3=D0=B6=E0=BD=E2=A3=AC=C4=E3=B5=C4=B3=CC=D0=
=F2=D2=AA=CA=E4=B3=F6=D5=E2=D1=F9=B5=C4=BD=E2=A3=BA=BC=D9=C8=E7=B0=D1=CB=FC=
=BB=AF=CE=AA=20
      2^B =
=BD=F8=D6=C6=B5=C4=CA=FD=A3=AC=CB=FC=B5=C4=D6=B5=D2=AA=D7=EE=D0=A1=A1=A3 =
<BR><BR>SAMPLE OUTPUT (file hamming.out)<BR>0 7 25 30 42=20
      45 51 52 75 76<BR>82 85 97 102 120 127</DIV>
      <P></P>
      <HR>

      <P></P>
      <P><STRONG>USACO 2.1.5 Hamming =
Codes<BR>=CC=E1=BD=BB=B4=CE=CA=FD=A3=BA2=B4=CE</STRONG></P>
      =
<P><STRONG>=D5=E2=CC=E2=BF=C9=D2=D4=D6=B1=BD=D3=CB=D1=CB=F7=A3=AC=B9=D8=D3=
=DA=C5=D0=B6=CF=B6=FE=BD=F8=D6=C6=B2=BB=CD=AC=B5=C4=B8=F6=CA=FD=BF=C9=D2=D4=
=D5=E2=C3=B4=D7=F6=A1=A3=CF=C8=D3=C3x xor=20
      =
y=B5=C3=B5=BDxy=D6=AE=BC=E4=C4=C4=BC=B8=B8=F6=CA=FD=B2=BB=CD=AC=A3=AC=C8=BB=
=BA=F3=CD=B3=BC=C6=D5=E2=B8=F6=CA=FD=D3=D0=B6=E0=C9=D9=B8=F61.=D3=C3x =
and=20
      =
-x=B5=C3=B5=BD=D7=EE=D3=D2=B1=DF=B5=C4=D2=BB=B8=F61=A3=AC=C8=BB=BA=F3=BC=F5=
=C8=A5=CB=FC=A3=AC=D6=D8=B8=B4=D5=E2=B8=F6=D6=B1=B5=BD=CB=FC=CE=AA0.=BB=B9=
=D3=D0=D2=BB=D6=D6=B7=BD=B7=A8=CA=C7=B7=D6=D2=B1=B7=A8=B5=C3=B5=BD=A3=BA<=
/STRONG></P><STRONG>
      <HR>
      </STRONG>
      <P><FONT =
size=3D2><STRONG>=CE=D2=BD=E2=CA=CD=B5=C3=BF=CF=B2=BB=C8=E7m67=C5=A3=B5=C4=
=BA=C3=A3=AC=D2=FD=D3=C3=CB=FC=B5=C4=B0=C9=A1=A3=D2=BB=CF=C2<FONT=20
      =
color=3D#3366ff>=C0=B6=C9=AB=D7=D6=CC=E5=B2=BF=B7=D6</FONT>=D7=AA=D7=D4m6=
7=B5=C4blog</STRONG></FONT></P>
      <P><FONT size=3D2><FONT style=3D"BACKGROUND-COLOR: #ffffff"><FONT=20
      =
color=3D#3366ff>=BC=C6=CB=E3=B6=FE=BD=F8=D6=C6=D6=D0=B5=C41=B5=C4=B8=F6=CA=
=FD<BR>&nbsp;&nbsp;&nbsp;&nbsp;=20
      =
=CD=AC=D1=F9=BC=D9=C9=E8x=CA=C7=D2=BB=B8=F632=CE=BB=D5=FB=CA=FD=A1=A3=BE=AD=
=B9=FD=CF=C2=C3=E6=CE=E5=B4=CE=B8=B3=D6=B5=BA=F3=A3=ACx=B5=C4=D6=B5=BE=CD=
=CA=C7=D4=AD=CA=FD=B5=C4=B6=FE=BD=F8=D6=C6=B1=ED=CA=BE=D6=D0=CA=FD=D7=D61=
=B5=C4=B8=F6=CA=FD=A1=A3=B1=C8=C8=E7=A3=AC=B3=F5=CA=BC=CA=B1x=CE=AA131452=
0=A3=A8=CD=F8=D3=D1=D7=A5=BF=F1=A3=BA=C4=DC=B2=BB=C4=DC=BB=BB=D2=BB=B8=F6=
=CA=FD=B0=A1=A3=A9=A3=AC=C4=C7=C3=B4=D7=EE=BA=F3x=BE=CD=B1=E4=B3=C9=C1=CB=
9=A3=AC=CB=FC=B1=ED=CA=BE1314520=B5=C4=B6=FE=BD=F8=D6=C6=D6=D0=D3=D09=B8=F6=
1=A1=A3<BR></FONT></FONT></FONT></P>
      <DIV class=3DUBBPanel>
      <DIV class=3DUBBContent><PRE><CODE class=3Dpascal><FONT =
style=3D"BACKGROUND-COLOR: #ffffff" color=3D#3366ff size=3D2>x :=3D (x =
<SPAN class=3Dkeyword>and</SPAN> $<SPAN class=3Dnumber>55555555</SPAN>) =
+ ((x <SPAN class=3Dkeyword>shr</SPAN> <SPAN class=3Dnumber>1</SPAN>) =
<SPAN class=3Dkeyword>and</SPAN> $<SPAN class=3Dnumber>55555555</SPAN>); =

x :=3D (x <SPAN class=3Dkeyword>and</SPAN> $<SPAN =
class=3Dnumber>33333333</SPAN>) + ((x <SPAN class=3Dkeyword>shr</SPAN> =
<SPAN class=3Dnumber>2</SPAN>) <SPAN class=3Dkeyword>and</SPAN> $<SPAN =
class=3Dnumber>33333333</SPAN>);=20
x :=3D (x <SPAN class=3Dkeyword>and</SPAN> $<SPAN =
class=3Dnumber>0</SPAN>F0F0F0F) + ((x <SPAN class=3Dkeyword>shr</SPAN> =
<SPAN class=3Dnumber>4</SPAN>) <SPAN class=3Dkeyword>and</SPAN> $<SPAN =
class=3Dnumber>0</SPAN>F0F0F0F);=20
x :=3D (x <SPAN class=3Dkeyword>and</SPAN> $<SPAN =
class=3Dnumber>00</SPAN>FF00FF) + ((x <SPAN class=3Dkeyword>shr</SPAN> =
<SPAN class=3Dnumber>8</SPAN>) <SPAN class=3Dkeyword>and</SPAN> $<SPAN =
class=3Dnumber>00</SPAN>FF00FF);=20
x :=3D (x <SPAN class=3Dkeyword>and</SPAN> $<SPAN =
class=3Dnumber>0000</SPAN>FFFF) + ((x <SPAN class=3Dkeyword>shr</SPAN> =
<SPAN class=3Dnumber>16</SPAN>) <SPAN class=3Dkeyword>and</SPAN> $<SPAN =
class=3Dnumber>0000</SPAN>FFFF); </FONT></CODE></PRE></DIV></DIV>
      <P><BR><FONT style=3D"BACKGROUND-COLOR: #ffffff" color=3D#3366ff=20
      size=3D2>&nbsp;&nbsp;&nbsp;&nbsp;=20
      =
=CE=AA=C1=CB=B1=E3=D3=DA=BD=E2=CB=B5=A3=AC=CE=D2=C3=C7=CF=C2=C3=E6=BD=F6=CB=
=B5=C3=F7=D5=E2=B8=F6=B3=CC=D0=F2=CA=C7=C8=E7=BA=CE=B6=D4=D2=BB=B8=F68=CE=
=BB=D5=FB=CA=FD=BD=F8=D0=D0=B4=A6=C0=ED=B5=C4=A1=A3=CE=D2=C3=C7=C4=C3=CA=FD=
=D7=D6211=A3=A8=CE=D2=C3=C7=B0=E0=C4=B3MM=B5=C4=C9=FA=C8=D5=A3=A9=C0=B4=BF=
=AA=B5=B6=A1=A3211=B5=C4=B6=FE=BD=F8=D6=C6=CE=AA11010011=A1=A3<BR><BR></F=
ONT><FONT=20
      size=3D2><FONT style=3D"BACKGROUND-COLOR: #ffffff"><FONT=20
      color=3D#3366ff><SPAN>+---+---+---+---+---+---+---+---+<BR>| 1 | 1 =
| 0 | 1 |=20
      0 | 0 | 1 | 1 |&nbsp;&nbsp;=20
      =
&lt;---=D4=AD=CA=FD<BR>+---+---+---+---+---+---+---+---+<BR>|&nbsp;&nbsp;=
 1=20
      0&nbsp;&nbsp; |&nbsp;&nbsp; 0 1&nbsp;&nbsp; |&nbsp;&nbsp; 0 =
0&nbsp;&nbsp;=20
      |&nbsp;&nbsp; 1 0&nbsp;&nbsp; |&nbsp;&nbsp;=20
      =
&lt;---=B5=DA=D2=BB=B4=CE=D4=CB=CB=E3=BA=F3<BR>+-------+-------+-------+-=
------+<BR>|&nbsp;&nbsp;&nbsp;&nbsp;=20
      0 0 1 1&nbsp;&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp; 0 0 1=20
      0&nbsp;&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;=20
      =
&lt;---=B5=DA=B6=FE=B4=CE=D4=CB=CB=E3=BA=F3<BR>+---------------+---------=
------+<BR>|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
      0 0 0 0 0 1 0 1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
      |&nbsp;&nbsp;=20
      =
&lt;---=B5=DA=C8=FD=B4=CE=D4=CB=CB=E3=BA=F3=A3=AC=B5=C3=CA=FD=CE=AA5<BR>+=
-------------------------------+</SPAN><BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;=20
      =
=D5=FB=B8=F6=B3=CC=D0=F2=CA=C7=D2=BB=B8=F6=B7=D6=D6=CE=B5=C4=CB=BC=CF=EB=A1=
=A3=B5=DA=D2=BB=B4=CE=CE=D2=C3=C7=B0=D1=C3=BF=CF=E0=C1=DA=B5=C4=C1=BD=CE=BB=
=BC=D3=C6=F0=C0=B4=A3=AC=B5=C3=B5=BD=C3=BF=C1=BD=CE=BB=C0=EF1=B5=C4=B8=F6=
=CA=FD=A3=AC=B1=C8=C8=E7=C7=B0=C1=BD=CE=BB10=BE=CD=B1=ED=CA=BE=D4=AD=CA=FD=
=B5=C4=C7=B0=C1=BD=CE=BB=D3=D02=B8=F61=A1=A3=B5=DA=B6=FE=B4=CE=CE=D2=C3=C7=
=BC=CC=D0=F8=C1=BD=C1=BD=CF=E0=BC=D3=A3=AC10+01=3D11=A3=AC00+10=3D10=A3=AC=
=B5=C3=B5=BD=B5=C4=BD=E1=B9=FB=CA=C700110010=A3=AC=CB=FC=B1=ED=CA=BE=D4=AD=
=CA=FD=C7=B04=CE=BB=D3=D03=B8=F61=A3=AC=C4=A94=CE=BB=D3=D02=B8=F61=A1=A3=D7=
=EE=BA=F3=D2=BB=B4=CE=CE=D2=C3=C7=B0=D10011=BA=CD0010=BC=D3=C6=F0=C0=B4=A3=
=AC=B5=C3=B5=BD=B5=C4=BE=CD=CA=C7=D5=FB=B8=F6=B6=FE=BD=F8=D6=C6=D6=D01=B5=
=C4=B8=F6=CA=FD=A1=A3=B3=CC=D0=F2=D6=D0=C7=C9=C3=EE=B5=D8=CA=B9=D3=C3=C8=A1=
=CE=BB=BA=CD=D3=D2=D2=C6=A3=AC=B1=C8=C8=E7=B5=DA=B6=FE=D0=D0=D6=D0$333333=
33=B5=C4=B6=FE=BD=F8=D6=C6=CE=AA00110011001100....=A3=AC=D3=C3=CB=FC=BA=CD=
x=D7=F6and=D4=CB=CB=E3=BE=CD=CF=E0=B5=B1=D3=DA=D2=D42=CE=AA=B5=A5=CE=BB=BC=
=E4=B8=F4=C8=A1=CA=FD=A1=A3shr=B5=C4=D7=F7=D3=C3=BE=CD=CA=C7=C8=C3=BC=D3=B7=
=A8=D4=CB=CB=E3=B5=C4=CF=E0=CD=AC=CA=FD=CE=BB=B6=D4=C6=EB=A1=A3</FONT></F=
ONT></FONT></P><FONT=20
      size=3D2><FONT style=3D"BACKGROUND-COLOR: #ffffff"><FONT=20
color=3D#3366ff><STRONG>
      <HR>
      </STRONG></FONT></FONT></FONT>
      =
<P><STRONG>=D5=E2=CC=E2=B5=C4=CA=E4=B3=F6=D5=E6=C2=E9=B7=B3...=CE=AA=B4=CB=
wa=C1=CB=D2=BB=B4=CE=A1=A3</STRONG></P>
      <P><STRONG>{<BR>TASK:hamming<BR>LANG:PASCAL<BR>}<BR>program=20
      hamming;<BR>var<BR>&nbsp;&nbsp;&nbsp; =
n,b,d:integer;<BR>&nbsp;&nbsp;&nbsp;=20
      ans:array[0..10000] of integer;<BR>procedure=20
      init;<BR>begin<BR>&nbsp;&nbsp;&nbsp;=20
      assign(input,'hamming.in');reset(input);<BR>&nbsp;&nbsp;&nbsp;=20
      readln(n,b,d);<BR>&nbsp;&nbsp;&nbsp; =
close(input);<BR>end;<BR>function=20
      check(x:longint):boolean;<BR>var<BR>&nbsp;&nbsp;&nbsp;=20
      i:integer;<BR>&nbsp;&nbsp;&nbsp;=20
      pos:longint;<BR>begin<BR>&nbsp;&nbsp;&nbsp; for i:=3D1 to ans[0]=20
      do<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
      =
begin<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs=
p;=20
      pos:=3Dx xor=20
      =
ans[i];<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n=
bsp;=20
      pos :=3D (pos and $55555555) + ((pos shr 1) and=20
      =
$55555555);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs=
p;&nbsp;=20
      pos :=3D (pos and $33333333) + ((pos shr 2) and=20
      =
$33333333);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs=
p;&nbsp;=20
      pos :=3D (pos and $0F0F0F0F) + ((pos shr 4) and=20
      =
$0F0F0F0F);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs=
p;&nbsp;=20
      pos :=3D (pos and $00FF00FF) + ((pos shr 8) and=20
      =
$00FF00FF);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs=
p;&nbsp;=20
      pos :=3D (pos and $0000FFFF) + ((pos shr 16) and=20
      =
$0000FFFF);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs=
p;&nbsp;=20
      if pos&lt;d then=20
      exit(false);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
      end;<BR>&nbsp;&nbsp;&nbsp; exit(true);<BR>end;<BR>procedure=20
      work;<BR>var<BR>&nbsp;&nbsp;&nbsp;=20
      num:longint;<BR>begin<BR>&nbsp;&nbsp;&nbsp;=20
      ans[0]:=3D1;<BR>&nbsp;&nbsp;&nbsp; =
ans[1]:=3D0;<BR>&nbsp;&nbsp;&nbsp;=20
      num:=3D0;<BR>&nbsp;&nbsp;&nbsp; for num:=3D1 to 1 shl b=20
      do<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
      =
begin<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs=
p;=20
      if check(num)=20
      =
then<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp=
;&nbsp;&nbsp;&nbsp;&nbsp;=20
      =
begin<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs=
p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
      =
inc(ans[0]);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
      =
ans[ans[0]]:=3Dnum;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n=
bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
      =
end;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp=
;=20
      if ans[0]=3Dn then =
break;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
      end;<BR>&nbsp;&nbsp;&nbsp;=20
      =
assign(output,'hamming.out');rewrite(output);<BR>&nbsp;&nbsp;&nbsp; for=20
      num:=3D1 to ans[0] =
do<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
      =
begin<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs=
p;=20
      if (num-1)mod 10=3D0 then=20
      =
write(ans[num])<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=
&nbsp;&nbsp;=20
      else write('=20
      =
',ans[num]);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;=20
      if num mod 10=3D0 then=20
      writeln;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
      end;<BR>&nbsp;&nbsp;&nbsp; if num mod 10&lt;&gt;0 then=20
      writeln;<BR>&nbsp;&nbsp;&nbsp;=20
      close(output);<BR>end;<BR>begin<BR>&nbsp;&nbsp;&nbsp;=20
      init;<BR>&nbsp;&nbsp;&nbsp; work;<BR>end.</STRONG></P>
      <P></P><STRONG>
      <HR>
      </STRONG>
      =
<P><STRONG>USACO=B5=C4=B7=D6=CE=F6=A3=AC=D3=C3=B5=DD=B9=E9=C7=F3=BD=E2</S=
TRONG></P>
      <P><STRONG>There are only a few tools we need to solve this =
problem. First=20
      of all, we can use basic techniques to find the Nth bit of a =
number M:=20
      counting the least significant bit as bit 0, the next bit as bit =
1, etc.,=20
      we can find the value of that bit through the expression =
</STRONG></P><PRE><STRONG>          int Nth_bit =3D (1 &lt;&lt; N) &amp; =
M;
</STRONG></PRE>
      <P><STRONG>In other words, we are shifting the number 1 left by N =
spaces,=20
      and then performing a binary AND on the resulting number with our =
original=20
      number, which will leave either a 1 in the Nth bit or a 0. So the =
first=20
      thing we have to do is find out the distance between each pair of =
numbers=20
      within the set of all numbers with B bits (0..2<SUP>B</SUP>-1).=20
      </STRONG></P>
      <P><STRONG>We also know that 0 can be one of the numbers in the =
set,=20
      because if the minimum number in the set were N instead of 0, we =
could=20
      subtract N from each number in the set and they would still be the =
same=20
      distance apart. The limits on the problem are low enough that we =
can do a=20
      DFS, and as soon as we hit a solution we can output it and exit.=20
      </STRONG></P><PRE><STRONG>#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;iostream.h&gt;
#define MAX (1 &lt;&lt; 8 + 1)
#define NMAX 65
#define BMAX 10
#define DMAX 10

int nums[NMAX], dist[MAX][MAX];
int N, B, D, maxval;
FILE *in, *out;

void findgroups(int cur, int start) {
    int a, b, canuse;
    char ch;
    if (cur =3D=3D N) {
        for (a =3D 0; a &lt; cur; a++) {
            if (a % 10)
                fprintf(out, " ");
            fprintf(out, "%d", nums[a]);
            if (a % 10 =3D=3D 9 || a =3D=3D cur-1)
                fprintf(out, "\n");
        }
        exit(0);
    }
    for (a =3D start; a &lt; maxval; a++) {
        canuse =3D 1;
        for (b =3D 0; b &lt; cur; b++)
            if (dist[nums[b]][a] &lt; D) {
                canuse =3D 0;
                break;
            }
        if (canuse) {
            nums[cur] =3D a;
            findgroups(cur+1, a+1);
        }
    }
}

int main() {
    in =3D fopen("hamming.in", "r");
    out =3D fopen("hamming.out", "w");
    int a, b, c;
    fscanf(in, "%d%d%d", &amp;N, &amp;B, &amp;D);
    maxval =3D (1 &lt;&lt; B);
    for (a =3D 0; a &lt; maxval; a++)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -