Hex Artifact Content
Not logged in

Artifact ad7c58f8100455665aee544049f43d6c1ad52ec0:


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 63 6f 6d 70  ..#include <comp
00d0: 6c 65 78 3e 0d 0a 23 69 6e 63 6c 75 64 65 20 3c  lex>..#include <
00e0: 71 75 65 75 65 3e 0d 0a 23 69 6e 63 6c 75 64 65  queue>..#include
00f0: 20 3c 73 74 61 63 6b 3e 0d 0a 23 69 6e 63 6c 75   <stack>..#inclu
0100: 64 65 20 3c 63 6d 61 74 68 3e 0d 0a 75 73 69 6e  de <cmath>..usin
0110: 67 20 6e 61 6d 65 73 70 61 63 65 20 73 74 64 3b  g namespace std;
0120: 0d 0a 74 79 70 65 64 65 66 20 6c 6f 6e 67 20 6c  ..typedef long l
0130: 6f 6e 67 20 4c 4c 3b 0d 0a 0d 0a 69 6e 74 20 67  ong LL;....int g
0140: 63 64 28 69 6e 74 20 61 2c 20 69 6e 74 20 62 29  cd(int a, int b)
0150: 0d 0a 7b 0d 0a 09 77 68 69 6c 65 28 61 29 20 7b  ..{...while(a) {
0160: 0d 0a 09 09 69 6e 74 20 74 20 3d 20 61 3b 0d 0a  ....int t = a;..
0170: 09 09 61 20 3d 20 62 25 61 3b 0d 0a 09 09 62 20  ..a = b%a;....b 
0180: 3d 20 74 3b 0d 0a 09 7d 0d 0a 09 72 65 74 75 72  = t;...}...retur
0190: 6e 20 62 3b 0d 0a 7d 0d 0a 0d 0a 63 6c 61 73 73  n b;..}....class
01a0: 20 44 65 73 69 67 6e 43 61 6c 65 6e 64 61 72 0d   DesignCalendar.
01b0: 0a 7b 0d 0a 70 75 62 6c 69 63 3a 0d 0a 09 69 6e  .{..public:...in
01c0: 74 20 73 68 6f 72 74 65 73 74 50 65 72 69 6f 64  t shortestPeriod
01d0: 28 69 6e 74 20 64 61 79 4c 65 6e 67 74 68 2c 20  (int dayLength, 
01e0: 69 6e 74 20 79 65 61 72 4c 65 6e 67 74 68 29 20  int yearLength) 
01f0: 0d 0a 09 7b 0d 0a 09 09 72 65 74 75 72 6e 20 64  ...{....return d
0200: 61 79 4c 65 6e 67 74 68 20 2f 20 67 63 64 28 79  ayLength / gcd(y
0210: 65 61 72 4c 65 6e 67 74 68 2c 20 64 61 79 4c 65  earLength, dayLe
0220: 6e 67 74 68 29 3b 0d 0a 09 7d 0d 0a 0d 0a 2f 2f  ngth);...}....//
0230: 20 42 45 47 49 4e 20 43 55 54 20 48 45 52 45 0d   BEGIN CUT HERE.
0240: 0a 09 70 75 62 6c 69 63 3a 0d 0a 09 76 6f 69 64  ..public:...void
0250: 20 72 75 6e 5f 74 65 73 74 28 69 6e 74 20 43 61   run_test(int Ca
0260: 73 65 29 20 7b 20 69 66 20 28 28 43 61 73 65 20  se) { if ((Case 
0270: 3d 3d 20 2d 31 29 20 7c 7c 20 28 43 61 73 65 20  == -1) || (Case 
0280: 3d 3d 20 30 29 29 20 74 65 73 74 5f 63 61 73 65  == 0)) test_case
0290: 5f 30 28 29 3b 20 69 66 20 28 28 43 61 73 65 20  _0(); if ((Case 
02a0: 3d 3d 20 2d 31 29 20 7c 7c 20 28 43 61 73 65 20  == -1) || (Case 
02b0: 3d 3d 20 31 29 29 20 74 65 73 74 5f 63 61 73 65  == 1)) test_case
02c0: 5f 31 28 29 3b 20 69 66 20 28 28 43 61 73 65 20  _1(); if ((Case 
02d0: 3d 3d 20 2d 31 29 20 7c 7c 20 28 43 61 73 65 20  == -1) || (Case 
02e0: 3d 3d 20 32 29 29 20 74 65 73 74 5f 63 61 73 65  == 2)) test_case
02f0: 5f 32 28 29 3b 20 69 66 20 28 28 43 61 73 65 20  _2(); if ((Case 
0300: 3d 3d 20 2d 31 29 20 7c 7c 20 28 43 61 73 65 20  == -1) || (Case 
0310: 3d 3d 20 33 29 29 20 74 65 73 74 5f 63 61 73 65  == 3)) test_case
0320: 5f 33 28 29 3b 20 69 66 20 28 28 43 61 73 65 20  _3(); if ((Case 
0330: 3d 3d 20 2d 31 29 20 7c 7c 20 28 43 61 73 65 20  == -1) || (Case 
0340: 3d 3d 20 34 29 29 20 74 65 73 74 5f 63 61 73 65  == 4)) test_case
0350: 5f 34 28 29 3b 20 69 66 20 28 28 43 61 73 65 20  _4(); if ((Case 
0360: 3d 3d 20 2d 31 29 20 7c 7c 20 28 43 61 73 65 20  == -1) || (Case 
0370: 3d 3d 20 35 29 29 20 74 65 73 74 5f 63 61 73 65  == 5)) test_case
0380: 5f 35 28 29 3b 20 7d 0d 0a 09 70 72 69 76 61 74  _5(); }...privat
0390: 65 3a 0d 0a 09 74 65 6d 70 6c 61 74 65 20 3c 74  e:...template <t
03a0: 79 70 65 6e 61 6d 65 20 54 3e 20 73 74 72 69 6e  ypename T> strin
03b0: 67 20 70 72 69 6e 74 5f 61 72 72 61 79 28 63 6f  g print_array(co
03c0: 6e 73 74 20 76 65 63 74 6f 72 3c 54 3e 20 26 56  nst vector<T> &V
03d0: 29 20 7b 20 6f 73 74 72 69 6e 67 73 74 72 65 61  ) { ostringstrea
03e0: 6d 20 6f 73 3b 20 6f 73 20 3c 3c 20 22 7b 20 22  m os; os << "{ "
03f0: 3b 20 66 6f 72 20 28 74 79 70 65 6e 61 6d 65 20  ; for (typename 
0400: 76 65 63 74 6f 72 3c 54 3e 3a 3a 63 6f 6e 73 74  vector<T>::const
0410: 5f 69 74 65 72 61 74 6f 72 20 69 74 65 72 20 3d  _iterator iter =
0420: 20 56 2e 62 65 67 69 6e 28 29 3b 20 69 74 65 72   V.begin(); iter
0430: 20 21 3d 20 56 2e 65 6e 64 28 29 3b 20 2b 2b 69   != V.end(); ++i
0440: 74 65 72 29 20 6f 73 20 3c 3c 20 27 5c 22 27 20  ter) os << '\"' 
0450: 3c 3c 20 2a 69 74 65 72 20 3c 3c 20 22 5c 22 2c  << *iter << "\",
0460: 22 3b 20 6f 73 20 3c 3c 20 22 20 7d 22 3b 20 72  "; os << " }"; r
0470: 65 74 75 72 6e 20 6f 73 2e 73 74 72 28 29 3b 20  eturn os.str(); 
0480: 7d 0d 0a 09 76 6f 69 64 20 76 65 72 69 66 79 5f  }...void verify_
0490: 63 61 73 65 28 69 6e 74 20 43 61 73 65 2c 20 63  case(int Case, c
04a0: 6f 6e 73 74 20 69 6e 74 20 26 45 78 70 65 63 74  onst int &Expect
04b0: 65 64 2c 20 63 6f 6e 73 74 20 69 6e 74 20 26 52  ed, const int &R
04c0: 65 63 65 69 76 65 64 29 20 7b 20 63 65 72 72 20  eceived) { cerr 
04d0: 3c 3c 20 22 54 65 73 74 20 43 61 73 65 20 23 22  << "Test Case #"
04e0: 20 3c 3c 20 43 61 73 65 20 3c 3c 20 22 2e 2e 2e   << Case << "...
04f0: 22 3b 20 69 66 20 28 45 78 70 65 63 74 65 64 20  "; if (Expected 
0500: 3d 3d 20 52 65 63 65 69 76 65 64 29 20 63 65 72  == Received) cer
0510: 72 20 3c 3c 20 22 50 41 53 53 45 44 22 20 3c 3c  r << "PASSED" <<
0520: 20 65 6e 64 6c 3b 20 65 6c 73 65 20 7b 20 63 65   endl; else { ce
0530: 72 72 20 3c 3c 20 22 46 41 49 4c 45 44 22 20 3c  rr << "FAILED" <
0540: 3c 20 65 6e 64 6c 3b 20 63 65 72 72 20 3c 3c 20  < endl; cerr << 
0550: 22 5c 74 45 78 70 65 63 74 65 64 3a 20 5c 22 22  "\tExpected: \""
0560: 20 3c 3c 20 45 78 70 65 63 74 65 64 20 3c 3c 20   << Expected << 
0570: 27 5c 22 27 20 3c 3c 20 65 6e 64 6c 3b 20 63 65  '\"' << endl; ce
0580: 72 72 20 3c 3c 20 22 5c 74 52 65 63 65 69 76 65  rr << "\tReceive
0590: 64 3a 20 5c 22 22 20 3c 3c 20 52 65 63 65 69 76  d: \"" << Receiv
05a0: 65 64 20 3c 3c 20 27 5c 22 27 20 3c 3c 20 65 6e  ed << '\"' << en
05b0: 64 6c 3b 20 7d 20 7d 0d 0a 09 76 6f 69 64 20 74  dl; } }...void t
05c0: 65 73 74 5f 63 61 73 65 5f 30 28 29 20 7b 20 69  est_case_0() { i
05d0: 6e 74 20 41 72 67 30 20 3d 20 34 3b 20 69 6e 74  nt Arg0 = 4; int
05e0: 20 41 72 67 31 20 3d 20 31 34 36 31 3b 20 69 6e   Arg1 = 1461; in
05f0: 74 20 41 72 67 32 20 3d 20 34 3b 20 76 65 72 69  t Arg2 = 4; veri
0600: 66 79 5f 63 61 73 65 28 30 2c 20 41 72 67 32 2c  fy_case(0, Arg2,
0610: 20 73 68 6f 72 74 65 73 74 50 65 72 69 6f 64 28   shortestPeriod(
0620: 41 72 67 30 2c 20 41 72 67 31 29 29 3b 20 7d 0d  Arg0, Arg1)); }.
0630: 0a 09 76 6f 69 64 20 74 65 73 74 5f 63 61 73 65  ..void test_case
0640: 5f 31 28 29 20 7b 20 69 6e 74 20 41 72 67 30 20  _1() { int Arg0 
0650: 3d 20 38 36 34 30 30 3b 20 69 6e 74 20 41 72 67  = 86400; int Arg
0660: 31 20 3d 20 33 31 35 35 38 31 35 30 3b 20 69 6e  1 = 31558150; in
0670: 74 20 41 72 67 32 20 3d 20 31 37 32 38 3b 20 76  t Arg2 = 1728; v
0680: 65 72 69 66 79 5f 63 61 73 65 28 31 2c 20 41 72  erify_case(1, Ar
0690: 67 32 2c 20 73 68 6f 72 74 65 73 74 50 65 72 69  g2, shortestPeri
06a0: 6f 64 28 41 72 67 30 2c 20 41 72 67 31 29 29 3b  od(Arg0, Arg1));
06b0: 20 7d 0d 0a 09 76 6f 69 64 20 74 65 73 74 5f 63   }...void test_c
06c0: 61 73 65 5f 32 28 29 20 7b 20 69 6e 74 20 41 72  ase_2() { int Ar
06d0: 67 30 20 3d 20 39 38 3b 20 69 6e 74 20 41 72 67  g0 = 98; int Arg
06e0: 31 20 3d 20 31 30 30 3b 20 69 6e 74 20 41 72 67  1 = 100; int Arg
06f0: 32 20 3d 20 34 39 3b 20 76 65 72 69 66 79 5f 63  2 = 49; verify_c
0700: 61 73 65 28 32 2c 20 41 72 67 32 2c 20 73 68 6f  ase(2, Arg2, sho
0710: 72 74 65 73 74 50 65 72 69 6f 64 28 41 72 67 30  rtestPeriod(Arg0
0720: 2c 20 41 72 67 31 29 29 3b 20 7d 0d 0a 09 76 6f  , Arg1)); }...vo
0730: 69 64 20 74 65 73 74 5f 63 61 73 65 5f 33 28 29  id test_case_3()
0740: 20 7b 20 69 6e 74 20 41 72 67 30 20 3d 20 31 30   { int Arg0 = 10
0750: 30 30 20 3b 20 69 6e 74 20 41 72 67 31 20 3d 20  00 ; int Arg1 = 
0760: 35 30 30 30 30 31 30 30 3b 20 69 6e 74 20 41 72  50000100; int Ar
0770: 67 32 20 3d 20 31 30 3b 20 76 65 72 69 66 79 5f  g2 = 10; verify_
0780: 63 61 73 65 28 33 2c 20 41 72 67 32 2c 20 73 68  case(3, Arg2, sh
0790: 6f 72 74 65 73 74 50 65 72 69 6f 64 28 41 72 67  ortestPeriod(Arg
07a0: 30 2c 20 41 72 67 31 29 29 3b 20 7d 0d 0a 09 76  0, Arg1)); }...v
07b0: 6f 69 64 20 74 65 73 74 5f 63 61 73 65 5f 34 28  oid test_case_4(
07c0: 29 20 7b 20 69 6e 74 20 41 72 67 30 20 3d 20 35  ) { int Arg0 = 5
07d0: 36 37 33 3b 20 69 6e 74 20 41 72 67 31 20 3d 20  673; int Arg1 = 
07e0: 32 38 35 36 35 33 33 35 3b 20 69 6e 74 20 41 72  28565335; int Ar
07f0: 67 32 20 3d 20 35 36 37 33 3b 20 76 65 72 69 66  g2 = 5673; verif
0800: 79 5f 63 61 73 65 28 34 2c 20 41 72 67 32 2c 20  y_case(4, Arg2, 
0810: 73 68 6f 72 74 65 73 74 50 65 72 69 6f 64 28 41  shortestPeriod(A
0820: 72 67 30 2c 20 41 72 67 31 29 29 3b 20 7d 0d 0a  rg0, Arg1)); }..
0830: 09 76 6f 69 64 20 74 65 73 74 5f 63 61 73 65 5f  .void test_case_
0840: 35 28 29 20 7b 20 69 6e 74 20 41 72 67 30 20 3d  5() { int Arg0 =
0850: 20 35 35 35 35 3b 20 69 6e 74 20 41 72 67 31 20   5555; int Arg1 
0860: 3d 20 38 34 36 35 35 35 3b 20 69 6e 74 20 41 72  = 846555; int Ar
0870: 67 32 20 3d 20 31 31 31 31 3b 20 76 65 72 69 66  g2 = 1111; verif
0880: 79 5f 63 61 73 65 28 35 2c 20 41 72 67 32 2c 20  y_case(5, Arg2, 
0890: 73 68 6f 72 74 65 73 74 50 65 72 69 6f 64 28 41  shortestPeriod(A
08a0: 72 67 30 2c 20 41 72 67 31 29 29 3b 20 7d 0d 0a  rg0, Arg1)); }..
08b0: 0d 0a 2f 2f 20 45 4e 44 20 43 55 54 20 48 45 52  ..// END CUT HER
08c0: 45 0d 0a 7d 3b 0d 0a 2f 2f 20 42 45 47 49 4e 20  E..};..// BEGIN 
08d0: 43 55 54 20 48 45 52 45 20 0d 0a 69 6e 74 20 6d  CUT HERE ..int m
08e0: 61 69 6e 28 29 20 7b 20 44 65 73 69 67 6e 43 61  ain() { DesignCa
08f0: 6c 65 6e 64 61 72 28 29 2e 72 75 6e 5f 74 65 73  lendar().run_tes
0900: 74 28 2d 31 29 3b 20 7d 0d 0a 2f 2f 20 45 4e 44  t(-1); }..// END
0910: 20 43 55 54 20 48 45 52 45 0d 0a                  CUT HERE..