Hex Artifact Content
Not logged in

Artifact 41eb9ab0d33a154bbc6b42872a9b3151f146806e:


0000: 23 69 6e 63 6c 75 64 65 20 3c 69 6f 73 74 72 65  #include <iostre
0010: 61 6d 3e 0d 0a 23 69 6e 63 6c 75 64 65 20 3c 73  am>..#include <s
0020: 73 74 72 65 61 6d 3e 0d 0a 23 69 6e 63 6c 75 64  stream>..#includ
0030: 65 20 3c 69 6f 6d 61 6e 69 70 3e 0d 0a 23 69 6e  e <iomanip>..#in
0040: 63 6c 75 64 65 20 3c 76 65 63 74 6f 72 3e 0d 0a  clude <vector>..
0050: 23 69 6e 63 6c 75 64 65 20 3c 73 74 72 69 6e 67  #include <string
0060: 3e 0d 0a 23 69 6e 63 6c 75 64 65 20 3c 6d 61 70  >..#include <map
0070: 3e 0d 0a 23 69 6e 63 6c 75 64 65 20 3c 73 65 74  >..#include <set
0080: 3e 0d 0a 23 69 6e 63 6c 75 64 65 20 3c 61 6c 67  >..#include <alg
0090: 6f 72 69 74 68 6d 3e 0d 0a 23 69 6e 63 6c 75 64  orithm>..#includ
00a0: 65 20 3c 6e 75 6d 65 72 69 63 3e 0d 0a 23 69 6e  e <numeric>..#in
00b0: 63 6c 75 64 65 20 3c 69 74 65 72 61 74 6f 72 3e  clude <iterator>
00c0: 0d 0a 23 69 6e 63 6c 75 64 65 20 3c 66 75 6e 63  ..#include <func
00d0: 74 69 6f 6e 61 6c 3e 0d 0a 23 69 6e 63 6c 75 64  tional>..#includ
00e0: 65 20 3c 63 6f 6d 70 6c 65 78 3e 0d 0a 23 69 6e  e <complex>..#in
00f0: 63 6c 75 64 65 20 3c 71 75 65 75 65 3e 0d 0a 23  clude <queue>..#
0100: 69 6e 63 6c 75 64 65 20 3c 73 74 61 63 6b 3e 0d  include <stack>.
0110: 0a 23 69 6e 63 6c 75 64 65 20 3c 63 6d 61 74 68  .#include <cmath
0120: 3e 0d 0a 23 69 6e 63 6c 75 64 65 20 3c 63 61 73  >..#include <cas
0130: 73 65 72 74 3e 0d 0a 23 69 6e 63 6c 75 64 65 20  sert>..#include 
0140: 3c 63 73 74 72 69 6e 67 3e 0d 0a 75 73 69 6e 67  <cstring>..using
0150: 20 6e 61 6d 65 73 70 61 63 65 20 73 74 64 3b 0d   namespace std;.
0160: 0a 74 79 70 65 64 65 66 20 6c 6f 6e 67 20 6c 6f  .typedef long lo
0170: 6e 67 20 4c 4c 3b 0d 0a 74 79 70 65 64 65 66 20  ng LL;..typedef 
0180: 63 6f 6d 70 6c 65 78 3c 64 6f 75 62 6c 65 3e 20  complex<double> 
0190: 43 4d 50 3b 0d 0a 0d 0a 63 6c 61 73 73 20 4f 72  CMP;....class Or
01a0: 64 65 72 65 64 4e 69 6d 20 7b 20 70 75 62 6c 69  deredNim { publi
01b0: 63 3a 0d 0a 09 73 74 72 69 6e 67 20 77 69 6e 6e  c:...string winn
01c0: 65 72 28 76 65 63 74 6f 72 20 3c 69 6e 74 3e 20  er(vector <int> 
01d0: 6c 61 79 6f 75 74 29 20 0d 0a 09 7b 0d 0a 09 09  layout) ...{....
01e0: 62 6f 6f 6c 20 66 69 72 73 74 57 69 6e 20 3d 20  bool firstWin = 
01f0: 66 61 6c 73 65 3b 0d 0a 09 09 66 6f 72 28 69 6e  false;....for(in
0200: 74 20 69 3d 6c 61 79 6f 75 74 2e 73 69 7a 65 28  t i=layout.size(
0210: 29 2d 31 3b 20 69 3e 3d 30 3b 20 2d 2d 69 29 0d  )-1; i>=0; --i).
0220: 0a 09 09 09 69 66 28 20 6c 61 79 6f 75 74 5b 69  ....if( layout[i
0230: 5d 20 3d 3d 20 31 20 29 0d 0a 09 09 09 09 66 69  ] == 1 )......fi
0240: 72 73 74 57 69 6e 20 3d 20 21 66 69 72 73 74 57  rstWin = !firstW
0250: 69 6e 3b 0d 0a 09 09 09 65 6c 73 65 0d 0a 09 09  in;.....else....
0260: 09 09 66 69 72 73 74 57 69 6e 20 3d 20 74 72 75  ..firstWin = tru
0270: 65 3b 0d 0a 09 09 72 65 74 75 72 6e 20 66 69 72  e;....return fir
0280: 73 74 57 69 6e 20 3f 20 22 41 6c 69 63 65 22 20  stWin ? "Alice" 
0290: 3a 20 22 42 6f 62 22 3b 0d 0a 09 7d 0d 0a 7d 3b  : "Bob";...}..};
02a0: 0d 0a 0d 0a 2f 2f 20 42 45 47 49 4e 20 43 55 54  ....// BEGIN CUT
02b0: 20 48 45 52 45 0d 0a 23 69 6e 63 6c 75 64 65 20   HERE..#include 
02c0: 3c 63 74 69 6d 65 3e 0d 0a 64 6f 75 62 6c 65 20  <ctime>..double 
02d0: 73 74 61 72 74 5f 74 69 6d 65 3b 20 73 74 72 69  start_time; stri
02e0: 6e 67 20 74 69 6d 65 72 28 29 0d 0a 20 7b 20 6f  ng timer().. { o
02f0: 73 74 72 69 6e 67 73 74 72 65 61 6d 20 6f 73 3b  stringstream os;
0300: 20 6f 73 20 3c 3c 20 22 20 28 22 20 3c 3c 20 69   os << " (" << i
0310: 6e 74 28 28 63 6c 6f 63 6b 28 29 2d 73 74 61 72  nt((clock()-star
0320: 74 5f 74 69 6d 65 29 2f 43 4c 4f 43 4b 53 5f 50  t_time)/CLOCKS_P
0330: 45 52 5f 53 45 43 2a 31 30 30 30 29 20 3c 3c 20  ER_SEC*1000) << 
0340: 22 20 6d 73 65 63 29 22 3b 20 72 65 74 75 72 6e  " msec)"; return
0350: 20 6f 73 2e 73 74 72 28 29 3b 20 7d 0d 0a 74 65   os.str(); }..te
0360: 6d 70 6c 61 74 65 3c 74 79 70 65 6e 61 6d 65 20  mplate<typename 
0370: 54 3e 20 6f 73 74 72 65 61 6d 26 20 6f 70 65 72  T> ostream& oper
0380: 61 74 6f 72 3c 3c 28 6f 73 74 72 65 61 6d 26 20  ator<<(ostream& 
0390: 6f 73 2c 20 63 6f 6e 73 74 20 76 65 63 74 6f 72  os, const vector
03a0: 3c 54 3e 26 20 76 29 0d 0a 20 7b 20 6f 73 20 3c  <T>& v).. { os <
03b0: 3c 20 22 7b 20 22 3b 0d 0a 20 20 20 66 6f 72 28  < "{ ";..   for(
03c0: 74 79 70 65 6e 61 6d 65 20 76 65 63 74 6f 72 3c  typename vector<
03d0: 54 3e 3a 3a 63 6f 6e 73 74 5f 69 74 65 72 61 74  T>::const_iterat
03e0: 6f 72 20 69 74 3d 76 2e 62 65 67 69 6e 28 29 3b  or it=v.begin();
03f0: 20 69 74 21 3d 76 2e 65 6e 64 28 29 3b 20 2b 2b   it!=v.end(); ++
0400: 69 74 29 0d 0a 20 20 20 6f 73 20 3c 3c 20 27 5c  it)..   os << '\
0410: 22 27 20 3c 3c 20 2a 69 74 20 3c 3c 20 27 5c 22  "' << *it << '\"
0420: 27 20 3c 3c 20 28 69 74 2b 31 3d 3d 76 2e 65 6e  ' << (it+1==v.en
0430: 64 28 29 20 3f 20 22 22 20 3a 20 22 2c 20 22 29  d() ? "" : ", ")
0440: 3b 20 6f 73 20 3c 3c 20 22 20 7d 22 3b 20 72 65  ; os << " }"; re
0450: 74 75 72 6e 20 6f 73 3b 20 7d 0d 0a 76 6f 69 64  turn os; }..void
0460: 20 76 65 72 69 66 79 5f 63 61 73 65 28 63 6f 6e   verify_case(con
0470: 73 74 20 73 74 72 69 6e 67 26 20 45 78 70 65 63  st string& Expec
0480: 74 65 64 2c 20 63 6f 6e 73 74 20 73 74 72 69 6e  ted, const strin
0490: 67 26 20 52 65 63 65 69 76 65 64 29 20 7b 0d 0a  g& Received) {..
04a0: 20 62 6f 6f 6c 20 6f 6b 20 3d 20 28 45 78 70 65   bool ok = (Expe
04b0: 63 74 65 64 20 3d 3d 20 52 65 63 65 69 76 65 64  cted == Received
04c0: 29 3b 0d 0a 20 69 66 28 6f 6b 29 20 63 65 72 72  );.. if(ok) cerr
04d0: 20 3c 3c 20 22 50 41 53 53 45 44 22 20 3c 3c 20   << "PASSED" << 
04e0: 74 69 6d 65 72 28 29 20 3c 3c 20 65 6e 64 6c 3b  timer() << endl;
04f0: 20 20 65 6c 73 65 20 7b 20 63 65 72 72 20 3c 3c    else { cerr <<
0500: 20 22 46 41 49 4c 45 44 22 20 3c 3c 20 74 69 6d   "FAILED" << tim
0510: 65 72 28 29 20 3c 3c 20 65 6e 64 6c 3b 0d 0a 20  er() << endl;.. 
0520: 63 65 72 72 20 3c 3c 20 22 5c 74 6f 3a 20 5c 22  cerr << "\to: \"
0530: 22 20 3c 3c 20 45 78 70 65 63 74 65 64 20 3c 3c  " << Expected <<
0540: 20 27 5c 22 27 20 3c 3c 20 65 6e 64 6c 20 3c 3c   '\"' << endl <<
0550: 20 22 5c 74 78 3a 20 5c 22 22 20 3c 3c 20 52 65   "\tx: \"" << Re
0560: 63 65 69 76 65 64 20 3c 3c 20 27 5c 22 27 20 3c  ceived << '\"' <
0570: 3c 20 65 6e 64 6c 3b 20 7d 20 7d 0d 0a 23 64 65  < endl; } }..#de
0580: 66 69 6e 65 20 43 41 53 45 28 4e 29 20 7b 63 65  fine CASE(N) {ce
0590: 72 72 20 3c 3c 20 22 54 65 73 74 20 43 61 73 65  rr << "Test Case
05a0: 20 23 22 20 3c 3c 20 4e 20 3c 3c 20 22 2e 2e 2e   #" << N << "...
05b0: 22 20 3c 3c 20 66 6c 75 73 68 3b 20 73 74 61 72  " << flush; star
05c0: 74 5f 74 69 6d 65 3d 63 6c 6f 63 6b 28 29 3b 0d  t_time=clock();.
05d0: 0a 23 64 65 66 69 6e 65 20 45 4e 44 09 20 76 65  .#define END. ve
05e0: 72 69 66 79 5f 63 61 73 65 28 5f 2c 20 4f 72 64  rify_case(_, Ord
05f0: 65 72 65 64 4e 69 6d 28 29 2e 77 69 6e 6e 65 72  eredNim().winner
0600: 28 6c 61 79 6f 75 74 29 29 3b 7d 0d 0a 69 6e 74  (layout));}..int
0610: 20 6d 61 69 6e 28 29 7b 0d 0a 0d 0a 43 41 53 45   main(){....CASE
0620: 28 30 29 0d 0a 09 69 6e 74 20 6c 61 79 6f 75 74  (0)...int layout
0630: 5f 5b 5d 20 3d 20 7b 35 7d 3b 0d 0a 09 20 20 76  _[] = {5};...  v
0640: 65 63 74 6f 72 20 3c 69 6e 74 3e 20 6c 61 79 6f  ector <int> layo
0650: 75 74 28 6c 61 79 6f 75 74 5f 2c 20 6c 61 79 6f  ut(layout_, layo
0660: 75 74 5f 2b 73 69 7a 65 6f 66 28 6c 61 79 6f 75  ut_+sizeof(layou
0670: 74 5f 29 2f 73 69 7a 65 6f 66 28 2a 6c 61 79 6f  t_)/sizeof(*layo
0680: 75 74 5f 29 29 3b 20 0d 0a 09 73 74 72 69 6e 67  ut_)); ...string
0690: 20 5f 20 3d 20 22 41 6c 69 63 65 22 3b 20 0d 0a   _ = "Alice"; ..
06a0: 45 4e 44 0d 0a 43 41 53 45 28 31 29 0d 0a 09 69  END..CASE(1)...i
06b0: 6e 74 20 6c 61 79 6f 75 74 5f 5b 5d 20 3d 20 7b  nt layout_[] = {
06c0: 31 2c 32 7d 3b 0d 0a 09 20 20 76 65 63 74 6f 72  1,2};...  vector
06d0: 20 3c 69 6e 74 3e 20 6c 61 79 6f 75 74 28 6c 61   <int> layout(la
06e0: 79 6f 75 74 5f 2c 20 6c 61 79 6f 75 74 5f 2b 73  yout_, layout_+s
06f0: 69 7a 65 6f 66 28 6c 61 79 6f 75 74 5f 29 2f 73  izeof(layout_)/s
0700: 69 7a 65 6f 66 28 2a 6c 61 79 6f 75 74 5f 29 29  izeof(*layout_))
0710: 3b 20 0d 0a 09 73 74 72 69 6e 67 20 5f 20 3d 20  ; ...string _ = 
0720: 22 42 6f 62 22 3b 20 0d 0a 45 4e 44 0d 0a 43 41  "Bob"; ..END..CA
0730: 53 45 28 32 29 0d 0a 09 69 6e 74 20 6c 61 79 6f  SE(2)...int layo
0740: 75 74 5f 5b 5d 20 3d 20 7b 32 2c 31 7d 3b 0d 0a  ut_[] = {2,1};..
0750: 09 20 20 76 65 63 74 6f 72 20 3c 69 6e 74 3e 20  .  vector <int> 
0760: 6c 61 79 6f 75 74 28 6c 61 79 6f 75 74 5f 2c 20  layout(layout_, 
0770: 6c 61 79 6f 75 74 5f 2b 73 69 7a 65 6f 66 28 6c  layout_+sizeof(l
0780: 61 79 6f 75 74 5f 29 2f 73 69 7a 65 6f 66 28 2a  ayout_)/sizeof(*
0790: 6c 61 79 6f 75 74 5f 29 29 3b 20 0d 0a 09 73 74  layout_)); ...st
07a0: 72 69 6e 67 20 5f 20 3d 20 22 41 6c 69 63 65 22  ring _ = "Alice"
07b0: 3b 20 0d 0a 45 4e 44 0d 0a 43 41 53 45 28 33 29  ; ..END..CASE(3)
07c0: 0d 0a 09 69 6e 74 20 6c 61 79 6f 75 74 5f 5b 5d  ...int layout_[]
07d0: 20 3d 20 7b 31 30 2c 39 2c 38 2c 37 2c 36 2c 35   = {10,9,8,7,6,5
07e0: 2c 34 2c 33 2c 32 2c 31 7d 3b 0d 0a 09 20 20 76  ,4,3,2,1};...  v
07f0: 65 63 74 6f 72 20 3c 69 6e 74 3e 20 6c 61 79 6f  ector <int> layo
0800: 75 74 28 6c 61 79 6f 75 74 5f 2c 20 6c 61 79 6f  ut(layout_, layo
0810: 75 74 5f 2b 73 69 7a 65 6f 66 28 6c 61 79 6f 75  ut_+sizeof(layou
0820: 74 5f 29 2f 73 69 7a 65 6f 66 28 2a 6c 61 79 6f  t_)/sizeof(*layo
0830: 75 74 5f 29 29 3b 20 0d 0a 09 73 74 72 69 6e 67  ut_)); ...string
0840: 20 5f 20 3d 20 22 41 6c 69 63 65 22 3b 20 0d 0a   _ = "Alice"; ..
0850: 45 4e 44 0d 0a 43 41 53 45 28 34 29 0d 0a 09 69  END..CASE(4)...i
0860: 6e 74 20 6c 61 79 6f 75 74 5f 5b 5d 20 3d 20 7b  nt layout_[] = {
0870: 31 2c 31 2c 31 2c 31 7d 3b 0d 0a 09 20 20 76 65  1,1,1,1};...  ve
0880: 63 74 6f 72 20 3c 69 6e 74 3e 20 6c 61 79 6f 75  ctor <int> layou
0890: 74 28 6c 61 79 6f 75 74 5f 2c 20 6c 61 79 6f 75  t(layout_, layou
08a0: 74 5f 2b 73 69 7a 65 6f 66 28 6c 61 79 6f 75 74  t_+sizeof(layout
08b0: 5f 29 2f 73 69 7a 65 6f 66 28 2a 6c 61 79 6f 75  _)/sizeof(*layou
08c0: 74 5f 29 29 3b 20 0d 0a 09 73 74 72 69 6e 67 20  t_)); ...string 
08d0: 5f 20 3d 20 22 42 6f 62 22 3b 20 0d 0a 45 4e 44  _ = "Bob"; ..END
08e0: 0d 0a 43 41 53 45 28 35 29 0d 0a 09 69 6e 74 20  ..CASE(5)...int 
08f0: 6c 61 79 6f 75 74 5f 5b 5d 20 3d 20 7b 31 7d 3b  layout_[] = {1};
0900: 0d 0a 09 20 20 76 65 63 74 6f 72 20 3c 69 6e 74  ...  vector <int
0910: 3e 20 6c 61 79 6f 75 74 28 6c 61 79 6f 75 74 5f  > layout(layout_
0920: 2c 20 6c 61 79 6f 75 74 5f 2b 73 69 7a 65 6f 66  , layout_+sizeof
0930: 28 6c 61 79 6f 75 74 5f 29 2f 73 69 7a 65 6f 66  (layout_)/sizeof
0940: 28 2a 6c 61 79 6f 75 74 5f 29 29 3b 20 0d 0a 09  (*layout_)); ...
0950: 73 74 72 69 6e 67 20 5f 20 3d 20 22 41 6c 69 63  string _ = "Alic
0960: 65 22 3b 20 0d 0a 45 4e 44 0d 0a 43 41 53 45 28  e"; ..END..CASE(
0970: 36 29 0d 0a 09 69 6e 74 20 6c 61 79 6f 75 74 5f  6)...int layout_
0980: 5b 5d 20 3d 20 7b 31 2c 31 2c 31 2c 31 2c 31 2c  [] = {1,1,1,1,1,
0990: 31 2c 31 2c 31 2c 31 2c 31 2c 31 2c 31 2c 31 2c  1,1,1,1,1,1,1,1,
09a0: 31 2c 31 2c 31 2c 31 2c 31 2c 31 2c 31 2c 31 2c  1,1,1,1,1,1,1,1,
09b0: 31 2c 31 2c 31 2c 31 2c 31 2c 31 2c 31 2c 31 2c  1,1,1,1,1,1,1,1,
09c0: 31 2c 31 2c 31 2c 31 2c 31 2c 31 2c 31 2c 31 2c  1,1,1,1,1,1,1,1,
09d0: 31 2c 31 2c 31 2c 31 2c 31 2c 31 2c 31 2c 31 2c  1,1,1,1,1,1,1,1,
09e0: 31 2c 31 2c 31 2c 31 2c 31 7d 3b 0d 0a 09 20 20  1,1,1,1,1};...  
09f0: 76 65 63 74 6f 72 20 3c 69 6e 74 3e 20 6c 61 79  vector <int> lay
0a00: 6f 75 74 28 6c 61 79 6f 75 74 5f 2c 20 6c 61 79  out(layout_, lay
0a10: 6f 75 74 5f 2b 73 69 7a 65 6f 66 28 6c 61 79 6f  out_+sizeof(layo
0a20: 75 74 5f 29 2f 73 69 7a 65 6f 66 28 2a 6c 61 79  ut_)/sizeof(*lay
0a30: 6f 75 74 5f 29 29 3b 20 0d 0a 09 73 74 72 69 6e  out_)); ...strin
0a40: 67 20 5f 20 3d 20 22 42 6f 62 22 3b 20 0d 0a 45  g _ = "Bob"; ..E
0a50: 4e 44 0d 0a 0d 0a 7d 0d 0a 2f 2f 20 45 4e 44 20  ND....}..// END 
0a60: 43 55 54 20 48 45 52 45 0d 0a                    CUT HERE..