From a17c82b741218719e8831ad53ded5d7f0e3c12d9 Mon Sep 17 00:00:00 2001 From: Jingzhe Tang Date: Mon, 26 Dec 2022 15:35:55 +0800 Subject: [PATCH] add untracked trainings Signed-off-by: Jingzhe Tang --- Summer_Training/2016-07-23/A.cpp | 85 ++++++ Summer_Training/2016-07-23/B.cpp | 122 +++++++++ Summer_Training/2016-07-23/C.cpp | 99 +++++++ Summer_Training/2016-07-23/D.cpp | 47 ++++ Summer_Training/2016-07-23/E.cpp | 61 +++++ Summer_Training/2016-07-23/F.cpp | 88 ++++++ Summer_Training/2016-07-23/G.cpp | 89 ++++++ Summer_Training/2016-07-23/H.cpp | 52 ++++ Summer_Training/2016-07-23/I.cpp | 64 +++++ Summer_Training/2016-07-23/J.cpp | 88 ++++++ Summer_Training/2016-07-23/K.cpp | 34 +++ Summer_Training/2016-07-23/L.cpp | 15 + Summer_Training/2016-07-26/A.cpp | 127 +++++++++ Summer_Training/2016-07-26/C.cpp | 50 ++++ Summer_Training/2016-07-26/D.cpp | 101 +++++++ Summer_Training/2016-07-26/F.cpp | 107 ++++++++ Summer_Training/2016-07-26/G.cpp | 80 ++++++ Summer_Training/2016-07-26/H.cpp | 30 ++ Summer_Training/2016-07-26/I.cpp | 144 ++++++++++ Summer_Training/2016-08-02/H.cpp | 39 +++ Summer_Training/2016-08-02/J.cpp | 35 +++ Summer_Training/2016-08-09/H.cpp | 35 +++ Summer_Training/2016-08-11/A - Ball - tjz.cpp | 46 ++++ .../2016-08-11/C - color II - tjz.cpp | 48 ++++ .../2016-08-11/F - physics - tjz.cpp | 26 ++ .../H - Rikka with Sequence - tjz.cpp | 171 ++++++++++++ .../I - Rikka with Subset - tjz.cpp | 110 ++++++++ .../K - Rikka with Parenthesis II - tjz.cpp | 48 ++++ .../2016-08-18/A - Median - lzy.cpp | 54 ++++ .../2016-08-18/B - Hard problem - tjz.cpp | 14 + .../2016-08-18/D - Death Sequence - tjz.cpp | 258 ++++++++++++++++++ Summer_Training/2016-08-18/E - Road - lzy.cpp | 53 ++++ .../F - Counting Intersections - tjz.cpp | 79 ++++++ .../G - cjj's string game - tjz.cpp | 69 +++++ .../2016-08-18/K - Water problem - lzy.cpp | 77 ++++++ ...A - Different Circle Permutation - tjz.cpp | 91 ++++++ ...B - Different GCD Subarray Query - tjz.cpp | 76 ++++++ .../2016-09-10/F - Football Games - tjz.cpp | 27 ++ .../G - Friends and Enemies - tjz.cpp | 9 + .../2016-09-10/H - Function - lzy.cpp | 64 +++++ .../2016-09-10/I - Sparse Graph - lzy.cpp | 65 +++++ .../2016-09-10/J - Weak Pair - lzy.cpp | 75 +++++ Summer_Training/2016-09-15/A - git - lzy.cpp | 74 +++++ .../B - Circles and Squares - tjz.cpp | 72 +++++ .../2016-09-15/E - Cat and Mouse - lzy.cpp | 70 +++++ .../2016-09-15/F - Similar Strings - lzy.cpp | 35 +++ ...hota Mouse and his Birthday Cake - tjz.cpp | 23 ++ .../H - Longest Palindrome - lzy.cpp | 35 +++ .../A - I Count Two Three - liuzy.cpp | 44 +++ Summer_Training/2016-09-17/B - Cure - tjz.cpp | 25 ++ .../2016-09-17/C - Family View - tjz.cpp | 128 +++++++++ .../2016-09-17/E - Balanced Game - lzy.cpp | 11 + .../2016-09-17/F - The Best Path - lzy.cpp | 70 +++++ Summer_Training/2016-09-17/G - Sort - tjz.cpp | 84 ++++++ .../2016-09-17/I - Tower Defence - tjz.cpp | 144 ++++++++++ .../2016-09-17/K - Barricade - lzy.cpp | 104 +++++++ .../2016-09-18/A - Resident Evil - tjz.cpp | 85 ++++++ ..._j\342\200\231s Biological Test - tjz.cpp" | 34 +++ .../D - Mathematician QSC - tjz.cpp | 87 ++++++ .../2016-09-18/E - Running King - tjz.cpp | 112 ++++++++ .../2016-09-18/G - odd-even number - lzy.cpp | 84 ++++++ .../2016-09-18/H - oasis in desert - tjz.cpp | 171 ++++++++++++ .../2016-09-18/I - QSC and Master - lzy.cpp | 54 ++++ .../2016-09-18/J - Count primes - tjz.cpp | 43 +++ .../2016-09-19/A - Xiongnu's Land - lzy.cpp | 48 ++++ .../2016-09-19/E - Stamps - tjz.cpp | 46 ++++ ...rious Antiques in Sackler Museum - lzy.cpp | 68 +++++ .../2016-09-19/J - Osu! Master - tjz.cpp | 32 +++ .../2016-09-19/K - A Math Problem - tjz.cpp | 55 ++++ .../upsolved/I - Snake Carpet - tjz.cpp | 45 +++ .../2016-09-24/A - The Book List - tjz.cpp | 66 +++++ .../2016-09-24/C - A Simple Job - tjz.cpp | 70 +++++ .../2016-09-24/F - Periodic Signal - tjz.cpp | 124 +++++++++ .../2016-10-02/B - Inspection - liuzy.cpp | 78 ++++++ .../2016-10-02/C - Karmon Go - lzy.cpp | 17 ++ .../2016-10-02/E - Autocomplete - lzy.cpp | 70 +++++ .../F - Amazing Divisibility - lzy.cpp | 50 ++++ .../2016-10-02/G - Group Tournament - lzy.cpp | 113 ++++++++ .../2016-10-02/J - Carts - tjz.cpp | 36 +++ .../upsolved/I - Guess The Modulo - tjz.cpp | 138 ++++++++++ .../A - Nearest Neighbor Search - lzy.cpp | 83 ++++++ .../2016-10-04/E - Coins - liuzy.cpp | 35 +++ .../2016-10-04/F - Floyd-Warshall - lzy.cpp | 131 +++++++++ .../2016-10-04/G - Road History - tjz.cpp | 70 +++++ .../2016-10-04/H - Around the World - tjz.cpp | 31 +++ ...- Longest Increasing Subsequence - tjz.cpp | 64 +++++ .../J - Matrix Transformation - tjz.cpp | 75 +++++ .../upsolved/B - Odd Discount - tjz.cpp | 42 +++ .../D - Longest Common Subsequence - tjz.cpp | 57 ++++ .../2016-04-24/upsolved/G - Boxes - tjz.cpp | 105 +++++++ self_training/2017-04-30/4502A.cpp | 17 ++ self_training/2017-04-30/4502B.cpp | 56 ++++ self_training/2017-04-30/4502C.cpp | 24 ++ self_training/2017-04-30/4502D.cpp | 44 +++ self_training/2017-04-30/4502E.cpp | 41 +++ self_training/2017-04-30/4502F.cpp | 56 ++++ self_training/2017-08-05/1001.cpp | 96 +++++++ self_training/2017-08-05/1002.cpp | 126 +++++++++ self_training/2017-08-05/1003.cpp | 47 ++++ self_training/2017-08-05/1004.cpp | 44 +++ self_training/2017-08-05/1005.cpp | 26 ++ self_training/2017-08-13/1001.cpp | 26 ++ self_training/2017-08-13/1003.cpp | 45 +++ self_training/2017-08-13/1004.cpp | 58 ++++ self_training/2017-08-13/1005.cpp | 105 +++++++ self_training/2017-08-13/1006.cpp | 43 +++ self_training/2017-08-27/2A.cpp | 74 +++++ self_training/2017-08-27/2B.cpp | 22 ++ self_training/2017-08-27/2C.cpp | 84 ++++++ self_training/2017-09-15/A.cpp | 18 ++ self_training/2017-09-15/B.cpp | 17 ++ self_training/2017-09-15/C.cpp | 11 + self_training/2017-09-15/D.cpp | 32 +++ self_training/2017-09-15/E.py | 13 + self_training/2017-09-15/F.cpp | 24 ++ self_training/2017-09-15/G.cpp | 105 +++++++ self_training/2017-09-15/H.cpp | 53 ++++ self_training/2017-09-15/J.cpp | 23 ++ self_training/2018-01-17/7491A.cpp | 23 ++ self_training/2018-01-17/7491B.cpp | 26 ++ self_training/2018-01-17/7491C.cpp | 12 + self_training/2018-01-17/7491D.cpp | 34 +++ self_training/2018-01-17/7491E.cpp | 53 ++++ self_training/2018-01-17/7491F.cpp | 109 ++++++++ self_training/2018-02-24/agc21A.cpp | 17 ++ self_training/2018-02-24/agc21B.cpp | 61 +++++ self_training/2018-02-24/agc21C.cpp | 89 ++++++ self_training/2018-02-24/agc21D.cpp | 24 ++ self_training/2018-03-13/7736B.cpp | 35 +++ self_training/2018-03-13/7736C.cpp | 24 ++ self_training/2018-03-13/7736D.cpp | 33 +++ self_training/2018-03-30/82A.cpp | 30 ++ self_training/2018-03-30/82B.cpp | 35 +++ self_training/2018-03-30/82C.cpp | 64 +++++ self_training/2018-03-30/82D.cpp | 19 ++ self_training/2018-03-30/82E.cpp | 72 +++++ self_training/2018-04-08-2/7989A.cpp | 20 ++ self_training/2018-04-08-2/7989B.cpp | 27 ++ self_training/2018-04-08-2/7989C.cpp | 28 ++ self_training/2018-04-08-2/7989D.cpp | 57 ++++ self_training/2018-04-08/A.cpp | 43 +++ self_training/2018-04-08/B.cpp | 34 +++ self_training/2018-04-08/C.cpp | 59 ++++ self_training/2018-04-08/D.cpp | 37 +++ self_training/2018-07-06/131A.cpp | 27 ++ self_training/2018-07-06/131B.cpp | 35 +++ self_training/2018-07-06/131C.cpp | 117 ++++++++ self_training/2018-07-06/131D.cpp | 47 ++++ self_training/2018-07-06/131E.cpp | 131 +++++++++ self_training/2018-07-06/131F.cpp | 73 +++++ self_training/2018-07-19/A.cpp | 20 ++ self_training/2018-07-19/B.cpp | 20 ++ self_training/2018-07-19/C.cpp | 51 ++++ self_training/2018-07-19/F.cpp | 78 ++++++ self_training/2018-07-19/G.cpp | 115 ++++++++ self_training/2018-07-19/I.cpp | 85 ++++++ self_training/2018-07-19/J.cpp | 55 ++++ self_training/2018-07-21/B.cpp | 60 ++++ self_training/2018-07-21/E.cpp | 116 ++++++++ self_training/2018-07-21/G.cpp | 83 ++++++ self_training/2018-07-21/I.cpp | 20 ++ self_training/2018-07-21/K.cpp | 69 +++++ self_training/2018-07-22/A.cpp | 31 +++ self_training/2018-07-22/B.cpp | 60 ++++ self_training/2018-07-22/C.cpp | 51 ++++ self_training/2018-08-04/1001.cpp | 44 +++ self_training/2018-08-04/1002.cpp | 26 ++ self_training/2018-08-04/1003.cpp | 69 +++++ self_training/2018-08-04/1004-TLE.cpp | 72 +++++ self_training/2018-08-04/1005.cpp | 37 +++ self_training/2018-08-04/1006.cpp | 67 +++++ self_training/2018-08-11/1001.cpp | 17 ++ self_training/2018-08-11/1002.cpp | 50 ++++ self_training/2018-08-11/1003.cpp | 55 ++++ self_training/2018-08-11/1004.cpp | 41 +++ self_training/2018-08-11/1005.cpp | 76 ++++++ self_training/2018-08-18/1001.cpp | 42 +++ self_training/2018-08-18/1002.cpp | 41 +++ self_training/2018-08-18/1003.cpp | 59 ++++ self_training/2018-08-18/1004.cpp | 49 ++++ self_training/2018-08-18/1005.cpp | 56 ++++ self_training/2018-08-18/1006.cpp | 76 ++++++ self_training/2018-09-01/B.cpp | 44 +++ self_training/2018-09-01/C.cpp | 70 +++++ self_training/2018-09-01/G.cpp | 87 ++++++ self_training/2018-09-01/H.cpp | 70 +++++ self_training/2018-09-15/C.cpp | 90 ++++++ self_training/2018-09-15/D.java | 66 +++++ self_training/2018-09-15/G.cpp | 21 ++ self_training/2018-09-15/H.cpp | 127 +++++++++ self_training/2018-09-15/K.cpp | 62 +++++ self_training/2018-09-22/C.cpp | 161 +++++++++++ self_training/2018-09-22/E.py | 12 + self_training/2018-10-12/CHEFPRMS.cpp | 27 ++ self_training/2018-10-12/QABC.cpp | 26 ++ self_training/2018-10-12/QUALPREL.cpp | 16 ++ self_training/2018-10-12/SPREAD2.cpp | 17 ++ self_training/2018-10-12/TEAMMATE.cpp | 37 +++ self_training/2018-11-03/ANGLE.cpp | 67 +++++ self_training/2018-11-03/BUDDYNIM.cpp | 27 ++ self_training/2018-11-03/PAINTREE.cpp | 79 ++++++ self_training/2018-11-03/STRTF.cpp | 47 ++++ self_training/2018-11-09/217A.cpp | 16 ++ self_training/2018-11-09/217B.cpp | 26 ++ self_training/2018-11-09/217C.cpp | 64 +++++ self_training/2018-11-09/217D.cpp | 27 ++ self_training/2018-11-09/217E.cpp | 88 ++++++ self_training/2018-11-09/217F.cpp | 96 +++++++ self_training/2018-11-30/272A.cpp | 20 ++ self_training/2018-11-30/272B.cpp | 48 ++++ self_training/2018-11-30/272C.cpp | 8 + self_training/2018-11-30/272D.cpp | 77 ++++++ self_training/2018-11-30/272E.cpp | 117 ++++++++ self_training/2018-11-30/272F.cpp | 79 ++++++ self_training/2018-12-01/A.cpp | 117 ++++++++ self_training/2018-12-01/B.cpp | 44 +++ self_training/2018-12-01/C.cpp | 62 +++++ self_training/2018-12-01/D.cpp | 45 +++ self_training/2018-12-01/E.cpp | 42 +++ self_training/2018-12-01/F.cpp | 147 ++++++++++ self_training/2018-12-01/G.cpp | 124 +++++++++ self_training/2018-12-01/H.cpp | 69 +++++ self_training/2018-12-01/I.cpp | 68 +++++ self_training/2018-12-01/J.cpp | 119 ++++++++ self_training/2018-12-08/ADIMAT.cpp | 51 ++++ self_training/2018-12-08/ADITREE.cpp | 109 ++++++++ self_training/2018-12-08/ADJLEAF2.cpp | 123 +++++++++ self_training/2018-12-08/EBAIT.cpp | 66 +++++ self_training/2018-12-08/GDIST.cpp | 114 ++++++++ self_training/2018-12-08/IMPO.cpp | 78 ++++++ self_training/2018-12-08/RECOVER.cpp | 139 ++++++++++ self_training/2018-12-08/RECTLIT.cpp | 62 +++++ self_training/2018-12-08/SFXPAL.cpp | 13 + self_training/2018-12-08/XORTABLE.cpp | 106 +++++++ self_training/2019-04-06/A.cpp | 23 ++ self_training/2019-04-06/B.cpp | 16 ++ self_training/2019-04-06/C.py | 49 ++++ self_training/2019-04-06/D.cpp | 43 +++ self_training/2019-04-13/A.cpp | 101 +++++++ self_training/2019-04-13/B.cpp | 54 ++++ self_training/2019-04-13/C.cpp | 45 +++ self_training/2019-05-18/A.cpp | 48 ++++ self_training/2019-05-18/C.cpp | 83 ++++++ self_training/2019-05-18/D.cpp | 64 +++++ self_training/2019-07-26/A.cpp | 81 ++++++ self_training/2019-07-26/B.cpp | 20 ++ self_training/2019-07-26/C.cpp | 53 ++++ self_training/2019-07-26/D.cpp | 21 ++ self_training/2019-07-26/E.cpp | 73 +++++ self_training/2019-07-28/A.cpp | 33 +++ self_training/2019-07-28/B.cpp | 74 +++++ self_training/2019-07-28/C.cpp | 86 ++++++ self_training/2019-08-17-2/1001.cpp | 31 +++ self_training/2019-08-17-2/1002.cpp | 48 ++++ self_training/2019-08-17-2/1003.cpp | 86 ++++++ self_training/2019-08-17-2/1005.cpp | 104 +++++++ self_training/2019-08-17/A.cpp | 49 ++++ self_training/2019-08-17/C.cpp | 62 +++++ self_training/2019-08-17/D.py | 28 ++ self_training/2019-08-17/F.cpp | 95 +++++++ self_training/2019-08-17/J.cpp | 39 +++ self_training/2019-08-18/A.cpp | 47 ++++ self_training/2019-08-18/B.cpp | 69 +++++ self_training/2019-08-18/C.cpp | 57 ++++ self_training/2019-08-18/D.cpp | 66 +++++ self_training/2019-08-18/E.cpp | 89 ++++++ self_training/2019-08-18/F.cpp | 65 +++++ self_training/2019-08-23/1005.cpp | 51 ++++ self_training/2019-08-23/1009.cpp | 72 +++++ self_training/2019-08-23/1010.cpp | 147 ++++++++++ self_training/2019-08-23/1011.cpp | 87 ++++++ self_training/2019-08-31/A.cpp | 36 +++ self_training/2019-08-31/B.cpp | 40 +++ self_training/2019-08-31/C.cpp | 58 ++++ self_training/2019-08-31/E.cpp | 63 +++++ self_training/2019-09-07/H.cpp | 87 ++++++ self_training/2019-09-08/A.cpp | 147 ++++++++++ self_training/2019-09-08/E.cpp | 28 ++ self_training/2019-09-08/H.cpp | 62 +++++ self_training/2019-09-14/I.cpp | 151 ++++++++++ self_training/2019-09-15/K.cpp | 139 ++++++++++ self_training/2019-09-15/L.cpp | 96 +++++++ self_training/2019-09-20/E.cpp | 59 ++++ self_training/2019-10-20/1239A.cpp | 13 + self_training/2019-10-23/D.cpp | 60 ++++ self_training/2019-10-25/E.cpp | 135 +++++++++ self_training/2019-10-25/G.cpp | 56 ++++ self_training/2019-10-25/H.cpp | 55 ++++ self_training/2019-10-25/I.cpp | 56 ++++ self_training/2019-10-25/J.cpp | 27 ++ self_training/2020-04-19/A.cpp | 19 ++ self_training/2020-04-19/B.cpp | 21 ++ self_training/2020-04-19/C.cpp | 51 ++++ self_training/2020-04-19/D.cpp | 60 ++++ self_training/2020-04-21-cf3/A.cpp | 18 ++ self_training/2020-04-21-cf3/B.cpp | 26 ++ self_training/2020-04-21-cf3/C.cpp | 29 ++ self_training/2020-04-21-cf3/D.cpp | 29 ++ self_training/2020-04-21-cf3/E.cpp | 52 ++++ self_training/2020-04-21-cf3/F.cpp | 80 ++++++ self_training/2020-04-21-ks/A.cpp | 22 ++ self_training/2020-04-21-ks/B.cpp | 26 ++ self_training/2020-04-21-ks/C.cpp | 31 +++ self_training/2020-04-21-ks/D.cpp | 53 ++++ self_training/2020-04-22-cf3/A.cpp | 14 + self_training/2020-04-22-cf3/B.cpp | 18 ++ self_training/2020-04-22-cf3/C.cpp | 29 ++ self_training/2020-04-22-cf3/D.cpp | 22 ++ self_training/2020-04-22-cf3/E1.cpp | 43 +++ self_training/2020-04-22-cf3/E2.cpp | 43 +++ self_training/2020-04-22-cf3/F.cpp | 65 +++++ self_training/2020-04-23-cf1/2A.cpp | 18 ++ self_training/2020-04-23-cf1/2B.cpp | 33 +++ self_training/2020-04-23-cf1/A.cpp | 46 ++++ self_training/2020-04-23-cf1/B.cpp | 52 ++++ self_training/2020-04-23-cf1/C.cpp | 68 +++++ self_training/2020-04-23-cf1/D.cpp | 50 ++++ self_training/2020-04-23-cf1/E.cpp | 85 ++++++ self_training/2020-04-23-cf1/F.cpp | 200 ++++++++++++++ self_training/2020-04-23-cfe/A.cpp | 22 ++ self_training/2020-04-23-cfe/B.cpp | 20 ++ self_training/2020-04-23-cfe/C.cpp | 26 ++ self_training/2020-04-23-cfe/D.cpp | 30 ++ self_training/2020-04-23-cfe/E.cpp | 132 +++++++++ self_training/2020-04-23-cfe/F.cpp | 56 ++++ self_training/2020-04-23-cfe/G.cpp | 77 ++++++ self_training/2020-04-25-v/A.cpp | 27 ++ self_training/2020-04-25-v/B.cpp | 33 +++ self_training/2020-04-25-v/C.cpp | 26 ++ self_training/2020-04-25-v/D.cpp | 38 +++ self_training/2020-04-25-v/E.cpp | 46 ++++ self_training/2020-04-25-v/F-2.cpp | 102 +++++++ self_training/2020-04-25-v/F.cpp | 181 ++++++++++++ self_training/2020-04-25-v/G.cpp | 35 +++ self_training/2020-04-25-v/H.cpp | 91 ++++++ self_training/2020-04-25-v/I.cpp | 109 ++++++++ self_training/2020-05-06-cf1/2A.cpp | 13 + self_training/2020-05-06-cf1/2B.cpp | 26 ++ self_training/2020-05-06-cf1/A.cpp | 32 +++ self_training/2020-05-06-cf1/B.cpp | 67 +++++ self_training/2020-05-06-cf1/C.cpp | 71 +++++ self_training/2020-05-06-cf1/D.cpp | 77 ++++++ self_training/2020-05-06-cf1/E.cpp | 123 +++++++++ self_training/2020-05-06-cf1/F.cpp | 117 ++++++++ self_training/2020-05-08-cft/A.cpp | 12 + self_training/2020-05-08-cft/B.cpp | 22 ++ self_training/2020-05-08-cft/C.cpp | 29 ++ self_training/2020-05-09-cf4/A.cpp | 27 ++ self_training/2020-05-09-cf4/B.cpp | 33 +++ self_training/2020-05-09-cf4/C.cpp | 21 ++ self_training/2020-05-09-cf4/D.cpp | 35 +++ self_training/2020-05-09-cf4/E.cpp | 32 +++ self_training/2020-05-09-cf4/F.cpp | 65 +++++ self_training/2020-05-09-cf4/G.cpp | 37 +++ self_training/2020-05-10-cfe/A.cpp | 27 ++ self_training/2020-05-10-cfe/B.cpp | 30 ++ self_training/2020-05-10-cfe/C.cpp | 38 +++ self_training/2020-05-10-cfe/D.cpp | 50 ++++ self_training/2020-05-10-cfe/E.cpp | 49 ++++ self_training/2020-05-10-cfe/F.cpp | 100 +++++++ self_training/2020-05-17-abc/A.cpp | 14 + self_training/2020-05-17-abc/B.cpp | 23 ++ self_training/2020-05-17-abc/C.cpp | 22 ++ self_training/2020-05-17-abc/D.cpp | 42 +++ self_training/2020-05-17-abc/E.cpp | 51 ++++ self_training/2020-05-17-abc/F.cpp | 109 ++++++++ self_training/2020-05-17-ks/A.cpp | 26 ++ self_training/2020-05-17-ks/B.cpp | 58 ++++ self_training/2020-05-17-ks/C.cpp | 31 +++ self_training/2020-05-17-ks/D.cpp | 75 +++++ self_training/2020-05-23-agc/A.cpp | 37 +++ self_training/2020-05-23-agc/B.cpp | 52 ++++ self_training/2020-05-23-agc/C.cpp | 58 ++++ self_training/2020-05-23-agc/D.cpp | 101 +++++++ self_training/2020-05-23-agc/E.cpp | 51 ++++ self_training/2020-05-23-agc/F.cpp | 158 +++++++++++ self_training/2020-06-18-cf3/A.cpp | 25 ++ self_training/2020-06-18-cf3/B.cpp | 26 ++ self_training/2020-06-18-cf3/C.cpp | 42 +++ self_training/2020-06-18-cf3/D.cpp | 60 ++++ self_training/2020-06-18-cf3/E.cpp | 32 +++ self_training/2020-06-18-cf3/F.cpp | 72 +++++ self_training/2020-07-19-astar/A.cpp | 21 ++ self_training/2020-07-19-astar/B.cpp | 28 ++ self_training/2020-07-19-astar/C.cpp | 25 ++ self_training/2020-07-19-astar/D.cpp | 71 +++++ self_training/2020-07-19-astar/E.cpp | 20 ++ self_training/2020-07-19-astar/F.cpp | 102 +++++++ self_training/2020-07-19-astar/G.cpp | 54 ++++ self_training/2020-07-19-astar/H.cpp | 48 ++++ self_training/2020-08-16-fhc/A1.cpp | 63 +++++ self_training/2020-08-16-fhc/A2.cpp | 71 +++++ self_training/2020-08-16-fhc/A3.cpp | 71 +++++ self_training/2020-08-16-fhc/B.cpp | 56 ++++ self_training/2020-08-16-fhc/C.cpp | 195 +++++++++++++ 395 files changed, 23627 insertions(+) create mode 100644 Summer_Training/2016-07-23/A.cpp create mode 100644 Summer_Training/2016-07-23/B.cpp create mode 100644 Summer_Training/2016-07-23/C.cpp create mode 100644 Summer_Training/2016-07-23/D.cpp create mode 100644 Summer_Training/2016-07-23/E.cpp create mode 100644 Summer_Training/2016-07-23/F.cpp create mode 100644 Summer_Training/2016-07-23/G.cpp create mode 100644 Summer_Training/2016-07-23/H.cpp create mode 100644 Summer_Training/2016-07-23/I.cpp create mode 100644 Summer_Training/2016-07-23/J.cpp create mode 100644 Summer_Training/2016-07-23/K.cpp create mode 100644 Summer_Training/2016-07-23/L.cpp create mode 100644 Summer_Training/2016-07-26/A.cpp create mode 100644 Summer_Training/2016-07-26/C.cpp create mode 100644 Summer_Training/2016-07-26/D.cpp create mode 100644 Summer_Training/2016-07-26/F.cpp create mode 100644 Summer_Training/2016-07-26/G.cpp create mode 100644 Summer_Training/2016-07-26/H.cpp create mode 100644 Summer_Training/2016-07-26/I.cpp create mode 100644 Summer_Training/2016-08-02/H.cpp create mode 100644 Summer_Training/2016-08-02/J.cpp create mode 100644 Summer_Training/2016-08-09/H.cpp create mode 100644 Summer_Training/2016-08-11/A - Ball - tjz.cpp create mode 100644 Summer_Training/2016-08-11/C - color II - tjz.cpp create mode 100644 Summer_Training/2016-08-11/F - physics - tjz.cpp create mode 100644 Summer_Training/2016-08-11/H - Rikka with Sequence - tjz.cpp create mode 100644 Summer_Training/2016-08-11/I - Rikka with Subset - tjz.cpp create mode 100644 Summer_Training/2016-08-11/K - Rikka with Parenthesis II - tjz.cpp create mode 100644 Summer_Training/2016-08-18/A - Median - lzy.cpp create mode 100644 Summer_Training/2016-08-18/B - Hard problem - tjz.cpp create mode 100644 Summer_Training/2016-08-18/D - Death Sequence - tjz.cpp create mode 100644 Summer_Training/2016-08-18/E - Road - lzy.cpp create mode 100644 Summer_Training/2016-08-18/F - Counting Intersections - tjz.cpp create mode 100644 Summer_Training/2016-08-18/G - cjj's string game - tjz.cpp create mode 100644 Summer_Training/2016-08-18/K - Water problem - lzy.cpp create mode 100644 Summer_Training/2016-09-10/A - Different Circle Permutation - tjz.cpp create mode 100644 Summer_Training/2016-09-10/B - Different GCD Subarray Query - tjz.cpp create mode 100644 Summer_Training/2016-09-10/F - Football Games - tjz.cpp create mode 100644 Summer_Training/2016-09-10/G - Friends and Enemies - tjz.cpp create mode 100644 Summer_Training/2016-09-10/H - Function - lzy.cpp create mode 100644 Summer_Training/2016-09-10/I - Sparse Graph - lzy.cpp create mode 100644 Summer_Training/2016-09-10/J - Weak Pair - lzy.cpp create mode 100644 Summer_Training/2016-09-15/A - git - lzy.cpp create mode 100644 Summer_Training/2016-09-15/B - Circles and Squares - tjz.cpp create mode 100644 Summer_Training/2016-09-15/E - Cat and Mouse - lzy.cpp create mode 100644 Summer_Training/2016-09-15/F - Similar Strings - lzy.cpp create mode 100644 Summer_Training/2016-09-15/G - Chota Mouse and his Birthday Cake - tjz.cpp create mode 100644 Summer_Training/2016-09-15/H - Longest Palindrome - lzy.cpp create mode 100644 Summer_Training/2016-09-17/A - I Count Two Three - liuzy.cpp create mode 100644 Summer_Training/2016-09-17/B - Cure - tjz.cpp create mode 100644 Summer_Training/2016-09-17/C - Family View - tjz.cpp create mode 100644 Summer_Training/2016-09-17/E - Balanced Game - lzy.cpp create mode 100644 Summer_Training/2016-09-17/F - The Best Path - lzy.cpp create mode 100644 Summer_Training/2016-09-17/G - Sort - tjz.cpp create mode 100644 Summer_Training/2016-09-17/I - Tower Defence - tjz.cpp create mode 100644 Summer_Training/2016-09-17/K - Barricade - lzy.cpp create mode 100644 Summer_Training/2016-09-18/A - Resident Evil - tjz.cpp create mode 100644 "Summer_Training/2016-09-18/C - hannnnah_j\342\200\231s Biological Test - tjz.cpp" create mode 100644 Summer_Training/2016-09-18/D - Mathematician QSC - tjz.cpp create mode 100644 Summer_Training/2016-09-18/E - Running King - tjz.cpp create mode 100644 Summer_Training/2016-09-18/G - odd-even number - lzy.cpp create mode 100644 Summer_Training/2016-09-18/H - oasis in desert - tjz.cpp create mode 100644 Summer_Training/2016-09-18/I - QSC and Master - lzy.cpp create mode 100644 Summer_Training/2016-09-18/J - Count primes - tjz.cpp create mode 100644 Summer_Training/2016-09-19/A - Xiongnu's Land - lzy.cpp create mode 100644 Summer_Training/2016-09-19/E - Stamps - tjz.cpp create mode 100644 Summer_Training/2016-09-19/G - Mysterious Antiques in Sackler Museum - lzy.cpp create mode 100644 Summer_Training/2016-09-19/J - Osu! Master - tjz.cpp create mode 100644 Summer_Training/2016-09-19/K - A Math Problem - tjz.cpp create mode 100644 Summer_Training/2016-09-19/upsolved/I - Snake Carpet - tjz.cpp create mode 100644 Summer_Training/2016-09-24/A - The Book List - tjz.cpp create mode 100644 Summer_Training/2016-09-24/C - A Simple Job - tjz.cpp create mode 100644 Summer_Training/2016-09-24/F - Periodic Signal - tjz.cpp create mode 100644 Summer_Training/2016-10-02/B - Inspection - liuzy.cpp create mode 100644 Summer_Training/2016-10-02/C - Karmon Go - lzy.cpp create mode 100644 Summer_Training/2016-10-02/E - Autocomplete - lzy.cpp create mode 100644 Summer_Training/2016-10-02/F - Amazing Divisibility - lzy.cpp create mode 100644 Summer_Training/2016-10-02/G - Group Tournament - lzy.cpp create mode 100644 Summer_Training/2016-10-02/J - Carts - tjz.cpp create mode 100644 Summer_Training/2016-10-02/upsolved/I - Guess The Modulo - tjz.cpp create mode 100644 Summer_Training/2016-10-04/A - Nearest Neighbor Search - lzy.cpp create mode 100644 Summer_Training/2016-10-04/E - Coins - liuzy.cpp create mode 100644 Summer_Training/2016-10-04/F - Floyd-Warshall - lzy.cpp create mode 100644 Summer_Training/2016-10-04/G - Road History - tjz.cpp create mode 100644 Summer_Training/2016-10-04/H - Around the World - tjz.cpp create mode 100644 Summer_Training/2016-10-04/I - Longest Increasing Subsequence - tjz.cpp create mode 100644 Summer_Training/2016-10-04/J - Matrix Transformation - tjz.cpp create mode 100644 Summer_Training/2016-10-04/upsolved/B - Odd Discount - tjz.cpp create mode 100644 Summer_Training/2016-10-04/upsolved/D - Longest Common Subsequence - tjz.cpp create mode 100644 TDL_training/2016-04-24/upsolved/G - Boxes - tjz.cpp create mode 100644 self_training/2017-04-30/4502A.cpp create mode 100644 self_training/2017-04-30/4502B.cpp create mode 100644 self_training/2017-04-30/4502C.cpp create mode 100644 self_training/2017-04-30/4502D.cpp create mode 100644 self_training/2017-04-30/4502E.cpp create mode 100644 self_training/2017-04-30/4502F.cpp create mode 100644 self_training/2017-08-05/1001.cpp create mode 100644 self_training/2017-08-05/1002.cpp create mode 100644 self_training/2017-08-05/1003.cpp create mode 100644 self_training/2017-08-05/1004.cpp create mode 100644 self_training/2017-08-05/1005.cpp create mode 100644 self_training/2017-08-13/1001.cpp create mode 100644 self_training/2017-08-13/1003.cpp create mode 100644 self_training/2017-08-13/1004.cpp create mode 100644 self_training/2017-08-13/1005.cpp create mode 100644 self_training/2017-08-13/1006.cpp create mode 100644 self_training/2017-08-27/2A.cpp create mode 100644 self_training/2017-08-27/2B.cpp create mode 100644 self_training/2017-08-27/2C.cpp create mode 100644 self_training/2017-09-15/A.cpp create mode 100644 self_training/2017-09-15/B.cpp create mode 100644 self_training/2017-09-15/C.cpp create mode 100644 self_training/2017-09-15/D.cpp create mode 100644 self_training/2017-09-15/E.py create mode 100644 self_training/2017-09-15/F.cpp create mode 100644 self_training/2017-09-15/G.cpp create mode 100644 self_training/2017-09-15/H.cpp create mode 100644 self_training/2017-09-15/J.cpp create mode 100644 self_training/2018-01-17/7491A.cpp create mode 100644 self_training/2018-01-17/7491B.cpp create mode 100644 self_training/2018-01-17/7491C.cpp create mode 100644 self_training/2018-01-17/7491D.cpp create mode 100644 self_training/2018-01-17/7491E.cpp create mode 100644 self_training/2018-01-17/7491F.cpp create mode 100644 self_training/2018-02-24/agc21A.cpp create mode 100644 self_training/2018-02-24/agc21B.cpp create mode 100644 self_training/2018-02-24/agc21C.cpp create mode 100644 self_training/2018-02-24/agc21D.cpp create mode 100644 self_training/2018-03-13/7736B.cpp create mode 100644 self_training/2018-03-13/7736C.cpp create mode 100644 self_training/2018-03-13/7736D.cpp create mode 100644 self_training/2018-03-30/82A.cpp create mode 100644 self_training/2018-03-30/82B.cpp create mode 100644 self_training/2018-03-30/82C.cpp create mode 100644 self_training/2018-03-30/82D.cpp create mode 100644 self_training/2018-03-30/82E.cpp create mode 100644 self_training/2018-04-08-2/7989A.cpp create mode 100644 self_training/2018-04-08-2/7989B.cpp create mode 100644 self_training/2018-04-08-2/7989C.cpp create mode 100644 self_training/2018-04-08-2/7989D.cpp create mode 100644 self_training/2018-04-08/A.cpp create mode 100644 self_training/2018-04-08/B.cpp create mode 100644 self_training/2018-04-08/C.cpp create mode 100644 self_training/2018-04-08/D.cpp create mode 100644 self_training/2018-07-06/131A.cpp create mode 100644 self_training/2018-07-06/131B.cpp create mode 100644 self_training/2018-07-06/131C.cpp create mode 100644 self_training/2018-07-06/131D.cpp create mode 100644 self_training/2018-07-06/131E.cpp create mode 100644 self_training/2018-07-06/131F.cpp create mode 100644 self_training/2018-07-19/A.cpp create mode 100644 self_training/2018-07-19/B.cpp create mode 100644 self_training/2018-07-19/C.cpp create mode 100644 self_training/2018-07-19/F.cpp create mode 100644 self_training/2018-07-19/G.cpp create mode 100644 self_training/2018-07-19/I.cpp create mode 100644 self_training/2018-07-19/J.cpp create mode 100644 self_training/2018-07-21/B.cpp create mode 100644 self_training/2018-07-21/E.cpp create mode 100644 self_training/2018-07-21/G.cpp create mode 100644 self_training/2018-07-21/I.cpp create mode 100644 self_training/2018-07-21/K.cpp create mode 100644 self_training/2018-07-22/A.cpp create mode 100644 self_training/2018-07-22/B.cpp create mode 100644 self_training/2018-07-22/C.cpp create mode 100644 self_training/2018-08-04/1001.cpp create mode 100644 self_training/2018-08-04/1002.cpp create mode 100644 self_training/2018-08-04/1003.cpp create mode 100644 self_training/2018-08-04/1004-TLE.cpp create mode 100644 self_training/2018-08-04/1005.cpp create mode 100644 self_training/2018-08-04/1006.cpp create mode 100644 self_training/2018-08-11/1001.cpp create mode 100644 self_training/2018-08-11/1002.cpp create mode 100644 self_training/2018-08-11/1003.cpp create mode 100644 self_training/2018-08-11/1004.cpp create mode 100644 self_training/2018-08-11/1005.cpp create mode 100644 self_training/2018-08-18/1001.cpp create mode 100644 self_training/2018-08-18/1002.cpp create mode 100644 self_training/2018-08-18/1003.cpp create mode 100644 self_training/2018-08-18/1004.cpp create mode 100644 self_training/2018-08-18/1005.cpp create mode 100644 self_training/2018-08-18/1006.cpp create mode 100644 self_training/2018-09-01/B.cpp create mode 100644 self_training/2018-09-01/C.cpp create mode 100644 self_training/2018-09-01/G.cpp create mode 100644 self_training/2018-09-01/H.cpp create mode 100644 self_training/2018-09-15/C.cpp create mode 100644 self_training/2018-09-15/D.java create mode 100644 self_training/2018-09-15/G.cpp create mode 100644 self_training/2018-09-15/H.cpp create mode 100644 self_training/2018-09-15/K.cpp create mode 100644 self_training/2018-09-22/C.cpp create mode 100644 self_training/2018-09-22/E.py create mode 100644 self_training/2018-10-12/CHEFPRMS.cpp create mode 100644 self_training/2018-10-12/QABC.cpp create mode 100644 self_training/2018-10-12/QUALPREL.cpp create mode 100644 self_training/2018-10-12/SPREAD2.cpp create mode 100644 self_training/2018-10-12/TEAMMATE.cpp create mode 100644 self_training/2018-11-03/ANGLE.cpp create mode 100644 self_training/2018-11-03/BUDDYNIM.cpp create mode 100644 self_training/2018-11-03/PAINTREE.cpp create mode 100644 self_training/2018-11-03/STRTF.cpp create mode 100644 self_training/2018-11-09/217A.cpp create mode 100644 self_training/2018-11-09/217B.cpp create mode 100644 self_training/2018-11-09/217C.cpp create mode 100644 self_training/2018-11-09/217D.cpp create mode 100644 self_training/2018-11-09/217E.cpp create mode 100644 self_training/2018-11-09/217F.cpp create mode 100644 self_training/2018-11-30/272A.cpp create mode 100644 self_training/2018-11-30/272B.cpp create mode 100644 self_training/2018-11-30/272C.cpp create mode 100644 self_training/2018-11-30/272D.cpp create mode 100644 self_training/2018-11-30/272E.cpp create mode 100644 self_training/2018-11-30/272F.cpp create mode 100644 self_training/2018-12-01/A.cpp create mode 100644 self_training/2018-12-01/B.cpp create mode 100644 self_training/2018-12-01/C.cpp create mode 100644 self_training/2018-12-01/D.cpp create mode 100644 self_training/2018-12-01/E.cpp create mode 100644 self_training/2018-12-01/F.cpp create mode 100644 self_training/2018-12-01/G.cpp create mode 100644 self_training/2018-12-01/H.cpp create mode 100644 self_training/2018-12-01/I.cpp create mode 100644 self_training/2018-12-01/J.cpp create mode 100644 self_training/2018-12-08/ADIMAT.cpp create mode 100644 self_training/2018-12-08/ADITREE.cpp create mode 100644 self_training/2018-12-08/ADJLEAF2.cpp create mode 100644 self_training/2018-12-08/EBAIT.cpp create mode 100644 self_training/2018-12-08/GDIST.cpp create mode 100644 self_training/2018-12-08/IMPO.cpp create mode 100644 self_training/2018-12-08/RECOVER.cpp create mode 100644 self_training/2018-12-08/RECTLIT.cpp create mode 100644 self_training/2018-12-08/SFXPAL.cpp create mode 100644 self_training/2018-12-08/XORTABLE.cpp create mode 100644 self_training/2019-04-06/A.cpp create mode 100644 self_training/2019-04-06/B.cpp create mode 100644 self_training/2019-04-06/C.py create mode 100644 self_training/2019-04-06/D.cpp create mode 100644 self_training/2019-04-13/A.cpp create mode 100644 self_training/2019-04-13/B.cpp create mode 100644 self_training/2019-04-13/C.cpp create mode 100644 self_training/2019-05-18/A.cpp create mode 100644 self_training/2019-05-18/C.cpp create mode 100644 self_training/2019-05-18/D.cpp create mode 100644 self_training/2019-07-26/A.cpp create mode 100644 self_training/2019-07-26/B.cpp create mode 100644 self_training/2019-07-26/C.cpp create mode 100644 self_training/2019-07-26/D.cpp create mode 100644 self_training/2019-07-26/E.cpp create mode 100644 self_training/2019-07-28/A.cpp create mode 100644 self_training/2019-07-28/B.cpp create mode 100644 self_training/2019-07-28/C.cpp create mode 100644 self_training/2019-08-17-2/1001.cpp create mode 100644 self_training/2019-08-17-2/1002.cpp create mode 100644 self_training/2019-08-17-2/1003.cpp create mode 100644 self_training/2019-08-17-2/1005.cpp create mode 100644 self_training/2019-08-17/A.cpp create mode 100644 self_training/2019-08-17/C.cpp create mode 100644 self_training/2019-08-17/D.py create mode 100644 self_training/2019-08-17/F.cpp create mode 100644 self_training/2019-08-17/J.cpp create mode 100644 self_training/2019-08-18/A.cpp create mode 100644 self_training/2019-08-18/B.cpp create mode 100644 self_training/2019-08-18/C.cpp create mode 100644 self_training/2019-08-18/D.cpp create mode 100644 self_training/2019-08-18/E.cpp create mode 100644 self_training/2019-08-18/F.cpp create mode 100644 self_training/2019-08-23/1005.cpp create mode 100644 self_training/2019-08-23/1009.cpp create mode 100644 self_training/2019-08-23/1010.cpp create mode 100644 self_training/2019-08-23/1011.cpp create mode 100644 self_training/2019-08-31/A.cpp create mode 100644 self_training/2019-08-31/B.cpp create mode 100644 self_training/2019-08-31/C.cpp create mode 100644 self_training/2019-08-31/E.cpp create mode 100644 self_training/2019-09-07/H.cpp create mode 100644 self_training/2019-09-08/A.cpp create mode 100644 self_training/2019-09-08/E.cpp create mode 100644 self_training/2019-09-08/H.cpp create mode 100644 self_training/2019-09-14/I.cpp create mode 100644 self_training/2019-09-15/K.cpp create mode 100644 self_training/2019-09-15/L.cpp create mode 100644 self_training/2019-09-20/E.cpp create mode 100644 self_training/2019-10-20/1239A.cpp create mode 100644 self_training/2019-10-23/D.cpp create mode 100644 self_training/2019-10-25/E.cpp create mode 100644 self_training/2019-10-25/G.cpp create mode 100644 self_training/2019-10-25/H.cpp create mode 100644 self_training/2019-10-25/I.cpp create mode 100644 self_training/2019-10-25/J.cpp create mode 100644 self_training/2020-04-19/A.cpp create mode 100644 self_training/2020-04-19/B.cpp create mode 100644 self_training/2020-04-19/C.cpp create mode 100644 self_training/2020-04-19/D.cpp create mode 100644 self_training/2020-04-21-cf3/A.cpp create mode 100644 self_training/2020-04-21-cf3/B.cpp create mode 100644 self_training/2020-04-21-cf3/C.cpp create mode 100644 self_training/2020-04-21-cf3/D.cpp create mode 100644 self_training/2020-04-21-cf3/E.cpp create mode 100644 self_training/2020-04-21-cf3/F.cpp create mode 100644 self_training/2020-04-21-ks/A.cpp create mode 100644 self_training/2020-04-21-ks/B.cpp create mode 100644 self_training/2020-04-21-ks/C.cpp create mode 100644 self_training/2020-04-21-ks/D.cpp create mode 100644 self_training/2020-04-22-cf3/A.cpp create mode 100644 self_training/2020-04-22-cf3/B.cpp create mode 100644 self_training/2020-04-22-cf3/C.cpp create mode 100644 self_training/2020-04-22-cf3/D.cpp create mode 100644 self_training/2020-04-22-cf3/E1.cpp create mode 100644 self_training/2020-04-22-cf3/E2.cpp create mode 100644 self_training/2020-04-22-cf3/F.cpp create mode 100644 self_training/2020-04-23-cf1/2A.cpp create mode 100644 self_training/2020-04-23-cf1/2B.cpp create mode 100644 self_training/2020-04-23-cf1/A.cpp create mode 100644 self_training/2020-04-23-cf1/B.cpp create mode 100644 self_training/2020-04-23-cf1/C.cpp create mode 100644 self_training/2020-04-23-cf1/D.cpp create mode 100644 self_training/2020-04-23-cf1/E.cpp create mode 100644 self_training/2020-04-23-cf1/F.cpp create mode 100644 self_training/2020-04-23-cfe/A.cpp create mode 100644 self_training/2020-04-23-cfe/B.cpp create mode 100644 self_training/2020-04-23-cfe/C.cpp create mode 100644 self_training/2020-04-23-cfe/D.cpp create mode 100644 self_training/2020-04-23-cfe/E.cpp create mode 100644 self_training/2020-04-23-cfe/F.cpp create mode 100644 self_training/2020-04-23-cfe/G.cpp create mode 100644 self_training/2020-04-25-v/A.cpp create mode 100644 self_training/2020-04-25-v/B.cpp create mode 100644 self_training/2020-04-25-v/C.cpp create mode 100644 self_training/2020-04-25-v/D.cpp create mode 100644 self_training/2020-04-25-v/E.cpp create mode 100644 self_training/2020-04-25-v/F-2.cpp create mode 100644 self_training/2020-04-25-v/F.cpp create mode 100644 self_training/2020-04-25-v/G.cpp create mode 100644 self_training/2020-04-25-v/H.cpp create mode 100644 self_training/2020-04-25-v/I.cpp create mode 100644 self_training/2020-05-06-cf1/2A.cpp create mode 100644 self_training/2020-05-06-cf1/2B.cpp create mode 100644 self_training/2020-05-06-cf1/A.cpp create mode 100644 self_training/2020-05-06-cf1/B.cpp create mode 100644 self_training/2020-05-06-cf1/C.cpp create mode 100644 self_training/2020-05-06-cf1/D.cpp create mode 100644 self_training/2020-05-06-cf1/E.cpp create mode 100644 self_training/2020-05-06-cf1/F.cpp create mode 100644 self_training/2020-05-08-cft/A.cpp create mode 100644 self_training/2020-05-08-cft/B.cpp create mode 100644 self_training/2020-05-08-cft/C.cpp create mode 100644 self_training/2020-05-09-cf4/A.cpp create mode 100644 self_training/2020-05-09-cf4/B.cpp create mode 100644 self_training/2020-05-09-cf4/C.cpp create mode 100644 self_training/2020-05-09-cf4/D.cpp create mode 100644 self_training/2020-05-09-cf4/E.cpp create mode 100644 self_training/2020-05-09-cf4/F.cpp create mode 100644 self_training/2020-05-09-cf4/G.cpp create mode 100644 self_training/2020-05-10-cfe/A.cpp create mode 100644 self_training/2020-05-10-cfe/B.cpp create mode 100644 self_training/2020-05-10-cfe/C.cpp create mode 100644 self_training/2020-05-10-cfe/D.cpp create mode 100644 self_training/2020-05-10-cfe/E.cpp create mode 100644 self_training/2020-05-10-cfe/F.cpp create mode 100644 self_training/2020-05-17-abc/A.cpp create mode 100644 self_training/2020-05-17-abc/B.cpp create mode 100644 self_training/2020-05-17-abc/C.cpp create mode 100644 self_training/2020-05-17-abc/D.cpp create mode 100644 self_training/2020-05-17-abc/E.cpp create mode 100644 self_training/2020-05-17-abc/F.cpp create mode 100644 self_training/2020-05-17-ks/A.cpp create mode 100644 self_training/2020-05-17-ks/B.cpp create mode 100644 self_training/2020-05-17-ks/C.cpp create mode 100644 self_training/2020-05-17-ks/D.cpp create mode 100644 self_training/2020-05-23-agc/A.cpp create mode 100644 self_training/2020-05-23-agc/B.cpp create mode 100644 self_training/2020-05-23-agc/C.cpp create mode 100644 self_training/2020-05-23-agc/D.cpp create mode 100644 self_training/2020-05-23-agc/E.cpp create mode 100644 self_training/2020-05-23-agc/F.cpp create mode 100644 self_training/2020-06-18-cf3/A.cpp create mode 100644 self_training/2020-06-18-cf3/B.cpp create mode 100644 self_training/2020-06-18-cf3/C.cpp create mode 100644 self_training/2020-06-18-cf3/D.cpp create mode 100644 self_training/2020-06-18-cf3/E.cpp create mode 100644 self_training/2020-06-18-cf3/F.cpp create mode 100644 self_training/2020-07-19-astar/A.cpp create mode 100644 self_training/2020-07-19-astar/B.cpp create mode 100644 self_training/2020-07-19-astar/C.cpp create mode 100644 self_training/2020-07-19-astar/D.cpp create mode 100644 self_training/2020-07-19-astar/E.cpp create mode 100644 self_training/2020-07-19-astar/F.cpp create mode 100644 self_training/2020-07-19-astar/G.cpp create mode 100644 self_training/2020-07-19-astar/H.cpp create mode 100644 self_training/2020-08-16-fhc/A1.cpp create mode 100644 self_training/2020-08-16-fhc/A2.cpp create mode 100644 self_training/2020-08-16-fhc/A3.cpp create mode 100644 self_training/2020-08-16-fhc/B.cpp create mode 100644 self_training/2020-08-16-fhc/C.cpp diff --git a/Summer_Training/2016-07-23/A.cpp b/Summer_Training/2016-07-23/A.cpp new file mode 100644 index 00000000..b9a345d0 --- /dev/null +++ b/Summer_Training/2016-07-23/A.cpp @@ -0,0 +1,85 @@ +#include +#include +#include +#include +#include +const int maxn = 256; +int t, n, m, kk, tot, cur, pre = 1; +char str[maxn + 1][maxn + 1]; +std::pair a[maxn]; +std::bitset f[maxn << 1], g[2][maxn]; +int main() +{ + scanf("%d", &t); + for(int Case = 1; Case <= t; ++Case) + { + scanf("%d%d%d", &n, &m, &kk); + for(int i = 0; i < n; ++i) + scanf("%s", str[i]); + for(int i = 0; i < kk; ++i) + { + int x, y; + scanf("%d%d", &x, &y); + a[i] = std::make_pair(--x, --y); + } + std::sort(a, a + kk); + for(int i = 0; i < m; ++i) + { + a[kk++] = std::make_pair(n, i); + str[n][i] = 'W'; + } + tot = 0; + for(int i = 0; i < m; ++i) + { + g[pre][i].reset(); + g[cur][i].reset(); + g[cur][i].set(i); + } + for(int i = 0, j = 0; i <= n; ++i) + { + for( ; j < kk && a[j].first == i; ++j) + f[tot++] = g[cur][a[j].second]; + if(i == n) + break; + pre ^= 1; + cur ^= 1; + for(int j = 0; j < m; ++j) + { + g[cur][j] ^= g[pre][j]; + if(j) + g[cur][j] ^= g[pre][j - 1]; + if(j + 1 < m) + g[cur][j] ^= g[pre][j + 1]; + if(str[i][j] == 'B') + g[cur][j].flip(m); + } + } + bool flag = 1; + int r = 0; + for(int i = 0; i < m; ++i) + { + int k = -1; + for(int j = r; j < tot; ++j) + if(f[j].test(i)) + { + k = j; + break; + } + if(k == -1) + continue; + std::swap(f[r], f[k]); + for(int j = i + 1; j < tot; ++j) + if(f[j].test(i)) + f[j] ^= f[i]; + ++r; + } + for(int i = r; i < tot; ++i) + if(f[i].test(m)) + { + flag = 0; + break; + } + printf("Case #%d:\n%s\n", Case, flag ? "YES" : "NO"); + } + return 0; +} diff --git a/Summer_Training/2016-07-23/B.cpp b/Summer_Training/2016-07-23/B.cpp new file mode 100644 index 00000000..9fd9045a --- /dev/null +++ b/Summer_Training/2016-07-23/B.cpp @@ -0,0 +1,122 @@ +#include +#include +#include +#include +const int maxn = 20010, maxs = ((maxn - 1) >> 6) + 1, maxd = 1 << 4; +int t, n, h[maxn << 1], lbt[1 << maxd]; +char str[maxn], buf[maxn << 1 | 1]; +struct Bitset +{ + int n; + size_t len; + unsigned long long data[maxs]; + void init(int n) + { + this -> n = n; + len = ((n - 1) >> 6) + 1; + memset(data, 0, len * sizeof data[0]); + } + void set(size_t pos) + { + data[pos >> 6] |= 1ULL << (pos & 63); + } + Bitset operator & (const Bitset &t) const + { + Bitset ret = {std::min(n, t.n), std::min(len, t.len)}; + for(size_t i = 0; i < ret.len; ++i) + ret.data[i] = data[i] & t.data[i]; + return ret; + } + void shift_left_one() + { + int last = 0, cur; + for(int i = 0; i < (int)len; ++i) + { + cur = (data[i] >> 63) & 1; + data[i] <<= 1; + data[i] |= last; + last = cur; + } + ++n; + if(n > (int)(len << 6)) + data[len++] = last; + } + void shift_right_one() + { + int last = 0, cur; + for(int i = len - 1; i >= 0; --i) + { + cur = data[i] & 1; + data[i] >>= 1; + if(last) + data[i] |= 1ULL << 63; + last = cur; + } + --n; + if(n <= (int)((len - 1) << 6)) + --len; + } + int lowbit() const + { + for(int i = 0; i < (int)len; ++i) + if(data[i]) + { + unsigned long long tmp = data[i]; + for(int j = 0; j < 4; ++j, tmp >>= maxd) + if(tmp & ((1 << maxd) - 1)) + return (i << 6) + (j << 4) + lbt[(int)(tmp & ((1 << maxd) - 1))]; + } + return (int)(~0u >> 1); + } +} f, g, w; +int main() +{ + for(int i = 1; i < 1 << maxd; ++i) + lbt[i] = i & 1 ? 0 : lbt[i >> 1] + 1; + scanf("%d", &t); + while(t--) + { + scanf("%s", str); + n = strlen(str); + for(int i = 0; i < n; ++i) + { + buf[i << 1] = str[i]; + buf[i << 1 | 1] = '#'; + } + buf[n << 1] = '\0'; + for(int i = 0, mx = 0, id = 0; i <= (n - 1) << 1; ++i) + { + h[i] = i < mx ? std::min(mx - i, h[(id << 1) - i]) : 0; + for( ; h[i] <= i && buf[i - h[i]] == buf[i + h[i]]; ++h[i]); + if(mx < i + h[i]) + { + mx = i + h[i]; + id = i; + } + } + f.init(1); + f.set(0); + g.init(n); + for(int i = 0; i < n; ++i) + if(h[i + n - 1] > n - i - 1) + g.set(i); + bool flag = 0; + for(int i = 0; i <= (n - 1) << 1 && !flag; ++i) + { + int k = (h[i] - (i & 1) - (~(i - h[i]) & 1)) >> 1; + w = f & g; + w.shift_right_one(); + flag |= w.lowbit() <= k; + if(i & 1) + { + f.shift_left_one(); + if(h[(i + 1) >> 1] > ((i + 1) >> 1)) + f.set(0); + } + else + g.shift_right_one(); + } + puts(flag ? "Yes" : "No"); + } + return 0; +} diff --git a/Summer_Training/2016-07-23/C.cpp b/Summer_Training/2016-07-23/C.cpp new file mode 100644 index 00000000..628cfa72 --- /dev/null +++ b/Summer_Training/2016-07-23/C.cpp @@ -0,0 +1,99 @@ +#include +#include +#include +const int maxn = 100010, maxd = 18, maxs = 11; +struct List +{ + int len, val[maxs]; + void insert(int x) + { + if(len < maxs) + val[len++] = x; + } + List merge(const List &t) const + { + List ret = {std::min(len + t.len, maxs)}; + for(int i = 0, j = 0, k = 0; (i < len || j < t.len) && k < ret.len; ++k) + if(j >= t.len || i < len && val[i] <= t.val[j]) + ret.val[k] = val[i++]; + else + ret.val[k] = t.val[j++]; + return ret; + } +} st[maxd][maxn], ans; +int n, m, q, dep[maxn], fa[maxd][maxn]; +std::vector e[maxn]; +void dfs(int u) +{ + for(std::vector::iterator it = e[u].begin(); it != e[u].end(); ++it) + { + int v = *it; + if(v == fa[0][u]) + continue; + dep[v] = dep[u] + 1; + fa[0][v] = u; + for(int d = 1; fa[d - 1][v]; ++d) + { + fa[d][v] = fa[d - 1][fa[d - 1][v]]; + st[d][v] = st[d - 1][v].merge(st[d - 1][fa[d - 1][v]]); + } + dfs(v); + } +} +void query(int u, int v, List &ans) +{ + ans = (List){0}; + if(dep[u] > dep[v]) + std::swap(u, v); + for(int d = 0, i = dep[v] - dep[u]; i > 0; ++d, i >>= 1) + if(i & 1) + { + ans = ans.merge(st[d][v]); + v = fa[d][v]; + } + if(u == v) + { + ans = ans.merge(st[0][u]); + return; + } + for(int d = maxd - 1; d >= 0; --d) + if(fa[d][u] != fa[d][v]) + { + ans = ans.merge(st[d][u]); + u = fa[d][u]; + ans = ans.merge(st[d][v]); + v = fa[d][v]; + } + ans = ans.merge(st[0][u]).merge(st[0][v]).merge(st[0][fa[0][u]]); +} +int main() +{ + scanf("%d%d%d", &n, &m, &q); + for(int i = 1; i < n; ++i) + { + int u, v; + scanf("%d%d", &u, &v); + e[u].push_back(v); + e[v].push_back(u); + } + for(int i = 1; i <= m; ++i) + { + int x; + scanf("%d", &x); + st[0][x].insert(i); + } + dfs(1); + while(q--) + { + int u, v, w; + scanf("%d%d%d", &u, &v, &w); + query(u, v, ans); + if(w > ans.len) + w = ans.len; + printf("%d", w); + for(int i = 0; i < w; ++i) + printf(" %d", ans.val[i]); + putchar('\n'); + } + return 0; +} diff --git a/Summer_Training/2016-07-23/D.cpp b/Summer_Training/2016-07-23/D.cpp new file mode 100644 index 00000000..db51501e --- /dev/null +++ b/Summer_Training/2016-07-23/D.cpp @@ -0,0 +1,47 @@ +#include +#include +#include +typedef long long LL; +const int maxn = 2001; +int n, x[maxn], y[maxn], fm[maxn]; +LL ans; +std::map, int> Hash; +int main() +{ + scanf("%d", &n); + for(int i = 0; i < n; ++i) + { + int a, b, c; + scanf("%d%d%d", &a, &b, &c); + x[i] = a * c; + y[i] = b * c; + fm[i] = a * a + b * b; + } + for(int i = 0; i < n; ++i) + { + std::map, int>().swap(Hash); + for(int j = i + 1; j < n; ++j) + { + LL p = (LL)x[i] * fm[j] - (LL)x[j] * fm[i]; + LL q = (LL)y[i] * fm[j] - (LL)y[j] * fm[i]; + if(p < 0) + { + p = -p; + q = -q; + } + else if(!p && q < 0) + q = -q; + else if(!p && !q) + { + ans += n - i - 2; + continue; + } + LL r = std::__gcd(p, q); + p /= r; + q /= r; + ans += Hash[std::make_pair(p, q)]++; + } + } + printf("%I64d\n", ans); + return 0; +} diff --git a/Summer_Training/2016-07-23/E.cpp b/Summer_Training/2016-07-23/E.cpp new file mode 100644 index 00000000..9a6b65cf --- /dev/null +++ b/Summer_Training/2016-07-23/E.cpp @@ -0,0 +1,61 @@ +#include +typedef long long LL; +const int maxn = 20, maxm = 100000, maxs = 1 << maxn, mod = 1000000007; +int n, m, val[maxm], len, inv_len, x[maxs], sz[maxs]; +char str[maxm + 1]; +inline void FWT_iterate(int x[], int len, int delta) +{ + for(int i = 0, j, t, px = 0, py = delta; i < len; i += delta << 1, px = py, py += delta) + for(j = 0; j < delta; ++j, ++px, ++py) + { + t = x[py]; + x[py] = x[px] - t < 0 ? x[px] - t + mod : x[px] - t; + x[px] = x[px] + t >= mod ? x[px] + t - mod : x[px] + t; + } +} +inline void FWT(int x[], int len, int flag) +{ + if(!flag) + for(int i = 1; i < len; i <<= 1) + FWT_iterate(x, len, i); + else + { + for(int i = len >> 1; i > 0; i >>= 1) + FWT_iterate(x, len, i); + for(int i = 0; i < len; ++i) + x[i] = (LL)x[i] * inv_len % mod; + } +} +int main() +{ + scanf("%d%d", &n, &m); + for(int i = 0; i < n; ++i) + { + scanf("%s", str); + for(int j = 0; j < m; ++j) + if(str[j] == '1') + val[j] |= 1 << i; + } + for(int i = 0; i < m; ++i) + ++x[val[i]]; + len = 1 << n; + inv_len = 1; + for(int i = 0; i < n; ++i) + inv_len = (LL)inv_len * ((mod + 1) >> 1) % mod; + for(int i = 1; i < len; ++i) + sz[i] = sz[i >> 1] + (i & 1); + for(int i = 0; i < len; ++i) + if(sz[i] > n - sz[i]) + sz[i] = n - sz[i]; + FWT(x, len, 0); + FWT(sz, len, 0); + for(int i = 0; i < len; ++i) + x[i] = (LL)x[i] * sz[i] % mod; + FWT(x, len, 1); + int ans = n * m; + for(int i = 0; i < len; ++i) + if(ans > x[i]) + ans = x[i]; + printf("%d\n", ans); + return 0; +} diff --git a/Summer_Training/2016-07-23/F.cpp b/Summer_Training/2016-07-23/F.cpp new file mode 100644 index 00000000..3ffdb0dc --- /dev/null +++ b/Summer_Training/2016-07-23/F.cpp @@ -0,0 +1,88 @@ +#include +#include +#include +typedef unsigned int UL; +const int maxp = 131, maxq = 2000001, maxs = 1 << 14, maxd = 1415; +UL tot, prime[maxp], cnt[maxp]; +UL C(UL x, UL y) +{ + UL ret = 1; + memset(cnt, 0, tot * sizeof(UL)); + for(UL i = 0; i < y; ++i) + { + { + UL val = x - i; + for(UL j = 0; j < tot; ++j) + for( ; val % prime[j] == 0; ++cnt[j], val /= prime[j]); + ret *= val; + } + { + UL val = i + 1; + for(UL j = 0; j < tot; ++j) + for( ; val % prime[j] == 0; --cnt[j], val /= prime[j]); + //ret *= mod_inv(val); + } + } + for(UL i = 0; i < tot; ++i) + for( ; cnt[i]--; ret *= prime[i]); + return ret; +} +struct Matrix +{ + UL r, c, num[maxp][maxp]; + Matrix operator * (const Matrix &t) const + { + Matrix ret = {r, t.c}; + for(UL i = 0; i < r; ++i) + for(UL j = 0; j < c; ++j) + for(UL k = 0; k < t.c; ++k) + ret.num[i][k] += num[i][j] * t.num[j][k]; + return ret; + } +} g[maxd]; +UL n, p, q, sz, f[maxs][maxp], ans; +int main() +{ + scanf("%u%u%u", &n, &p, &q); + if(p >= n) + p = n - 1; + sz = 1; + for(UL tmp = sqrt((p ? p : 1) * q); sz < tmp; sz <<= 1); + for(UL i = 2; i <= p; ++i) + { + if(!cnt[i]) + prime[tot++] = i; + for(UL j = 0; j < tot && i * prime[j] <= p; ++j) + { + cnt[i * prime[j]] = 1; + if(i % prime[j] == 0) + break; + } + } + g[1].r = g[1].c = p + 1; + for(UL i = 0; i <= p; ++i) + for(UL j = i; j <= p; ++j) + g[1].num[i][j] = C(n - p + j, j - i); + for(UL i = 0; i <= p; ++i) + f[0][i] = 1; + for(UL i = 1; i < sz; ++i) + for(UL j = 0; j <= p; ++j) + for(UL k = j; k <= p; ++k) + f[i][k] += f[i - 1][j] * g[1].num[j][k]; + g[0].r = g[0].c = p + 1; + for(UL i = 0; i <= p; ++i) + g[0].num[i][i] = 1; + for(UL i = 1; i < sz; i <<= 1) + g[1] = g[1] * g[1]; + for(UL i = 2; i * sz <= q; ++i) + g[i] = g[i - 1] * g[1]; + for(UL i = 1, x = sz == 1, y = sz > 1; i <= q; ++i, y == sz - 1 ? ++x, y = 0 : ++y) + { + UL tmp = 0; + for(UL j = 0; j <= p; ++j) + tmp += f[y][j] * g[x].num[j][p]; + ans ^= i * tmp; + } + printf("%u\n", ans); + return 0; +} diff --git a/Summer_Training/2016-07-23/G.cpp b/Summer_Training/2016-07-23/G.cpp new file mode 100644 index 00000000..ca8afb19 --- /dev/null +++ b/Summer_Training/2016-07-23/G.cpp @@ -0,0 +1,89 @@ +#include +#include +const int n = 4, maxd = 4, dx[maxd] = {-1, 0, 1, 0}, dy[maxd] = {0, -1, 0, 1}, INF = 0x3f3f3f3f; +int t, x[n], y[n], px[n], py[n], perm[n], dir[n], seg[4][2], len[4], pos[4]; +bool dfs(int dep, int lim) +{ + if(dep == n) + { + for(int i = 0; i < 4; ++i) + { + seg[i][0] = -INF; + seg[i][1] = INF; + } + for(int i = 0; i < n; ++i) + { + int ox = perm[i] & 1, oy = (perm[i] >> 1) & 1, L, R; + L = x[i], R = x[i] + dx[dir[i]] * lim; + if(L > R) + std::swap(L, R); + seg[ox][0] = std::max(seg[ox][0], L); + seg[ox][1] = std::min(seg[ox][1], R); + L = y[i], R = y[i] + dy[dir[i]] * lim; + if(L > R) + std::swap(L, R); + seg[2 + oy][0] = std::max(seg[2 + oy][0], L); + seg[2 + oy][1] = std::min(seg[2 + oy][1], R); + } + for(int i = 0; i < 4; ++i) + if(seg[i][0] > seg[i][1]) + return 0; + for(int i = 0; i < 4; ++i) + len[i] = (seg[i][1] - seg[i ^ 1][0]) * (i & 1 ? 1 : -1); + if((len[0] = std::max(len[0], len[2])) > (len[1] = std::min(len[1], len[3]))) + return 0; + for(int i = 0; i < 4; ++i) + pos[i] = i & 1 ? pos[i - 1] + len[0] : std::max(seg[i][0], seg[i ^ 1][0] - len[0]); + for(int i = 0; i < n; ++i) + { + px[i] = pos[perm[i] & 1]; + py[i] = pos[2 + ((perm[i] >> 1) & 1)]; + } + return 1; + } + for(int i = 0; i < maxd; ++i) + { + dir[dep] = i; + if(dfs(dep + 1, lim)) + return 1; + } + return 0; +} +bool check(int lim) +{ + for(int i = 0; i < n; ++i) + perm[i] = i; + do + { + if(dfs(0, lim)) + return 1; + } while(std::next_permutation(perm, perm + n)); + return 0; +} +int main() +{ + scanf("%d", &t); + while(t--) + { + for(int i = 0; i < n; ++i) + scanf("%d%d", x + i, y + i); + int L = 0, R = INF; + while(L < R) + { + int M = L + R >> 1; + if(check(M)) + R = M; + else + L = M + 1; + } + if(check(L)) + { + printf("%d\n", L); + for(int i = 0; i < 4; ++i) + printf("%d %d\n", px[i], py[i]); + } + else + puts("-1"); + } + return 0; +} diff --git a/Summer_Training/2016-07-23/H.cpp b/Summer_Training/2016-07-23/H.cpp new file mode 100644 index 00000000..7e413fb8 --- /dev/null +++ b/Summer_Training/2016-07-23/H.cpp @@ -0,0 +1,52 @@ +#include +#include +const int maxn = 301; +int t, n, m, a[maxn], f[maxn][maxn], g[maxn][maxn]; +inline void upd(int &x, int y) +{ + if(x < y) + x = y; +} +int main() +{ + scanf("%d", &t); + while(t--) + { + scanf("%d%d", &n, &m); + for(int i = 1; i <= n; ++i) + { + scanf("%d", a + i); + memset(f[i] + i, 0, (n - i + 1) * sizeof(int)); + memset(g[i] + i, 0, (n - i + 1) * sizeof(int)); + } + for(int i = 1, x; i <= m; ++i) + { + scanf("%d", &x); + for(int j = 1; j <= n; ++j) + for(int k = j + 1; k <= n; ++k) + f[j][k] |= a[k] - a[j] == x; + } + for(int l = 2; l <= n; ++l) + for(int i = 1, j = l; j <= n; ++i, ++j) + { + upd(g[i][j], g[i + 1][j]); + upd(g[i][j], g[i][j - 1]); + if(f[i][j] && g[i + 1][j - 1] == j - i - 1) + upd(g[i][j], j - i + 1); + for(int k = i + 1; k < j; ++k) + { + upd(g[i][j], g[i][k] + g[k + 1][j]); + bool flag1 = f[i][k] && g[i + 1][k - 1] == k - i - 1; + bool flag2 = f[k][j] && g[k + 1][j - 1] == j - k - 1; + if(flag1) + upd(g[i][j], g[i + 1][k - 1] + 2 + g[k + 1][j]); + if(flag2) + upd(g[i][j], g[i][k - 1] + 2 + g[k + 1][j - 1]); + if(flag1 && flag2 && a[k] - a[i] == a[j] - a[k]) + upd(g[i][j], j - i + 1); + } + } + printf("%d\n", g[1][n]); + } + return 0; +} diff --git a/Summer_Training/2016-07-23/I.cpp b/Summer_Training/2016-07-23/I.cpp new file mode 100644 index 00000000..60e80638 --- /dev/null +++ b/Summer_Training/2016-07-23/I.cpp @@ -0,0 +1,64 @@ +#include +#include +#include +const int maxn = 400001; +int n, m, a[maxn], L[maxn], R[maxn], cnt[maxn], mx, g[maxn]; +struct Query +{ + int idx, pos, val, pL, pR; + bool operator < (const Query &t) const + { + return pos < t.pos; + } +} que[maxn]; +bool cmp(const Query &x, const Query &y) +{ + return x.idx < y.idx; +} +int main() +{ + scanf("%d%d", &n, &m); + for(int i = 0; i < n; ++i) + scanf("%d", a + i); + for(int i = 0; i < m; ++i) + { + scanf("%d%d", &que[i].pos, &que[i].val); + --que[i].pos; + que[i].idx = i; + } + std::sort(que, que + m); + // pre + for(int i = 0, j = 0; i < n; ++i) + { + for( ; j < m && que[j].pos == i; ++j) + que[j].pL = std::lower_bound(g + 1, g + mx + 1, que[j].val) - g; + L[i] = std::lower_bound(g + 1, g + mx + 1, a[i]) - g; + g[L[i]] = a[i]; + if(mx < L[i]) + ++mx; + } + // suf + mx = 0; + for(int i = n - 1, j = m - 1; i >= 0; --i) + { + for( ; j >= 0 && que[j].pos == i; --j) + que[j].pR = std::lower_bound(g + 1, g + mx + 1, -que[j].val) - g; + R[i] = std::lower_bound(g + 1, g + mx + 1, -a[i]) - g; + g[R[i]] = -a[i]; + if(mx < R[i]) + ++mx; + } + // pro + for(int i = 0; i < n; ++i) + if(L[i] + R[i] > mx) + ++cnt[L[i]]; + std::sort(que, que + m, cmp); + for(int i = 0; i < m; ++i) + if(que[i].pL + que[i].pR > mx) + printf("%d\n", que[i].pL + que[i].pR - 1); + else if(L[que[i].pos] + R[que[i].pos] > mx && cnt[L[que[i].pos]] == 1) + printf("%d\n", mx - 1); + else + printf("%d\n", mx); + return 0; +} diff --git a/Summer_Training/2016-07-23/J.cpp b/Summer_Training/2016-07-23/J.cpp new file mode 100644 index 00000000..70158974 --- /dev/null +++ b/Summer_Training/2016-07-23/J.cpp @@ -0,0 +1,88 @@ +#include +#include +#include +#include +const int maxn = 1000001; +int n, m, a[maxn], b[maxn], fa[maxn]; +std::pair c[maxn]; +std::vector e[maxn]; +int find(int x) +{ + return fa[x] < 0 ? x : fa[x] = find(fa[x]); +} +void merge(int u, int v) +{ + if((u = find(u)) == (v = find(v))) + return; + if(fa[u] == fa[v]) + { + fa[v] = u; + --fa[u]; + } + else if(fa[u] < fa[v]) + fa[v] = u; + else + fa[u] = v; +} +int dfs(int u) +{ + if(b[u]) + return b[u]; + int ret = 0; + for(std::vector::iterator it = e[u].begin(); it != e[u].end(); ++it) + ret = std::max(ret, dfs(*it)); + return b[u] = ret + 1; +} +int main() +{ + scanf("%d%d", &n, &m); + for(int i = 0; i < n * m; ++i) + scanf("%d", a + i); + memset(fa, -1, n * m * sizeof(int)); + for(int i = 0; i < n; ++i) + { + for(int j = 0; j < m; ++j) + c[j] = std::make_pair(a[i * m + j], i * m + j); + std::sort(c, c + m); + for(int j = 0, k = 0; j < m; j = k) + for( ; k < m && c[j].first == c[k].first; ++k) + merge(c[j].second, c[k].second); + } + for(int i = 0; i < m; ++i) + { + for(int j = 0; j < n; ++j) + c[j] = std::make_pair(a[j * m + i], j * m + i); + std::sort(c, c + n); + for(int j = 0, k = 0; j < n; j = k) + for( ; k < n && c[j].first == c[k].first; ++k) + merge(c[j].second, c[k].second); + } + for(int i = 0; i < n; ++i) + { + for(int j = 0; j < m; ++j) + c[j] = std::make_pair(a[i * m + j], i * m + j); + std::sort(c, c + m); + for(int j = 0, k = 0; j < m; j = k) + { + for( ; k < m && c[j].first == c[k].first; ++k); + if(j) + e[find(c[j].second)].push_back(find(c[j - 1].second)); + } + } + for(int i = 0; i < m; ++i) + { + for(int j = 0; j < n; ++j) + c[j] = std::make_pair(a[j * m + i], j * m + i); + std::sort(c, c + n); + for(int j = 0, k = 0; j < n; j = k) + { + for( ; k < n && c[j].first == c[k].first; ++k); + if(j) + e[find(c[j].second)].push_back(find(c[j - 1].second)); + } + } + for(int i = 0; i < n; ++i) + for(int j = 0; j < m; ++j) + printf("%d%c", dfs(find(i * m + j)), " \n"[j == m - 1]); + return 0; +} diff --git a/Summer_Training/2016-07-23/K.cpp b/Summer_Training/2016-07-23/K.cpp new file mode 100644 index 00000000..02bef9a4 --- /dev/null +++ b/Summer_Training/2016-07-23/K.cpp @@ -0,0 +1,34 @@ +#include +#include +const int maxn = 100; +int n, a[maxn], tot, que[maxn]; +char str[maxn][maxn + 1]; +int main() +{ + scanf("%d", &n); + for(int i = 0; i < n; ++i) + scanf("%s", str[i]); + for(int i = 0; i < n; ++i) + scanf("%d", a + i); + for(int i = 0; i < n; ++i) + { + int k = -1; + for(int j = 0; j < n; ++j) + if(!a[j]) + { + k = j; + break; + } + if(k == -1) + break; + que[tot++] = k; + for(int j = 0; j < n; ++j) + if(str[k][j] == '1') + --a[j]; + } + std::sort(que, que + tot); + printf("%d\n", tot); + for(int i = 0; i < tot; ++i) + printf("%d%c", que[i] + 1, " \n"[i == tot - 1]); + return 0; +} diff --git a/Summer_Training/2016-07-23/L.cpp b/Summer_Training/2016-07-23/L.cpp new file mode 100644 index 00000000..17a67d54 --- /dev/null +++ b/Summer_Training/2016-07-23/L.cpp @@ -0,0 +1,15 @@ +#include +int n, m, od, ev, fi, se; +int main() +{ + scanf("%d%d", &n, &m); + for(int i = 0, x; i < n; ++i) + { + scanf("%d", &x); + x & 1 ? ++od : ++ev; + } + fi = n - m >> 1; + se = n - m - fi; + puts(n == m && (~od & 1) || m < n && (od <= fi || (~m & 1) && ev <= fi || (~(n - m) & 1) && ((~m & 1) || se < ev)) ? "Daenerys" : "Stannis"); + return 0; +} diff --git a/Summer_Training/2016-07-26/A.cpp b/Summer_Training/2016-07-26/A.cpp new file mode 100644 index 00000000..77848d57 --- /dev/null +++ b/Summer_Training/2016-07-26/A.cpp @@ -0,0 +1,127 @@ +#include +#include +#include +#include +typedef long long LL; +const int maxn = 100000, maxd = 100, maxt = 1; +int n, d, mod, a[maxn][maxd], b[maxn], c[maxn], e[maxn], f[maxn]; +inline void scan(int &x) +{ + register int ch; + while((ch = getchar()) < '0' || ch > '9'); + for(x = ch - '0'; (ch = getchar()) >= '0' && ch <= '9'; x = (x << 3) + (x << 1) + (ch - '0')); +} +int main() +{ + int p = -1, q = -1; + scan(n), scan(d), scan(mod); + for(int i = 0; i < n; ++i) + { + for(int j = 0; j < d; ++j) + { + scan(a[i][j]); + a[i][j] %= mod; + b[i] += a[i][j] * a[i][j]; + } + b[i] = ((mod & 1 ? (LL)b[i] * b[i] : b[i]) + n - 1) % mod; + } + if((LL)n * n * d <= (LL)1e8) + { + for(int i = 0; i < n; ++i) + for(int j = i + 1; j < n; ++j) + { + int val = 0; + for(int k = 0; k < d; ++k) + val += a[i][k] * a[j][k]; + if(val % mod == 0) + { + p = i + 1; + q = j + 1; + break; + } + } + printf("%d %d\n", p, q); + return 0; + } + //srand(time(NULL)); + for(int t = 0; t < maxt; ++t) + { + for(int i = 0; i < n; ++i) + c[i] = 1; //rand() % mod; + if(mod & 1) // mod = 3 + { + int len = d * d; + memset(e, 0, len * sizeof(int)); + for(int i = 0; i < n; ++i) + for(int j = 0, k = 0, o = 0; j < d; ++o, (k == d - 1 ? (++j, k = 0) : ++k)) + e[o] += c[i] * a[i][j] * a[i][k]; + for(int i = 0; i < len; ++i) + e[i] %= mod; + memset(f, 0, n * sizeof(int)); + for(int i = 0; i < n; ++i) + for(int j = 0, k = 0, o = 0; j < d; ++o, (k == d - 1 ? (++j, k = 0) : ++k)) + f[i] += e[o] * a[i][j] * a[i][k]; + for(int i = 0; i < n; ++i) + if((f[i] - b[i] * c[i]) % mod) + { + for(int j = 0; j < n; ++j) + { + if(i == j) + continue; + int val = 0; + for(int k = 0; k < d; ++k) + val += a[i][k] * a[j][k]; + if(val % mod == 0) + { + p = i + 1; + q = j + 1; + break; + } + } + if(p != -1) + break; + } + } + else // mod = 2 + { + memset(e, 0, d * sizeof(int)); + for(int i = 0; i < n; ++i) + for(int j = 0; j < d; ++j) + e[j] ^= c[i] & a[i][j]; + memset(f, 0, n * sizeof(int)); + for(int i = 0; i < n; ++i) + for(int j = 0; j < d; ++j) + f[i] ^= e[j] & a[i][j]; + for(int i = 0; i < n; ++i) + if(f[i] != (b[i] & c[i])) + { + for(int j = 0; j < n; ++j) + { + if(i == j) + continue; + int val = 0; + for(int k = 0; k < d; ++k) + val ^= a[i][k] & a[j][k]; + if(!val) + { + p = i + 1; + q = j + 1; + break; + } + } + if(p != -1) + break; + } + } + if(p != -1) + break; + } + if(p > q) + { + int t = p; + p = q; + q = t; + } + printf("%d %d\n", p, q); + return 0; +} diff --git a/Summer_Training/2016-07-26/C.cpp b/Summer_Training/2016-07-26/C.cpp new file mode 100644 index 00000000..f762b008 --- /dev/null +++ b/Summer_Training/2016-07-26/C.cpp @@ -0,0 +1,50 @@ +#include +const int maxn = 200001; +int n, a[maxn], b[maxn], c[maxn], s[maxn], tag[maxn]; +double ans; +int main() +{ + scanf("%d", &n); + for(int i = 1; i <= n; ++i) + scanf("%d", a + i); + for(int i = 1; i <= n; ++i) + scanf("%d", b + i); + for(int i = 1; i <= n; ++i) + c[b[i]] = i; + for(int i = 1; i <= n; ++i) + a[i] = c[a[i]]; + for(int i = 1; i <= n; ++i) + b[a[i]] = i; + a[n + 1] = b[n + 1] = 0; + s[1] = 1; + ++tag[1]; + --tag[2]; + for(int i = 2; i <= n; ++i) + { + if(b[i] > b[i + 1]) + { + s[i] = 1; + ++tag[i]; + --tag[i + 1]; + } + s[i] += s[i - 1]; + } + for(int i = 1; i <= n; ++i) + if(s[a[i] - 1] < s[a[i + 1] - 1]) + { + ++tag[a[i]]; + --tag[a[i + 1]]; + } + for(int i = 1; i <= n; ++i) + { + tag[i] += tag[i - 1]; + if(tag[i]) + ans += s[i] - s[i - 1]; + else + ans += 0.5; + } + printf("%.3f\n", ans - 1e-3); + printf("%.3f\n", ans); + printf("%.3f\n", ans + 1e-3); + return 0; +} diff --git a/Summer_Training/2016-07-26/D.cpp b/Summer_Training/2016-07-26/D.cpp new file mode 100644 index 00000000..57d9da22 --- /dev/null +++ b/Summer_Training/2016-07-26/D.cpp @@ -0,0 +1,101 @@ +#include +#include +using namespace std; +typedef long long LL; +LL p = 10000000000000LL; +LL mod_add(LL x, LL y) +{ + x += y; + if(x >= p) + x -= p; + return x; +} +LL mod_sub(LL x, LL y) +{ + x -= y; + if(x < 0) + x += p; + return x; +} +const int delen = 19, delta = 1 << delen, deltb = delta - 1; +LL mod_mul(LL x, LL y) +{ + LL ret = 0; + if(x < y) + swap(x, y); + while(y) + { + if(y & deltb) + ret = mod_add(ret, x * (y & deltb) % p); + x = x * delta % p; + y >>= delen; + } + return ret; +} +struct Fibonacci +{ + LL num1, num2; + bool operator == (const Fibonacci &x) const + { + return num1 == x.num1 && num2 == x.num2; + } + Fibonacci operator * (const Fibonacci &x) const + { + return (Fibonacci){mod_add(mod_mul(num1, x.num1), mod_mul(num2, x.num2)), + mod_add(mod_mul(num1, x.num2), mod_mul(num2, mod_add(x.num1, x.num2)))}; + } + Fibonacci pow(LL k) + { + Fibonacci ret = (Fibonacci){1, 0}, tmp = *this; + while(k) + { + if(k & 1) + ret = ret * tmp; + tmp = tmp * tmp; + k >>= 1; + } + return ret; + } +} A = {0, 1}, I = {1, 0}; +const int maxs = 2001; +int L, R; +LL n, que[maxs]; +int main() +{ + scanf("%I64d", &n); + que[R++] = 0; + for(LL md = 10, sd = 1; md <= p; md *= 10) + { + int RR = R, sz = md <= 10 ? 60 : md <= 1000 ? 5 : 10; + while(L != R) + { + LL now = que[L++]; + if(L == maxs) + L = 0; + for(int i = 0; i < sz; ++i, now += sd) + if((n - A.pow(now).num2) % md == 0) + { + que[RR++] = now; + if(RR == maxs) + RR = 0; + } + } + R = RR; + sd *= sz; + } + if(L == R) + puts("-1"); + else + { + LL ans = que[L]; + while(L != R) + { + if(ans > que[L]) + ans = que[L]; + if(++L == maxs) + L = 0; + } + printf("%I64d\n", ans); + } + return 0; +} diff --git a/Summer_Training/2016-07-26/F.cpp b/Summer_Training/2016-07-26/F.cpp new file mode 100644 index 00000000..1738eae4 --- /dev/null +++ b/Summer_Training/2016-07-26/F.cpp @@ -0,0 +1,107 @@ +#include +#include +#include +const int maxn = 100001; +int t, n, rN, cN, m, tot, que[maxn << 1], s[maxn << 1], ans; +struct Event +{ + int x, y1, y2, sgn; + bool operator < (const Event &t) const + { + return x < t.x; + } +} e[maxn << 1]; +struct Segment +{ + int vis, val; +} seg[maxn << 2]; +inline int seg_idx(int L, int R) +{ + return L + R | L < R; +} +void build(int L, int R) +{ + int rt = seg_idx(L, R); + if(L == R) + { + seg[rt].vis = 0; + seg[rt].val = s[L]; + return; + } + int M = L + R >> 1, lch = seg_idx(L, M), rch = seg_idx(M + 1, R); + build(L, M); + build(M + 1, R); + seg[rt].vis = 0; + seg[rt].val = std::max(seg[lch].val, seg[rch].val); +} +void upd(int L, int R, int l, int r, int v) +{ + int rt = seg_idx(L, R); + if(l <= L && R <= r) + { + if((seg[rt].vis += v)) + seg[rt].val = 0; + else if(L == R) + seg[rt].val = s[L]; + else + { + int M = L + R >> 1, lch = seg_idx(L, M), rch = seg_idx(M + 1, R); + seg[rt].val = std::max(seg[lch].val, seg[rch].val); + } + return; + } + int M = L + R >> 1, lch = seg_idx(L, M), rch = seg_idx(M + 1, R); + if(l <= M) + upd(L, M, l, r, v); + if(r > M) + upd(M + 1, R, l, r, v); + seg[rt].val = seg[rt].vis ? 0 : std::max(seg[lch].val, seg[rch].val); +} +int main() +{ + scanf("%d", &t); + for(int Case = 1; Case <= t; ++Case) + { + m = tot = ans = 0; + scanf("%d%d%d", &n, &rN, &cN); + while(n--) + { + int x, y; + scanf("%d%d", &x, &y); + e[m++] = (Event){x, y, y + cN, 1}; + e[m++] = (Event){x + rN, y, y + cN, -1}; + que[tot++] = y; + que[tot++] = y + cN; + } + std::sort(e, e + m); + std::sort(que, que + tot); + tot = std::unique(que, que + tot) - que; + memset(s, 0, tot * sizeof(int)); + for(int i = 0; i < m; ++i) + { + e[i].y1 = std::lower_bound(que, que + tot, e[i].y1) - que; + e[i].y2 = std::lower_bound(que, que + tot, e[i].y2) - que; + if(e[i].sgn > 0) + { + ++s[e[i].y1]; + --s[e[i].y2]; + } + } + for(int i = 1; i < tot; ++i) + s[i] += s[i - 1]; + build(0, tot - 1); + int c = 0, rt = seg_idx(0, tot - 1); + for(int i = 0, j = 0; i < m; i = j) + { + for( ; j < m && e[i].x == e[j].x; ++j) + { + upd(0, tot - 1, e[j].y1, e[j].y2 - 1, e[j].sgn); + c += e[j].sgn; + } + if(ans < c + seg[rt].val) + ans = c + seg[rt].val; + } + printf("Case #%d: %d\n", Case, ans); + } + return 0; +} diff --git a/Summer_Training/2016-07-26/G.cpp b/Summer_Training/2016-07-26/G.cpp new file mode 100644 index 00000000..a7940b5c --- /dev/null +++ b/Summer_Training/2016-07-26/G.cpp @@ -0,0 +1,80 @@ +#include +#include +typedef long long LL; +const int maxn = 51, mod = 1000000007; +int t, n, m, f[2][2][2][2], (*g)[2][2] = f[0], (*h)[2] = g[0], pre = 1, cur, ans; +bool e[maxn][maxn]; +int main() +{ + scanf("%d", &t); + for(int Case = 1; Case <= t; ++Case) + { + scanf("%d%d", &n, &m); + for(int i = 0; i < m; ++i) + { + int k, x; + memset(e[i], 0, n * sizeof(bool)); + scanf("%d", &k); + while(k--) + { + scanf("%d", &x); + e[i][--x] = 1; + } + } + ans = 0; + for(int i = 0; i < n; ++i) + { + for(int j = i + 1; j < n; ++j) + { + for(int k = j + 1; k < n; ++k) + { + memset(f[cur], 0, sizeof f[cur]); + f[cur][0][0][0] = 1; + for(int o = 0; o < m; ++o) + { + cur ^= 1; + pre ^= 1; + memcpy(f[cur], f[pre], sizeof f[pre]); + int di = e[o][i], dj = e[o][j], dk = e[o][k]; + for(int x = 0; x < 2; ++x) + for(int y = 0; y < 2; ++y) + for(int z = 0; z < 2; ++z) + if((f[cur][x][y][z] += f[pre][x ^ di][y ^ dj][z ^ dk]) >= mod) + f[cur][x][y][z] -= mod; + } + ans = (ans + 6LL * f[cur][1][1][1]) % mod; + } + memset(g[cur], 0, sizeof g[cur]); + g[cur][0][0] = 1; + for(int o = 0; o < m; ++o) + { + cur ^= 1; + pre ^= 1; + memcpy(g[cur], g[pre], sizeof g[pre]); + int di = e[o][i], dj = e[o][j]; + for(int x = 0; x < 2; ++x) + for(int y = 0; y < 2; ++y) + if((g[cur][x][y] += g[pre][x ^ di][y ^ dj]) >= mod) + g[cur][x][y] -= mod; + } + ans = (ans + 6LL * g[cur][1][1]) % mod; + } + memset(h[cur], 0, sizeof h[cur]); + h[cur][0] = 1; + for(int o = 0; o < m; ++o) + { + cur ^= 1; + pre ^= 1; + memcpy(h[cur], h[pre], sizeof h[pre]); + int di = e[o][i]; + for(int x = 0; x < 2; ++x) + if((h[cur][x] += h[pre][x ^ di]) >= mod) + h[cur][x] -= mod; + } + if((ans += h[cur][1]) >= mod) + ans -= mod; + } + printf("Case #%d: %d\n", Case, ans); + } + return 0; +} diff --git a/Summer_Training/2016-07-26/H.cpp b/Summer_Training/2016-07-26/H.cpp new file mode 100644 index 00000000..672c5e3c --- /dev/null +++ b/Summer_Training/2016-07-26/H.cpp @@ -0,0 +1,30 @@ +#include +typedef long long LL; +const int maxn = 100001, mod = 1000000007; +int n, f[maxn], g[maxn], s[maxn]; +inline LL sum2(int n) +{ + return n * (n + 1LL) >> 1; +} +inline LL sum3(int n) +{ + LL v1 = sum2(n); + int v2 = n + 2; + (v2 % 3 ? v1 /= 3 : v2 /= 3); + return v1 % mod * v2; +} +int main() +{ + f[1] = g[0] = s[0] = g[1] = 1; + s[1] = 2; + for(int i = 2; i < maxn; ++i) + { + g[i] = (sum2(g[i - 1]) + (LL)g[i - 1] * s[i - 2]) % mod; + if((s[i] = s[i - 1] + g[i]) >= mod) + s[i] -= mod; + f[i] = i & 1 ? (sum2(g[i >> 1]) % mod * s[(i >> 1) - 1] + sum3(g[i >> 1])) % mod : sum2(g[i >> 1]) % mod; + } + while(scanf("%d", &n) == 1 && n) + printf("%d\n", f[n]); + return 0; +} diff --git a/Summer_Training/2016-07-26/I.cpp b/Summer_Training/2016-07-26/I.cpp new file mode 100644 index 00000000..029eec0f --- /dev/null +++ b/Summer_Training/2016-07-26/I.cpp @@ -0,0 +1,144 @@ +#include +#include +#include +#include +#include +typedef long long LL; +const int maxn = 50001, maxd = 16; +int n, m, q, tot, val[maxn], sz, lpos[maxn]; +LL sum, ans[maxn]; +std::vector > e[maxn]; +struct Trie +{ + int cnt, ch[2]; +} p[maxn * maxd]; +struct Query +{ + int id, l, r; + bool operator < (const Query &t) const + { + if(lpos[l] != lpos[t.l]) + return lpos[l] < lpos[t.l]; + return r < t.r; + } +} query[maxn]; +void dfs(int u) +{ + for(std::vector >::iterator it = e[u].begin(); it != e[u].end(); ++it) + { + int v = it -> first, w = it -> second; + if(val[v] == -1) + { + val[v] = val[u] ^ w; + dfs(v); + } + } +} +void ins(int val) +{ + int rt = 0; + for(int i = maxd - 1; i >= 0; --i) + { + int o = (val >> i) & 1; + if(!p[rt].ch[o]) + { + p[rt].ch[o] = tot; + memset(p + (tot++), 0, sizeof p[0]); + } + rt = p[rt].ch[o]; + } +} +void upd(int val, int c) +{ + int rt = 0; + for(int i = maxd - 1; i >= 0; --i) + { + int o = (val >> i) & 1; + rt = p[rt].ch[o]; + p[rt].cnt += c; + } +} +int que(int val) +{ + int rt = 0, ret = 0; + //printf("query %d :\n", val); + for(int i = maxd - 1; i >= 0; --i) + { + int o1 = (val >> i) & 1, o2 = (m >> i) & 1; + if(!o2 && p[rt].ch[o1 ^ 1]) + { + ret += p[p[rt].ch[o1 ^ 1]].cnt; + //printf("diff %d : %d\n", i, p[p[rt].ch[o1 ^ 1]].cnt); + } + if(!p[rt].ch[o1 ^ o2]) + break; + rt = p[rt].ch[o1 ^ o2]; + } + return ret; +} +int main() +{ + while(scanf("%d%d%d", &n, &m, &q) == 3) + { + // calc value + memset(val, -1, sizeof val); + for(int i = 1; i <= n; ++i) + std::vector >().swap(e[i]); + for(int i = 1; i < n; ++i) + { + int u, v, w; + scanf("%d%d%d", &u, &v, &w); + e[u].push_back(std::make_pair(v, w)); + e[v].push_back(std::make_pair(u, w)); + } + val[1] = 0; + dfs(1); + // build trie + tot = 0; + memset(p + (tot++), 0, sizeof p[0]); + for(int i = 1; i <= n; ++i) + ins(val[i]); + // mo's algorithm + sz = (int)ceil(sqrt(n)); + for(int i = 1, j = 1; i <= n; ++i, (i % sz == 1 ? ++j : 0)) + lpos[i] = j; + for(int i = 1; i <= q; ++i) + { + query[i].id = i; + scanf("%d%d", &query[i].l, &query[i].r); + } + std::sort(query + 1, query + q + 1); + sum = 0; + for(int i = 1, qL = 1, qR = 0; i <= q; ++i) + { + for( ; qR < query[i].r; ++qR) + { + //printf("R : %d -> %d\n", qR, qR + 1); + sum += que(val[qR + 1]); + upd(val[qR + 1], 1); + } + for( ; qL > query[i].l; --qL) + { + //printf("L : %d -> %d\n", qL, qL - 1); + sum += que(val[qL - 1]); + upd(val[qL - 1], 1); + } + for( ; qL < query[i].l; ++qL) + { + //printf("L : %d -> %d\n", qL, qL + 1); + upd(val[qL], -1); + sum -= que(val[qL]); + } + for( ; qR > query[i].r; --qR) + { + //printf("R : %d -> %d\n", qR, qR - 1); + upd(val[qR], -1); + sum -= que(val[qR]); + } + ans[query[i].id] = sum; + } + for(int i = 1; i <= q; ++i) + printf("%d\n", ans[i]); + } + return 0; +} diff --git a/Summer_Training/2016-08-02/H.cpp b/Summer_Training/2016-08-02/H.cpp new file mode 100644 index 00000000..94cfe1ae --- /dev/null +++ b/Summer_Training/2016-08-02/H.cpp @@ -0,0 +1,39 @@ +#include +#include +const int maxn = 10000001; +int tot, prime[maxn], f[maxn]; +int main() +{ + f[1] = 1; + for(int i = 2; i < maxn; ++i) + { + if(!f[i]) + { + prime[tot++] = i; + f[i] = -1; + } + for(int j = 0, k = (maxn - 1) / i; j < tot && prime[j] <= k; ++j) + { + f[i * prime[j]] = -f[i]; + if(i % prime[j] == 0) + break; + } + f[i] += f[i - 1]; + } + int t, n, m; + scanf("%d", &t); + while(t--) + { + scanf("%d%d", &n, &m); + if(n > m) + std::swap(n, m); + long long ans = (long long)n * m; + for(int i = 1, j; i <= n; i = j + 1) + { + j = std::min(n / (n / i), m / (m / i)); + ans -= (long long)(n / i) * (m / i) * (f[j] - f[i - 1]); + } + printf("%I64d\n", ans); + } + return 0; +} diff --git a/Summer_Training/2016-08-02/J.cpp b/Summer_Training/2016-08-02/J.cpp new file mode 100644 index 00000000..a29b57be --- /dev/null +++ b/Summer_Training/2016-08-02/J.cpp @@ -0,0 +1,35 @@ +#include +typedef long long LL; +const int maxl = 5001; +int n, m, p, fact[maxl], a[maxl], str[maxl][maxl], f[2][maxl], g[2], cur, pre = 1; +int main() +{ + scanf("%d%d%d", &n, &m, &p); + fact[0] = a[0] = 1; + for(int i = 1; i < maxl; ++i) + { + fact[i] = (LL)fact[i - 1] * i % p; + a[i] = a[i - 1] * (m - i + 1LL) % p; + for(int j = 2; j < i; ++j) + str[i][j] = (str[i - 1][j - 1] + str[i - 1][j] * (j - 1LL)) % p; + str[i][i] = 1; + } + f[cur][0] = g[cur] = 1; + for(int i = 1, last = 0, len; i <= n; ++i, last = len) + { + scanf("%d", &len); + pre ^= 1; + cur ^= 1; + g[cur] = 0; + for(int j = 1; j <= len; ++j) + { + f[cur][j] = (LL)a[j] * str[len][j] % p * g[pre] % p; + if(j <= last && (f[cur][j] = (f[cur][j] - (LL)fact[j] * str[len][j] % p * f[pre][j]) % p) < 0) + f[cur][j] += p; + if((g[cur] += f[cur][j]) >= p) + g[cur] -= p; + } + } + printf("%d\n", g[cur]); + return 0; +} diff --git a/Summer_Training/2016-08-09/H.cpp b/Summer_Training/2016-08-09/H.cpp new file mode 100644 index 00000000..ce762d3b --- /dev/null +++ b/Summer_Training/2016-08-09/H.cpp @@ -0,0 +1,35 @@ +#include +typedef long long LL; +const int maxn = 2001, mod = 1000000007; +int t, n, m, c[maxn][maxn]; +int mod_pow(int x, int k) +{ + int ret = 1; + for( ; k > 0; k >>= 1, x = (LL)x * x % mod) + if(k & 1) + ret = (LL)ret * x % mod; + return ret; +} +int main() +{ + for(int i = 0; i < maxn; ++i) + { + c[i][0] = c[i][i] = 1; + for(int j = 1; j < i; ++j) + if((c[i][j] = c[i - 1][j - 1] + c[i - 1][j]) >= mod - 1) + c[i][j] -= mod - 1; + } + scanf("%d", &t); + while(t--) + { + scanf("%d%d", &n, &m); + int ans = 1; + if(n > 1) + for(int i = 2; i <= m; ++i) + ans = (LL)ans * mod_pow(i, (LL)n * c[m - i + n - 2][n - 2] % (mod - 1)) % mod; + else + ans = m; + printf("%d\n", ans); + } + return 0; +} diff --git a/Summer_Training/2016-08-11/A - Ball - tjz.cpp b/Summer_Training/2016-08-11/A - Ball - tjz.cpp new file mode 100644 index 00000000..444be9c9 --- /dev/null +++ b/Summer_Training/2016-08-11/A - Ball - tjz.cpp @@ -0,0 +1,46 @@ +#include +#include +#include +using namespace std; +const int maxn = 1010; +int t, n, m, a[maxn], b[maxn], ord[maxn]; +set pos[maxn]; +int main() +{ + scanf("%d", &t); + while(t--) + { + scanf("%d%d", &n, &m); + for(int i = 1; i <= n; ++i) + scanf("%d", a + i); + for(int i = 0; i <= n; ++i) + pos[i].clear(); + for(int i = 1; i <= n; ++i) + { + scanf("%d", b + i); + pos[b[i]].insert(i); + } + bool flag = 1; + for(int i = 1; i <= n; ++i) + { + if(!pos[a[i]].size()) + { + flag = 0; + break; + } + ord[i] = *pos[a[i]].begin(); + pos[a[i]].erase(ord[i]); + } + while(m--) + { + int l, r; + scanf("%d%d", &l, &r); + if(flag) + std::sort(ord + l, ord + r + 1); + } + for(int i = 1; i <= n && flag; ++i) + flag &= ord[i] == i; + puts(flag ? "Yes" : "No"); + } + return 0; +} diff --git a/Summer_Training/2016-08-11/C - color II - tjz.cpp b/Summer_Training/2016-08-11/C - color II - tjz.cpp new file mode 100644 index 00000000..3fce32c1 --- /dev/null +++ b/Summer_Training/2016-08-11/C - color II - tjz.cpp @@ -0,0 +1,48 @@ +#include +const int maxn = 18; +int t, n; +char e[maxn][maxn + 1], lbt[1 << maxn], f[1 << maxn]; +bool can[1 << maxn]; +inline void upd(char &x, char y) +{ + if(x > y) + x = y; +} +int main() +{ + for(int i = 1; i < 1 << maxn; ++i) + lbt[i] = i & 1 ? 0 : lbt[i >> 1] + 1; + scanf("%d", &t); + while(t--) + { + scanf("%d", &n); + int all = 1 << n; + for(int i = 0; i < n; ++i) + scanf("%s", e[i]); + can[0] = 1; + for(int i = 1; i < all; ++i) + { + int u = lbt[i & -i]; + if(!(can[i] = can[i ^ (1 << u)])) + continue; + for(int j = u + 1; j < n; ++j) + if((i & (1 << j)) && e[u][j] == '1') + { + can[i] = 0; + break; + } + } + f[0] = 0; + unsigned int ans = 0, pw = 233u; + for(int i = 1; i < all; ++i, pw *= 233u) + { + f[i] = n; + for(int j = i; j; j = (j - 1) & i) + if(can[j]) + upd(f[i], f[i ^ j] + 1); + ans += f[i] * pw; + } + printf("%u\n", ans); + } + return 0; +} diff --git a/Summer_Training/2016-08-11/F - physics - tjz.cpp b/Summer_Training/2016-08-11/F - physics - tjz.cpp new file mode 100644 index 00000000..d99f6dd5 --- /dev/null +++ b/Summer_Training/2016-08-11/F - physics - tjz.cpp @@ -0,0 +1,26 @@ +#include +#include +#include +const int maxn = 100001; +int t, n, c, q, v[maxn]; +int main() +{ + scanf("%d", &t); + while(t--) + { + scanf("%d%d", &n, &c); + for(int i = 0; i < n; ++i) + scanf("%d%*d%*d", v + i); + std::sort(v, v + n); + scanf("%d", &q); + while(q--) + { + int t, k; + scanf("%d%d", &t, &k); + long long val = 2LL * c * t + (long long)v[k - 1] * v[k - 1]; + double ans = sqrt(val); + printf("%.3f\n", ans); + } + } + return 0; +} diff --git a/Summer_Training/2016-08-11/H - Rikka with Sequence - tjz.cpp b/Summer_Training/2016-08-11/H - Rikka with Sequence - tjz.cpp new file mode 100644 index 00000000..954ddb4c --- /dev/null +++ b/Summer_Training/2016-08-11/H - Rikka with Sequence - tjz.cpp @@ -0,0 +1,171 @@ +#include +#include +#include +const int maxn = 100001; +typedef long long LL; +template inline void scan(T &x) +{ + int ch; + while((ch = getchar()) < '0' || ch > '9'); + x = ch - '0'; + while((ch = getchar()) >= '0' && ch <= '9') + x = (x << 3) + (x << 1) + (ch - '0'); +} +template inline void print(T x, char endc = '\n') +{ + int len = 0, num[20]; + while(x) + { + num[len++] = x % 10; + x /= 10; + } + if(!len) + num[len++] = 0; + while(len) + putchar('0' + num[--len]); + putchar(endc); +} +int squ[maxn]; +inline LL square_root(LL x) +{ + if(x < maxn) + return squ[x]; + LL ret = (LL)sqrt(x); + for( ; ret * ret > x; --ret); + for( ; (ret + 1) * (ret + 1) <= x; ++ret); + return ret; +} +int t, n, m; +struct Segment +{ + int size; // size + LL sum, tag, eqv; //sum, add tag, equal num +} seg[maxn << 1]; +inline int seg_idx(int L, int R) +{ + return (L + R) | (L < R); +} +inline void push_down(int rt, int lch, int rch) +{ + if(seg[rt].tag) + { + seg[lch].sum += seg[lch].size * seg[rt].tag; + seg[lch].tag += seg[rt].tag; + if(seg[lch].eqv) + seg[lch].eqv += seg[rt].tag; + seg[rch].sum += seg[rch].size * seg[rt].tag; + seg[rch].tag += seg[rt].tag; + if(seg[rch].eqv) + seg[rch].eqv += seg[rt].tag; + seg[rt].tag = 0; + } + if(seg[rt].eqv && seg[rt].eqv != seg[lch].eqv) + { + seg[lch].tag = 0; + seg[lch].eqv = seg[rt].eqv; + seg[lch].sum = seg[lch].size * seg[lch].eqv; + seg[rch].tag = 0; + seg[rch].eqv = seg[rt].eqv; + seg[rch].sum = seg[rch].size * seg[rch].eqv; + } +} +inline void push_up(int rt, int lch, int rch) +{ + seg[rt].sum = seg[lch].sum + seg[rch].sum; + seg[rt].eqv = seg[lch].eqv == seg[rch].eqv ? seg[lch].eqv : 0; +} +inline void init(int L, int R) +{ + int rt = seg_idx(L, R); + seg[rt].size = R - L + 1; + seg[rt].tag = 0; + if(L == R) + { + scan(seg[rt].sum); + seg[rt].eqv = seg[rt].sum; + return; + } + int M = (L + R) >> 1, lch = seg_idx(L, M), rch = seg_idx(M + 1, R); + init(L, M); + init(M + 1, R); + push_up(rt, lch, rch); +} +inline void upd_sum(int L, int R, int l, int r, int x) +{ + int rt = seg_idx(L, R); + if(l <= L && R <= r) + { + seg[rt].sum += (LL)seg[rt].size * x; + seg[rt].tag += x; + if(seg[rt].eqv) + seg[rt].eqv += x; + return; + } + int M = (L + R) >> 1, lch = seg_idx(L, M), rch = seg_idx(M + 1, R); + push_down(rt, lch, rch); + if(l <= M) + upd_sum(L, M, l, r, x); + if(r > M) + upd_sum(M + 1, R, l, r, x); + push_up(rt, lch, rch); +} +inline void upd_sqrt(int L, int R, int l, int r) +{ + int rt = seg_idx(L, R); + if(l <= L && R <= r && seg[rt].eqv) + { + seg[rt].tag = 0; + seg[rt].eqv = square_root(seg[rt].eqv); + seg[rt].sum = seg[rt].size * seg[rt].eqv; + return; + } + int M = (L + R) >> 1, lch = seg_idx(L, M), rch = seg_idx(M + 1, R); + push_down(rt, lch, rch); + if(l <= M) + upd_sqrt(L, M, l, r); + if(r > M) + upd_sqrt(M + 1, R, l, r); + push_up(rt, lch, rch); +} +inline LL query(int L, int R, int l, int r) +{ + int rt = seg_idx(L, R); + if(l <= L && R <= r) + return seg[rt].sum; + int M = (L + R) >> 1, lch = seg_idx(L, M), rch = seg_idx(M + 1, R); + LL ret = 0; + push_down(rt, lch, rch); + if(l <= M) + ret += query(L, M, l, r); + if(r > M) + ret += query(M + 1, R, l, r); + push_up(rt, lch, rch); + return ret; +} +int main() +{ + for(int i = 1; i * i < maxn; ++i) + for(int L = i * i, R = std::min((i + 1) * (i + 1), maxn); L < R; ++L) + squ[L] = i; + scan(t); + while(t--) + { + scan(n), scan(m); + init(1, n); + while(m--) + { + int typ, l, r, x; + scan(typ), scan(l), scan(r); + if(typ == 1) + { + scan(x); + upd_sum(1, n, l, r, x); + } + else if(typ == 2) + upd_sqrt(1, n, l, r); + else + print(query(1, n, l, r)); + } + } + return 0; +} diff --git a/Summer_Training/2016-08-11/I - Rikka with Subset - tjz.cpp b/Summer_Training/2016-08-11/I - Rikka with Subset - tjz.cpp new file mode 100644 index 00000000..8ca79036 --- /dev/null +++ b/Summer_Training/2016-08-11/I - Rikka with Subset - tjz.cpp @@ -0,0 +1,110 @@ +#include +#include +#include +typedef long long LL; +const int maxn = 100001, maxlen = 18, maxm = 1 << maxlen, mod = 998244353, gen = 3; +int pw[maxn], fact[maxn], iact[maxn]; +int t, n, a[maxn], h[maxn]; +int w[maxlen + 1], inv2[maxlen + 1], bitrev[maxm], f[maxm], g[maxm]; +inline int mod_pow(int x, int k) +{ + int ret = 1; + for( ; k; k >>= 1, x = (LL)x * x % mod) + if(k & 1) + ret = (LL)ret * x % mod; + return ret; +} +inline void NTT(int len, int x[], int flag) +{ + for(int i = 1; i < len; ++i) + if(i < bitrev[i]) + std::swap(x[i], x[bitrev[i]]); + for(int i = 1, d = 1; d < len; ++i, d <<= 1) + for(int j = 0; j < len; j += d << 1) + for(int k = j, ww = 1; k < j + d; ++k) + { + int t = (LL)ww * x[k + d] % mod; + x[k + d] = x[k] < t ? x[k] + mod - t : x[k] - t; + x[k] = x[k] + t >= mod ? x[k] + t - mod : x[k] + t; + ww = (LL)ww * w[i] % mod; + } + if(flag == -1) + { + std::reverse(x + 1, x + len); + int bitLen; + for(bitLen = 0; 1 << bitLen < len; ++bitLen); + for(int i = 0; i < len; ++i) + x[i] = (LL)x[i] * inv2[bitLen] % mod; + } +} +template inline void scan(T &x) +{ + int ch; + while((ch = getchar()) < '0' || ch > '9'); + x = ch - '0'; + while((ch = getchar()) >= '0' && ch <= '9') + x = (x << 3) + (x << 1) + (ch - '0'); +} +template inline void print(T x, char endc = '\n') +{ + int len = 0, num[20]; + while(x) + { + num[len++] = x % 10; + x /= 10; + } + if(!len) + num[len++] = 0; + while(len) + putchar('0' + num[--len]); + putchar(endc); +} +int main() +{ + w[maxlen] = mod_pow(gen, (mod - 1) >> maxlen); + for(int i = maxlen - 1; i > 0; --i) + w[i] = (LL)w[i + 1] * w[i + 1] % mod; + inv2[0] = 1; + inv2[1] = (mod + 1) >> 1; + for(int i = 2; i <= maxlen; ++i) + inv2[i] = (LL)inv2[i - 1] * inv2[1] % mod; + iact[1] = 1; + for(int i = 2; i < maxn; ++i) + iact[i] = mod - mod / i * (LL)iact[mod % i] % mod; + pw[0] = fact[0] = iact[0] = 1; + for(int i = 1; i < maxn; ++i) + { + if((pw[i] = pw[i - 1] << 1) >= mod) + pw[i] -= mod; + fact[i] = (LL)fact[i - 1] * i % mod; + iact[i] = (LL)iact[i - 1] * iact[i] % mod; + } + scan(t); + while(t--) + { + scan(n); + for(int i = 0; i < n; ++i) + scan(a[i]); + std::sort(a, a + n, std::greater()); + int bitlen, len; + for(bitlen = 0, len = 1; len < n << 1; ++bitlen, len <<= 1); + for(int i = 1; i < len; ++i) + bitrev[i] = (bitrev[i >> 1] >> 1) | ((i & 1) << (bitlen - 1)); + for(int i = 0; i < len; ++i) + f[i] = i < n ? (LL)a[i] * pw[n - i - 1] % mod * fact[i] % mod : 0; + NTT(len, f, 1); + for(int i = 0; i < len; ++i) + g[i] = i <= n ? iact[n - i] : 0; + NTT(len, g, 1); + for(int i = 0; i < len; ++i) + f[i] = (LL)f[i] * g[i] % mod; + NTT(len, f, -1); + for(int i = 0; i < n; ++i) + { + h[i] = ((i ? h[i - 1] : 0) + (LL)f[n + i] * iact[i]) % mod; + print(h[i], ' '); + } + putchar('\n'); + } + return 0; +} diff --git a/Summer_Training/2016-08-11/K - Rikka with Parenthesis II - tjz.cpp b/Summer_Training/2016-08-11/K - Rikka with Parenthesis II - tjz.cpp new file mode 100644 index 00000000..d65fb444 --- /dev/null +++ b/Summer_Training/2016-08-11/K - Rikka with Parenthesis II - tjz.cpp @@ -0,0 +1,48 @@ +#include +const int maxn = 100010; +int t, n, pL, pR; +char str[maxn]; +int main() +{ + scanf("%d", &t); + while(t--) + { + scanf("%d%s", &n, str); + pL = -1; + for(int i = 0; i < n; ++i) + if(str[i] == ')') + { + pL = i; + break; + } + pR = -1; + for(int i = n - 1; i >= 0; --i) + if(str[i] == '(') + { + pR = i; + break; + } + if(pL == -1 || pR == -1) + { + puts("No"); + continue; + } + str[pL] = '('; + str[pR] = ')'; + bool flag = 1; + int c = 0; + for(int i = 0; i < n; ++i) + { + c += str[i] == '(' ? 1 : -1; + if(c < 0) + { + flag = 0; + break; + } + } + if(c) + flag = 0; + puts(flag ? "Yes" : "No"); + } + return 0; +} diff --git a/Summer_Training/2016-08-18/A - Median - lzy.cpp b/Summer_Training/2016-08-18/A - Median - lzy.cpp new file mode 100644 index 00000000..1cf6c4c0 --- /dev/null +++ b/Summer_Training/2016-08-18/A - Median - lzy.cpp @@ -0,0 +1,54 @@ +#include +#include + +using namespace std; + +int T, n, m, tot, s1, s2, l1, r1, l2, r2; +int a[100009]; + +int get(int k) { + //printf("k: %d\n", k); + if (r1 < l2) { + if (k <= s1) return a[l1 + k - 1]; + else return a[l2 + k - s1 - 1]; + } else { + int l = min(l1 ,l2), r = max(r1, r2), m, s, t; + while (l < r) { + m = (l + r) >> 1; + s = 0; + if (m >= l1) s += min(m, r1) - l1 + 1; + if (m >= l2) s += min(m, r2) - l2 + 1; + t = s; + if (l1 <= m && m <= r1 && l2 <= m && m <= r2) --s; + if (s == k || t == k) { + l = r = m; + break; + } + if (s > k) r = m - 1; + if (t < k) l = m + 1; + } + return a[l]; + } +} + +int main() { + scanf("%d", &T); + while (T--) { + scanf("%d%d", &n, &m); + for (int i = 1; i <= n; ++i) + scanf("%d", a + i); + while (m--) { + scanf("%d%d%d%d", &l1, &r1, &l2, &r2); + if (l1 > l2) { + swap(l1, l2); + swap(r1, r2); + } + s1 = r1 - l1 + 1; + s2 = r2 - l2 + 1; + tot = s1 + s2; + if (tot & 1) printf("%.1f\n", (double)get((tot + 1) / 2)); + else printf("%.1f\n", get(tot / 2) * .5 + get(tot / 2 + 1) * .5 + 1e-6); + } + } + return 0; +} diff --git a/Summer_Training/2016-08-18/B - Hard problem - tjz.cpp b/Summer_Training/2016-08-18/B - Hard problem - tjz.cpp new file mode 100644 index 00000000..d17d5833 --- /dev/null +++ b/Summer_Training/2016-08-18/B - Hard problem - tjz.cpp @@ -0,0 +1,14 @@ +#include +const long double ans = 0.292762519060695649518959120713854688849038583906493915331; +int main() +{ + int t, n; + scanf("%d", &t); + while(t--) + { + scanf("%d", &n); + double ret = ans * n * n; + printf("%.2f\n", ret); + } + return 0; +} diff --git a/Summer_Training/2016-08-18/D - Death Sequence - tjz.cpp b/Summer_Training/2016-08-18/D - Death Sequence - tjz.cpp new file mode 100644 index 00000000..4ecdaf76 --- /dev/null +++ b/Summer_Training/2016-08-18/D - Death Sequence - tjz.cpp @@ -0,0 +1,258 @@ +#include +#include +namespace fastIO{ + #define BUF_SIZE 100000 + #define OUT_SIZE 100000 + #define ll long long + //fread->read + bool IOerror=0; + inline char nc(){ + static char buf[BUF_SIZE],*p1=buf+BUF_SIZE,*pend=buf+BUF_SIZE; + if (p1==pend){ + p1=buf; pend=buf+fread(buf,1,BUF_SIZE,stdin); + if (pend==p1){IOerror=1;return -1;} + //{printf("IO error!\n");system("pause");for (;;);exit(0);} + } + return *p1++; + } + inline bool blank(char ch){return ch==' '||ch=='\n'||ch=='\r'||ch=='\t';} + inline void read(int &x){ + bool sign=0; char ch=nc(); x=0; + for (;blank(ch);ch=nc()); + if (IOerror)return; + if (ch=='-')sign=1,ch=nc(); + for (;ch>='0'&&ch<='9';ch=nc())x=x*10+ch-'0'; + if (sign)x=-x; + } + inline void read(ll &x){ + bool sign=0; char ch=nc(); x=0; + for (;blank(ch);ch=nc()); + if (IOerror)return; + if (ch=='-')sign=1,ch=nc(); + for (;ch>='0'&&ch<='9';ch=nc())x=x*10+ch-'0'; + if (sign)x=-x; + } + inline void read(double &x){ + bool sign=0; char ch=nc(); x=0; + for (;blank(ch);ch=nc()); + if (IOerror)return; + if (ch=='-')sign=1,ch=nc(); + for (;ch>='0'&&ch<='9';ch=nc())x=x*10+ch-'0'; + if (ch=='.'){ + double tmp=1; ch=nc(); + for (;ch>='0'&&ch<='9';ch=nc())tmp/=10.0,x+=tmp*(ch-'0'); + } + if (sign)x=-x; + } + inline void read(char *s){ + char ch=nc(); + for (;blank(ch);ch=nc()); + if (IOerror)return; + for (;!blank(ch)&&!IOerror;ch=nc())*s++=ch; + *s=0; + } + inline void read(char &c){ + for (c=nc();blank(c);c=nc()); + if (IOerror){c=-1;return;} + } + //getchar->read + inline void read1(int &x){ + char ch;int bo=0;x=0; + for (ch=getchar();ch<'0'||ch>'9';ch=getchar())if (ch=='-')bo=1; + for (;ch>='0'&&ch<='9';x=x*10+ch-'0',ch=getchar()); + if (bo)x=-x; + } + inline void read1(ll &x){ + char ch;int bo=0;x=0; + for (ch=getchar();ch<'0'||ch>'9';ch=getchar())if (ch=='-')bo=1; + for (;ch>='0'&&ch<='9';x=x*10+ch-'0',ch=getchar()); + if (bo)x=-x; + } + inline void read1(double &x){ + char ch;int bo=0;x=0; + for (ch=getchar();ch<'0'||ch>'9';ch=getchar())if (ch=='-')bo=1; + for (;ch>='0'&&ch<='9';x=x*10+ch-'0',ch=getchar()); + if (ch=='.'){ + double tmp=1; + for (ch=getchar();ch>='0'&&ch<='9';tmp/=10.0,x+=tmp*(ch-'0'),ch=getchar()); + } + if (bo)x=-x; + } + inline void read1(char *s){ + char ch=getchar(); + for (;blank(ch);ch=getchar()); + for (;!blank(ch);ch=getchar())*s++=ch; + *s=0; + } + inline void read1(char &c){for (c=getchar();blank(c);c=getchar());} + //scanf->read + inline void read2(int &x){scanf("%d",&x);} + inline void read2(ll &x){ + #ifdef _WIN32 + scanf("%I64d",&x); + #else + #ifdef __linux + scanf("%lld",&x); + #else + puts("error:can't recognize the system!"); + #endif + #endif + } + inline void read2(double &x){scanf("%lf",&x);} + inline void read2(char *s){scanf("%s",s);} + inline void read2(char &c){scanf(" %c",&c);} + inline void readln2(char *s){gets(s);} + //fwrite->write + struct Ostream_fwrite{ + char *buf,*p1,*pend; + Ostream_fwrite(){buf=new char[BUF_SIZE];p1=buf;pend=buf+BUF_SIZE;} + void out(char ch){ + if (p1==pend){ + fwrite(buf,1,BUF_SIZE,stdout);p1=buf; + } + *p1++=ch; + } + void print(int x){ + static char s[15],*s1;s1=s; + if (!x)*s1++='0';if (x<0)out('-'),x=-x; + while(x)*s1++=x%10+'0',x/=10; + while(s1--!=s)out(*s1); + } + void println(int x){ + static char s[15],*s1;s1=s; + if (!x)*s1++='0';if (x<0)out('-'),x=-x; + while(x)*s1++=x%10+'0',x/=10; + while(s1--!=s)out(*s1); out('\n'); + } + void print(ll x){ + static char s[25],*s1;s1=s; + if (!x)*s1++='0';if (x<0)out('-'),x=-x; + while(x)*s1++=x%10+'0',x/=10; + while(s1--!=s)out(*s1); + } + void println(ll x){ + static char s[25],*s1;s1=s; + if (!x)*s1++='0';if (x<0)out('-'),x=-x; + while(x)*s1++=x%10+'0',x/=10; + while(s1--!=s)out(*s1); out('\n'); + } + void print(char *s){while (*s)out(*s++);} + void println(char *s){while (*s)out(*s++);out('\n');} + void flush(){if (p1!=buf){fwrite(buf,1,p1-buf,stdout);p1=buf;}} + ~Ostream_fwrite(){flush();} + }Ostream; + inline void print(int x){Ostream.print(x);} + inline void println(int x){Ostream.println(x);} + inline void print(char x){Ostream.out(x);} + inline void println(char x){Ostream.out(x);Ostream.out('\n');} + inline void print(ll x){Ostream.print(x);} + inline void println(ll x){Ostream.println(x);} + inline void print(char *s){Ostream.print(s);} + inline void println(char *s){Ostream.println(s);} + inline void println(){Ostream.out('\n');} + inline void flush(){Ostream.flush();} + //puts->write + char Out[OUT_SIZE],*o=Out; + inline void print1(int x){ + static char buf[15]; + char *p1=buf;if (!x)*p1++='0';if (x<0)*o++='-',x=-x; + while(x)*p1++=x%10+'0',x/=10; + while(p1--!=buf)*o++=*p1; + } + inline void println1(int x){print1(x);*o++='\n';} + inline void print1(ll x){ + static char buf[25]; + char *p1=buf;if (!x)*p1++='0';if (x<0)*o++='-',x=-x; + while(x)*p1++=x%10+'0',x/=10; + while(p1--!=buf)*o++=*p1; + } + inline void println1(ll x){print1(x);*o++='\n';} + inline void print1(char c){*o++=c;} + inline void println1(char c){*o++=c;*o++='\n';} + inline void print1(char *s){while (*s)*o++=*s++;} + inline void println1(char *s){print1(s);*o++='\n';} + inline void println1(){*o++='\n';} + inline void flush1(){if (o!=Out){if (*(o-1)=='\n')*--o=0;puts(Out);}} + struct puts_write{ + ~puts_write(){flush1();} + }_puts; + inline void print2(int x){printf("%d",x);} + inline void println2(int x){printf("%d\n",x);} + inline void print2(char x){printf("%c",x);} + inline void println2(char x){printf("%c\n",x);} + inline void print2(ll x){ + #ifdef _WIN32 + printf("%I64d",x); + #else + #ifdef __linux + printf("%lld",x); + #else + puts("error:can't recognize the system!"); + #endif + #endif + } + inline void println2(ll x){print2(x);printf("\n");} + inline void println2(){printf("\n");} + #undef ll + #undef OUT_SIZE + #undef BUF_SIZE +}; +using namespace fastIO; +const int maxn = 3000001; +int t, n, mx, bit[maxn], tot, seq[maxn]; +inline void add(int x, int v) +{ + for( ; x <= n; x += x & -x) + bit[x] += v; +} +inline void init() +{ + memset(bit + 1, 0, n * sizeof(int)); + for(int x = 1; x <= n; ++x) + { + ++bit[x]; + int y = x + (x & -x); + if(y <= n) + bit[y] += bit[x]; + } +} +inline int find(int v) +{ + int ret = 0; + for(int x = mx; x > 0; x >>= 1) + { + ret |= x; + if(ret < n && bit[ret] < v) + v -= bit[ret]; + else + ret ^= x; + } + return ret + 1; +} +int main() +{ + int m, q, x; + read(t);//scanf("%d", &t); + while(t--) + { + tot = 0; + read(n), read(m), read(q); //scanf("%d%d%d", &n, &m, &q); + init(); + for(mx = 1; mx < n; mx <<= 1); + mx >>= 1; + for(int sz = n, tmp, idx = 1; sz > 0; sz -= tmp, tot += tmp, ++idx) + { + tmp = 0; + for(int i = 1; i <= sz; i += m, ++tmp) + seq[tot + tmp] = find(i); + for(int i = 0; i < tmp; ++i) + add(seq[tot + i], -1); + } + while(q--) + { + read(x); //scanf("%d", &x); + println(seq[x - 1]); //printf("%d\n", seq[x]); + } + } + return 0; +} diff --git a/Summer_Training/2016-08-18/E - Road - lzy.cpp b/Summer_Training/2016-08-18/E - Road - lzy.cpp new file mode 100644 index 00000000..84abce8d --- /dev/null +++ b/Summer_Training/2016-08-18/E - Road - lzy.cpp @@ -0,0 +1,53 @@ +#include +#include +#include +#include + +#define N 200000 + +using namespace std; + +int n, m, a, b, w[N + 9]; +set s; +vector r[N + 9]; +int d[N + 9]; + +void get(int &x) { + x = 0; + int c; + while (isdigit(c = getchar())) + x = (x << 3) + (x << 1) + c - '0'; +} + +int main() { + while (scanf("%d%d%*c", &n, &m) == 2) { + for (int i = 1; i < n; ++i) + get(w[i]); + for (int i = 1; i <= n; ++i) + r[i].clear(); + for (int i = 1; i <= m; ++i) { + d[i] = 0; + get(a), get(b); + if (a > b) swap(a, b); + r[a].push_back(+i); + r[b].push_back(-i); + } + s.clear(); + for (int i = 1; i < n; ++i) { + for (int j: r[i]) + if (j > 0) s.insert(+j); + else s.erase(-j); + if (!s.empty()) { + //printf("%d %d %d\n", i, *s.begin(), *s.rbegin()); + d[*s.begin()] += w[i]; + d[*s.rbegin() + 1] -= w[i]; + } + } + int sum = 0; + for (int i = 1; i <= m; ++i) { + sum += d[i]; + printf("%d\n", sum); + } + } + return 0; +} diff --git a/Summer_Training/2016-08-18/F - Counting Intersections - tjz.cpp b/Summer_Training/2016-08-18/F - Counting Intersections - tjz.cpp new file mode 100644 index 00000000..5c560d02 --- /dev/null +++ b/Summer_Training/2016-08-18/F - Counting Intersections - tjz.cpp @@ -0,0 +1,79 @@ +#include +#include +#include +using namespace std; +typedef long long LL; +const int maxn = 100001; +int t, n, tot, tot_x, que[maxn << 1], bit[maxn << 1]; +LL ans; +struct Event +{ + int typ, x1, x2, y; + bool operator < (const Event &t) const + { + if(y != t.y) + return y < t.y; + return typ > t.typ; + } +} e[maxn << 1]; +void add(int x, int v) +{ + for( ; x <= tot_x; x += x & -x) + bit[x] += v; +} +int sum(int x) +{ + int ret = 0; + for( ; x > 0; x -= x & -x) + ret += bit[x]; + return ret; +} +int main() +{ + scanf("%d", &t); + while(t--) + { + tot = tot_x = 0; + ans = 0; + scanf("%d", &n); + while(n--) + { + int x1, y1, x2, y2; + scanf("%d%d%d%d", &x1, &y1, &x2, &y2); + if(x1 > x2) + swap(x1, x2); + if(y1 > y2) + swap(y1, y2); + if(x1 == x2) + { + e[tot++] = (Event){1, x1 - 1, x2, y1}; + e[tot++] = (Event){-1, x1 - 1, x2, y2}; + que[tot_x++] = x1; + } + else + { + e[tot++] = (Event){0, x1 - 1, x2, y1}; + que[tot_x++] = x1 - 1; + que[tot_x++] = x2; + } + } + sort(e, e + tot); + sort(que, que + tot_x); + tot_x = unique(que, que + tot_x) - que; + memset(bit + 1, 0, tot_x * sizeof(int)); + for(int i = 0; i < tot; ++i) + if(!e[i].typ) + { + e[i].x1 = lower_bound(que, que + tot_x, e[i].x1) - que + 1; + e[i].x2 = lower_bound(que, que + tot_x, e[i].x2) - que + 1; + ans += sum(e[i].x2) - sum(e[i].x1); + } + else + { + e[i].x2 = lower_bound(que, que + tot_x, e[i].x2) - que + 1; + add(e[i].x2, e[i].typ); + } + printf("%I64d\n", ans); + } + return 0; +} diff --git a/Summer_Training/2016-08-18/G - cjj's string game - tjz.cpp b/Summer_Training/2016-08-18/G - cjj's string game - tjz.cpp new file mode 100644 index 00000000..5b0d6a77 --- /dev/null +++ b/Summer_Training/2016-08-18/G - cjj's string game - tjz.cpp @@ -0,0 +1,69 @@ +#include +#include +typedef long long LL; +const int maxn = 11, mod = 1000000007; +struct Matrix +{ + int r, c, num[maxn][maxn]; + void init(int _r, int _c) + { + r = _r; + c = _c; + for(int i = 0; i < r; ++i) + memset(num[i], 0, c * sizeof(int)); + } + Matrix operator * (const Matrix &t) const + { + Matrix ret = {r, t.c}; + for(int i = 0; i < r; ++i) + for(int j = 0; j < c; ++j) + for(int k = 0; k < t.c; ++k) + ret.num[i][k] = (ret.num[i][k] + (LL)num[i][j] * t.num[j][k]) % mod; + return ret; + } + Matrix pow(int k) + { + Matrix ret = {r, r}, tmp = *this; + for(int i = 0; i < r; ++i) + ret.num[i][i] = 1; + for( ; k > 0; k >>= 1, tmp = tmp * tmp) + if(k & 1) + ret = ret * tmp; + return ret; + } +} A, B; +int t, n, m, p; +int main() +{ + scanf("%d", &t); + while(t--) + { + scanf("%d%d%d", &n, &m, &p); + A.init(m + 1, m + 1); // <= m + B.init(m, m); // < m + for(int i = 0; i <= m; ++i) + { + A.num[i][0] = p * (p - 1); + if(i + 1 <= m) + A.num[i][i + 1] = p; + if(i < m) + { + B.num[i][0] = p * (p - 1); + if(i + 1 < m) + B.num[i][i + 1] = p; + } + } + A = A.pow(n); + B = B.pow(n); + int ans = 0; + for(int i = 0; i <= m; ++i) + { + if((ans += A.num[0][i]) >= mod) + ans -= mod; + if(i < m && (ans -= B.num[0][i]) < 0) + ans += mod; + } + printf("%d\n", ans); + } + return 0; +} diff --git a/Summer_Training/2016-08-18/K - Water problem - lzy.cpp b/Summer_Training/2016-08-18/K - Water problem - lzy.cpp new file mode 100644 index 00000000..128fe080 --- /dev/null +++ b/Summer_Training/2016-08-18/K - Water problem - lzy.cpp @@ -0,0 +1,77 @@ +#include +#include +#include + +using namespace std; + +int n, m; +string s; + +string a[] = { + "", + "one", + "two", + "three", + "four", + "five", + "six", + "seven", + "eight", + "nine", +}; + +string b[] = { + "ten", + "eleven", + "twelve", + "thirteen", + "fourteen", + "fifteen", + "sixteen", + "seventeen", + "eighteen", + "nineteen", +}; + +string c[] = { + "", + "ten", + "twenty", + "thirty", + "forty", + "fifty", + "sixty", + "seventy", + "eighty", + "ninety", +}; + +int main() { + int T; + cin >> T; + while (T--) { + cin >> n; + s.clear(); + if (n == 1000) { + s = "onethousand"; + } else { + if (n >= 100) { + s += a[n / 100] + "hundred"; + + } + if (n %= 100) { + if (!s.empty()) s += "and"; + if (10 <= n && n < 20) { + s += b[n - 10]; + } else { + s += c[n / 10]; + s += a[n % 10]; + } + } + } + //cout << s << endl; + for (auto c: s) m += (bool)isalpha(c); + cout << m << endl; + } + return 0; +} diff --git a/Summer_Training/2016-09-10/A - Different Circle Permutation - tjz.cpp b/Summer_Training/2016-09-10/A - Different Circle Permutation - tjz.cpp new file mode 100644 index 00000000..2405caeb --- /dev/null +++ b/Summer_Training/2016-09-10/A - Different Circle Permutation - tjz.cpp @@ -0,0 +1,91 @@ +#include +typedef long long LL; +const int maxn = 31623, maxs = 10, mod = 1000000007; +int tot, prime[maxn], m, cnt, p[maxs], e[maxs], ans; +bool vis[maxn]; +int mod_inv(int x) +{ + return x <= 1 ? x : mod - (int)(mod / x * (LL)mod_inv(mod % x) % mod); +} +struct Matrix +{ + int num1, num2; + bool operator == (const Matrix &x) const + { + return num1 == x.num1 && num2 == x.num2; + } + Matrix operator * (const Matrix &x) const + { + return (Matrix){(int)(((LL)num1 * x.num1 + (LL)num2 * x.num2) % mod), + (int)(((LL)num1 * x.num2 + (LL)num2 * (x.num1 + x.num2)) % mod)}; + } + Matrix pow(int k) + { + Matrix ret = (Matrix){1, 0}, tmp = *this; + while(k) + { + if(k & 1) + ret = ret * tmp; + tmp = tmp * tmp; + k >>= 1; + } + return ret; + } +} A = {0, 1}; +void dfs(int dep, int val, int phi) +{ + if(dep == cnt) + { + Matrix B = A.pow(m / val); + ans = (ans + ((LL)(B.num1 << 1) + B.num2) * phi) % mod; + return; + } + dfs(dep + 1, val, phi); + for(int i = 1; i <= e[dep]; ++i) + { + dfs(dep + 1, val * p[dep], phi * (p[dep] - 1)); + val *= p[dep]; + phi *= p[dep]; + } +} +int main() +{ + for(int i = 2; i < maxn; ++i) + { + if(!vis[i]) + prime[tot++] = i; + for(int j = 0, o; j < tot && (o = i * prime[j]) < maxn; ++j) + { + vis[o] = 1; + if(i % prime[j] == 0) + break; + } + } + int n; + while(scanf("%d", &n) == 1) + { + if(n == 1) + { + puts("2"); + continue; + } + m = n; + cnt = 0; + for(int i = 0; i < tot; ++i) + if(n % prime[i] == 0) + { + for(e[cnt] = 0; n % prime[i] == 0; n /= prime[i], ++e[cnt]); + p[cnt++] = prime[i]; + } + if(n > 1) + { + e[cnt] = 1; + p[cnt++] = n; + } + ans = 0; + dfs(0, 1, 1); + ans = (LL)ans * mod_inv(m) % mod; + printf("%d\n", ans); + } + return 0; +} diff --git a/Summer_Training/2016-09-10/B - Different GCD Subarray Query - tjz.cpp b/Summer_Training/2016-09-10/B - Different GCD Subarray Query - tjz.cpp new file mode 100644 index 00000000..a4cb2d33 --- /dev/null +++ b/Summer_Training/2016-09-10/B - Different GCD Subarray Query - tjz.cpp @@ -0,0 +1,76 @@ +#include +#include +#include +using namespace std; +const int maxn = 100001, maxm = 1000001; +int n, q, a[maxn], tot, tim[maxm], pos[maxm], cnt, bit[maxn], ans[maxn]; +pair stack[maxn]; +struct Query +{ + int idx, l, r; + bool operator < (const Query &t) const + { + return r < t.r; + } +} query[maxn]; +void add(int x, int v) +{ + for( ; x <= n; x += x & -x) + bit[x] += v; +} +int sum(int x) +{ + int ret = 0; + for( ; x > 0; x -= x & -x) + ret += bit[x]; + return ret; +} +int main() +{ + for(int Case = 1; scanf("%d%d", &n, &q) == 2; ++Case) + { + tot = cnt = 0; + memset(bit + 1, 0, n * sizeof(int)); + for(int i = 1; i <= n; ++i) + scanf("%d", a + i); + for(int i = 1; i <= q; ++i) + { + query[i].idx = i; + scanf("%d%d", &query[i].l, &query[i].r); + } + sort(query + 1, query + q + 1); + for(int i = 1, o = 1; i <= n; ++i) + { + stack[tot++] = make_pair(0, i); + for(int j = 0; j < tot; ++j) + stack[j].first = __gcd(stack[j].first, a[i]); + int tmp = 0; + for(int j = 0; j < tot; ++j) + if(!tmp || stack[tmp - 1].first != stack[j].first) + stack[tmp++] = stack[j]; + tot = tmp; + for(int j = tot - 1, last = i + 1; j >= 0; last = stack[j--].second) + { + int val = stack[j].first, idx = last - 1; + if(tim[val] == Case) + { + add(pos[val], -1); + pos[val] = idx; + add(pos[val], 1); + } + else + { + tim[val] = Case; + pos[val] = idx; + add(pos[val], 1); + ++cnt; + } + } + for( ; o <= q && query[o].r == i; ++o) + ans[query[o].idx] = cnt - sum(query[o].l - 1); + } + for(int i = 1; i <= q; ++i) + printf("%d\n", ans[i]); + } + return 0; +} diff --git a/Summer_Training/2016-09-10/F - Football Games - tjz.cpp b/Summer_Training/2016-09-10/F - Football Games - tjz.cpp new file mode 100644 index 00000000..51ba8cdf --- /dev/null +++ b/Summer_Training/2016-09-10/F - Football Games - tjz.cpp @@ -0,0 +1,27 @@ +#include +typedef long long LL; +const int maxn = 100001; +int t, n, a[maxn]; +LL sum; +int main() +{ + while(scanf("%d", &t) != EOF) + { + while(t--) + { + bool flag = 1; + sum = 0; + scanf("%d", &n); + for(int i = 1; i <= n; ++i) + { + scanf("%d", a + i); + a[i] -= n - 1; + sum += a[i]; + flag &= -n < a[i] && a[i] < n; + } + flag &= !sum; + puts(flag ? "T" : "F"); + } + } + return 0; +} diff --git a/Summer_Training/2016-09-10/G - Friends and Enemies - tjz.cpp b/Summer_Training/2016-09-10/G - Friends and Enemies - tjz.cpp new file mode 100644 index 00000000..a150492b --- /dev/null +++ b/Summer_Training/2016-09-10/G - Friends and Enemies - tjz.cpp @@ -0,0 +1,9 @@ +#include +typedef long long LL; +int main() +{ + LL n, m; + while(scanf("%I64d%I64d", &n, &m) == 2) + puts(m >= (n >> 1) * (n + 1 >> 1) ? "T" : "F"); + return 0; +} diff --git a/Summer_Training/2016-09-10/H - Function - lzy.cpp b/Summer_Training/2016-09-10/H - Function - lzy.cpp new file mode 100644 index 00000000..5b207899 --- /dev/null +++ b/Summer_Training/2016-09-10/H - Function - lzy.cpp @@ -0,0 +1,64 @@ +#include +#include +#include + +#define N 100000 + +using namespace std; + +int n, m, l, r; +int a[N + 9], f[19][N + 9]; +int lg2[N + 9]; + +void get(int &x) { + x = 0; + int c; + while (!isdigit(c = getchar())); + x = c - '0'; + while (isdigit(c = getchar())) + x = x * 10 + c - '0'; +} + +int que(int l, int r) { + int j = lg2[r - l + 1]; + return min(f[j][l], f[j][r - (1 << j) + 1]); +} + +int find(int ll, int rr, int k) { + int l = ll, r = rr, m; + while (l < r) { + m = (l + r) >> 1; + if (que(ll, m) <= k) r = m; + else l = m + 1; + } + return a[l] <= k? l : rr + 1; +} + +int main() { + for (int i = 2; i <= N; ++i) lg2[i] = lg2[i >> 1] + 1; + int T; + get(T); + while (T--) { + get(n); + for (int i = 1; i <= n; ++i) { + get(a[i]); + f[0][i] = a[i]; + } + for (int j = 1; 1 << j <= n; ++j) + for (int i = 1; i + (1 << j) - 1 <= n; ++i) + f[j][i] = min(f[j - 1][i], f[j-1][i + (1 << j - 1)]); + get(m); + while (m--) { + get(l), get(r); + int v = a[l], k; + while (l < r) { + //printf("%d %d\n", l, v); + k = find(l + 1, r, v); + if (k > r) break; + v %= a[k]; + l = k; + } + printf("%d\n", v); + } + } +} diff --git a/Summer_Training/2016-09-10/I - Sparse Graph - lzy.cpp b/Summer_Training/2016-09-10/I - Sparse Graph - lzy.cpp new file mode 100644 index 00000000..5313ff1c --- /dev/null +++ b/Summer_Training/2016-09-10/I - Sparse Graph - lzy.cpp @@ -0,0 +1,65 @@ +#include +#include +#include +#include + +using namespace std; + +#define N 200000 +#define M 5500 + +const int inf = 0x3fffffff; + +int n, m, u, v, S; +vector lnk[N + 9]; +int d[N + 9]; +int l, r, q[N + 9]; +list s; +int ans[N + 9]; + +int main() { + int T; + scanf("%d", &T); + while (T--) { + scanf("%d%d", &n, &m); + for (int i = 1; i <= n; ++i) + lnk[i].clear(); + while (m--) { + scanf("%d%d", &u, &v); + lnk[u].push_back(v); + lnk[v].push_back(u); + } + scanf("%d", &S); + s.clear(); + for (int i = 1; i <= n; ++i) { + if (i == S) { + d[i] = 0; + continue; + } + d[i] = inf; + s.push_back(i); + } + s.sort(); + for (int i = 1; i <= n; ++i) + sort(lnk[i].begin(), lnk[i].end()); + q[(r = 0)++] = S; + for (int l = 0; l < r; ++l) { + u = q[l]; + int j = 0; + for (auto it = s.begin(), jt = it; it != s.end(); it = jt) { + jt = next(it); + while (j < (int)lnk[u].size() && lnk[u][j] < *it) ++j; + if (j < (int)lnk[u].size() && lnk[u][j] == *it) continue; + //printf("%d %d\n", u, *it); + d[*it] = d[u] + 1; + q[r++] = *it; + s.erase(it); + } + } + int tot = 0; + for (int i = 1; i <= n; ++i) + if (i != S) ans[tot++] = d[i] < inf? d[i] : -1; + for (int i = 0; i < tot; ++i) printf("%d%c", ans[i], " \n"[i == tot - 1]); + } + return 0; +} diff --git a/Summer_Training/2016-09-10/J - Weak Pair - lzy.cpp b/Summer_Training/2016-09-10/J - Weak Pair - lzy.cpp new file mode 100644 index 00000000..ab57a12f --- /dev/null +++ b/Summer_Training/2016-09-10/J - Weak Pair - lzy.cpp @@ -0,0 +1,75 @@ +#include +#include +#include +#include + +using namespace std; + +#define N 100000 + +typedef long long i64; + +int n, tot, u, v, a[N + 9], x[N + 9]; +vector lnk[N + 9]; +int c[N + 9]; +bool root[N + 9]; +i64 m, ans; + +void add(int k, int d) { + for (int i = k; i <= tot; i += i & -i) c[i] += d; +} + +int get(int k) { + int sum = 0; + for (int i = k; i >= 1; i -= i & -i) sum += c[i]; + return sum; +} + +void dfs(int u) { + int k; + if (a[u] && m / a[u] <= 1000000000) { + k = upper_bound(x + 1, x + 1 + tot, m / a[u]) - x; + //printf("%d %d %d\n", a[u], k, x[k]); + ans += get(k - 1); + + } else { + ans += get(tot); + } + k = lower_bound(x + 1, x + 1 + tot, a[u]) - x; + add(k, +1); + for (auto v: lnk[u]) dfs(v); + add(k, -1); +} + +int main() { + int T; + scanf("%d", &T); + while (T--) { + ans = 0; + memset(c, 0, sizeof c); + scanf("%d%I64d", &n, &m); + for (int i = 1; i <= n; ++i) { + lnk[i].clear(); + root[i] = true; + scanf("%d", a + i); + x[i] = a[i]; + } + sort(x + 1, x + 1 + n); + tot = unique(x + 1, x + 1 + n) - (x + 1); + //printf("%d\n", tot); + for (int i = 1; i < n; ++i) { + scanf("%d%d", &u, &v); + lnk[u].push_back(v); + root[v] = false; + } + + for (int i = 1; i <= n; ++i) + if (root[i]) { + dfs(i); + break; + } + //puts("!!!"); + printf("%I64d\n", ans); + } + return 0; +} diff --git a/Summer_Training/2016-09-15/A - git - lzy.cpp b/Summer_Training/2016-09-15/A - git - lzy.cpp new file mode 100644 index 00000000..ece17b8f --- /dev/null +++ b/Summer_Training/2016-09-15/A - git - lzy.cpp @@ -0,0 +1,74 @@ +#include +#include +#include +#include + +using namespace std; + +#define N 100 + +struct Node { + Node() { + f[0] = f[1] = -1; + } + char name[N + 9]; + vector sub; + int f[2]; +}; + +int T, n; +char str[N + 9]; + +void insert(Node &u, char s[], bool flag) { + char *p = strchr(s, '/'); + if (!p) { + u.f[flag] = 1; + u.f[!flag] = 0; + return; + } + *p = '\0'; + for (auto &v: u.sub) + if (!strcmp(v.name, s)) { + insert(v, p + 1, flag); + return; + } + u.sub.emplace_back(); + strcpy(u.sub.back().name, s); + insert(u.sub.back(), p + 1, flag); +} + +int dps(Node &u, bool flag) { + //if (~u.f[flag]) printf("%s, %d: %d\n", u.name, flag, u.f[flag]); + if (~u.f[flag]) return u.f[flag]; + int s = 0, t = 1; + for (auto &v: u.sub) { + s += dps(v, flag); + t += dps(v, !flag); + } + //printf("%s, %d: %d\n", u.name, flag, min(s, t)); + return (u.f[flag] = min(s, t)); +} + +void destroy(Node &u) { + for (auto v: u.sub) destroy(v); + u.sub.clear(); +} + +int main() { + scanf("%d", &T); + while (T--) { + Node root; + strcpy(root.name, "/"); + scanf("%d", &n); + while (n--) { + scanf("%s", str); + bool flag = strcmp(str, "stage"); + scanf("%s", str); + strcat(str, "/"); + insert(root, str + 1, flag); + } + printf("%d\n", dps(root, false)); + destroy(root); + } + return 0; +} diff --git a/Summer_Training/2016-09-15/B - Circles and Squares - tjz.cpp b/Summer_Training/2016-09-15/B - Circles and Squares - tjz.cpp new file mode 100644 index 00000000..3e60448d --- /dev/null +++ b/Summer_Training/2016-09-15/B - Circles and Squares - tjz.cpp @@ -0,0 +1,72 @@ +#include +#include +#include +using namespace std; +const int maxn = 100010; +typedef long long LL; +const long double pi = acos(-1.0), area[3] = {pi / 4, pi / 6 + sqrt(3.0) / 4, 1.0}; +int n, m, A, q, tot; +LL cnt[3]; +struct Point +{ + int x, y; + bool operator < (const Point &t) const + { + return x < t.x || x == t.x && y < t.y; + } + bool operator == (const Point &t) const + { + return x == t.x && y == t.y; + } +}p[maxn]; +struct Cir +{ + int lx, ly, idx; + bool operator < (const Cir &t) const + { + return lx < t.lx || lx == t.lx && ly < t.ly; + } +} c[maxn << 2]; +int main() +{ + while(scanf("%d%d%d%d", &n, &m, &A, &q) == 4) + { + tot = 0; + cnt[0] = cnt[1] = cnt[2] = 0; + for(int i = 0; i < q; ++i) + scanf("%d%d", &p[i].x, &p[i].y); + sort(p, p + q); + q = unique(p, p + q) - p; + for(int i = 0; i < q; ++i) + { + if(p[i].x > 0) + { + if(p[i].y > 0) + c[tot++] = (Cir){p[i].x - 1, p[i].y - 1, i}; + if(p[i].y < m) + c[tot++] = (Cir){p[i].x - 1, p[i].y, i}; + } + if(p[i].x < n) + { + if(p[i].y > 0) + c[tot++] = (Cir){p[i].x, p[i].y - 1, i}; + if(p[i].y < m) + c[tot++] = (Cir){p[i].x, p[i].y, i}; + } + } + sort(c, c + tot); + for(int i = 0, j = 0; i < tot; i = j) + { + int &lx = c[i].lx, &ly = c[i].ly; + for( ; j < tot && lx == c[j].lx && ly == c[j].ly; ++j); + int typ = i + 1 == j ? 0 : (i + 2 == j ? (p[c[i].idx].x != p[c[i + 1].idx].x && p[c[i].idx].y != p[c[i + 1].idx].y ? 2 : 1) : 2); + cnt[typ] += (min(lx, n - A) - max(lx - A + 1, 0) + 1LL) * (min(ly, m - A) - max(ly - A + 1, 0) + 1LL); + } + long double ans = 0; + for(int i = 0; i < 3; ++i) + ans += area[i] * cnt[i]; + LL total = (n - A + 1LL) * (m - A + 1LL); + printf("%.10f\n", (double)(ans / total)); + } + return 0; +} diff --git a/Summer_Training/2016-09-15/E - Cat and Mouse - lzy.cpp b/Summer_Training/2016-09-15/E - Cat and Mouse - lzy.cpp new file mode 100644 index 00000000..0f12fad9 --- /dev/null +++ b/Summer_Training/2016-09-15/E - Cat and Mouse - lzy.cpp @@ -0,0 +1,70 @@ +#include +#include +#include + +#include + +#define N 400 + +typedef long long i64; + +int n, k, tot; +int x[N + 9], y[N + 9]; +i64 s[N + 9][N + 9]; +bool g[N + 9][N + 9]; +i64 d[N * N + 9]; +int mat[N + 9]; +bool use[N + 9]; + +i64 sqr(i64 x) { + return x * x; +} + +bool aug(int u) { + for (int v = 1; v <= n; ++v) + if (!use[v] && g[u][v]) { + use[v] = true; + if (!mat[v] || aug(mat[v])) return (mat[v] = u); + } + return false; +} + +int calc(i64 dis) { + for (int i = 1; i <= n; ++i) + for (int j = 1; j <= n; ++j) + g[i][j] = s[i][j] > dis; + memset(mat, 0, sizeof mat); + int ret = 0; + for (int i = 1; i <= n; ++i) { + memset(use, 0, sizeof use); + ret += aug(i); + } + //printf("ret: %d\n", ret); + return n + n - ret; +} + +int gao() { + int l = 0, r = tot - 1, m; + std::sort(d, d + tot); + while (l < r) { + m = (l + r) >> 1; + //printf("%d %d %d %lld %d\n", l, r, m, d[m], calc(d[m])); + if (calc(d[m]) < k) l = m + 1; + else r = m ; + } + return l; +} + +int main() { + while (scanf("%d%d", &n, &k) == 2) { + for (int i = 1; i <= n + n; ++i) + scanf("%d%d", x + i, y + i); + tot = 0; + for (int i = 1; i <= n; ++i) + for (int j = n + 1; j <= n + n; ++j) + d[tot++] = s[i][j - n] = sqr(x[i] - x[j]) + sqr(y[i] - y[j]); + //printf("%lld\n", d[gao()]); + printf("%.8f\n", sqrt(d[gao()])); + } + return 0; +} diff --git a/Summer_Training/2016-09-15/F - Similar Strings - lzy.cpp b/Summer_Training/2016-09-15/F - Similar Strings - lzy.cpp new file mode 100644 index 00000000..023b4c2d --- /dev/null +++ b/Summer_Training/2016-09-15/F - Similar Strings - lzy.cpp @@ -0,0 +1,35 @@ +#include + +#define N 5000 + +constexpr int mod = 1000000007; + +int T, n; +int a[N + 9]; +int ff[N + 9], gg[N + 9], *f = ff, *g = gg, *h; +int ans[N + 9]; + +int main() { + scanf("%d", &T); + n = N; + for (int i = 1; i <= n; ++i) { + for (int j = 1; j <= n; ++j) { + a[j] = (25ll * g[j - 1] + 26) % mod; + } + for (int j = 1; j <= n; ++j) { + f[j] = f[j - 1] + a[j]; + if (f[j] >= mod) f[j] -= mod; + } + for (int j = 1; j <= n; ++j) { + f[j] += g[j]; + if (f[j] >= mod) f[j] -= mod; + } + ans[i] = f[i]; + h = f, f = g, g = h; + } + while (T--) { + scanf("%d", &n); + printf("%d\n", ans[n]); + } + return 0; +} diff --git a/Summer_Training/2016-09-15/G - Chota Mouse and his Birthday Cake - tjz.cpp b/Summer_Training/2016-09-15/G - Chota Mouse and his Birthday Cake - tjz.cpp new file mode 100644 index 00000000..a332e77c --- /dev/null +++ b/Summer_Training/2016-09-15/G - Chota Mouse and his Birthday Cake - tjz.cpp @@ -0,0 +1,23 @@ +#include +#include +const int maxn = 510; +int t, n, h, w, a[maxn], ans; +int main() +{ + scanf("%d", &t); + while(t--) + { + scanf("%d%d%d", &n, &h, &w); + a[0] = 0; + for(int i = 1; i <= n; ++i) + scanf("%d", a + i); + a[n + 1] = w; + std::sort(a + 1, a + n + 1); + ans = w; + for(int i = 0; i <= n; ++i) + ans = std::min(ans, a[i + 1] - a[i]); + ans *= h; + printf("%d\n", ans); + } + return 0; +} diff --git a/Summer_Training/2016-09-15/H - Longest Palindrome - lzy.cpp b/Summer_Training/2016-09-15/H - Longest Palindrome - lzy.cpp new file mode 100644 index 00000000..adbed696 --- /dev/null +++ b/Summer_Training/2016-09-15/H - Longest Palindrome - lzy.cpp @@ -0,0 +1,35 @@ +#include +#include +#include + +using namespace std; + +int T, n, a, b, c, d; +string s; + +int main() { + cin >> T; + while (T--) { + cin >> n; + a = b = c = d = 0; + while (n--) { + cin >> s; + if (s == "aa") ++a; + else if (s == "ab") ++b; + else if (s == "ba") ++c; + else if (s == "bb") ++d; + } + s.clear(); + for (; a >= 2; a -= 2) s += "aa"; + for (; b && c; --b, --c) s += "ab"; + for (; d >= 2; d -= 2) s += "bb"; + cout << s; + //cout << '|'; + if (a) cout << "aa"; + else if (d) cout << "bb"; + //cout << '|'; + std::reverse(s.begin(), s.end()); + cout << s << endl; + } + return 0; +} diff --git a/Summer_Training/2016-09-17/A - I Count Two Three - liuzy.cpp b/Summer_Training/2016-09-17/A - I Count Two Three - liuzy.cpp new file mode 100644 index 00000000..038871b4 --- /dev/null +++ b/Summer_Training/2016-09-17/A - I Count Two Three - liuzy.cpp @@ -0,0 +1,44 @@ +#include +#include +#include +#include +#include + +using namespace std; + +const int MAX = 2000000000; + +int N, T; +vector ans; + +int main() +{ + int t, i, j, k, l, x, y, z, p; + for(i = 0, x = 1;i <= 30;i += 1, x *= 2) + { + for(j = 0, y = 1;j <= 18;j += 1, y *= 3) + { + if((long long)x * y > MAX) + break; + for(k = 0, z = 1;k <= 12;k += 1, z *= 5) + { + if((long long)x * y * z > MAX) + break; + for(l = 0, p = 1;l <= 10;l += 1, p *= 7) + { + if((long long)x * y * z * p > MAX) + break; + ans.push_back(x * y * z * p); + } + } + } + } + sort(ans.begin(), ans.end()); + scanf("%d", &T); + for(t = 1;t <= T;t += 1) + { + scanf("%d", &N); + printf("%d\n", *lower_bound(ans.begin(), ans.end(), N)); + } + exit(0); +} diff --git a/Summer_Training/2016-09-17/B - Cure - tjz.cpp b/Summer_Training/2016-09-17/B - Cure - tjz.cpp new file mode 100644 index 00000000..e5123ca0 --- /dev/null +++ b/Summer_Training/2016-09-17/B - Cure - tjz.cpp @@ -0,0 +1,25 @@ +#include +#include +#include +const int maxn = 2000001; +const double pi = acos(-1.0), ans_inf = pi * pi / 6; +int len, n; +double f[maxn]; +char str[maxn]; +int main() +{ + for(int i = 1; i < maxn; ++i) + f[i] = f[i - 1] + 1.0 / i / i; + while(scanf("%s", str) == 1) + { + len = strlen(str); + if(len > 7) + { + printf("%.5f\n", ans_inf); + continue; + } + sscanf(str, "%d", &n); + printf("%.5f\n", n < maxn ? f[n] : ans_inf); + } + return 0; +} diff --git a/Summer_Training/2016-09-17/C - Family View - tjz.cpp b/Summer_Training/2016-09-17/C - Family View - tjz.cpp new file mode 100644 index 00000000..81e86161 --- /dev/null +++ b/Summer_Training/2016-09-17/C - Family View - tjz.cpp @@ -0,0 +1,128 @@ +#include +#include +const int maxn = 1000010, mod = 5800079, maxd = 26; +int t, sys_tim, n, tot, ban[maxn], f[maxn], s[maxn], *que = s, L, R; +char str[maxn]; +int e_tot, tim[mod], lnk[mod]; +struct Hash +{ + int nxt, idx, val; +} e[maxn]; +int m_tot, m_lnk[maxn]; +struct Edge +{ + int nxt, v; + char ch; +} m_e[maxn]; +inline void add_edge(int u, int v, char ch) +{ + m_e[m_tot] = (Edge){m_lnk[u], v, ch}; + m_lnk[u] = m_tot++; +} +inline int insert(int idx) +{ + int pos = idx % mod; + if(tim[pos] != sys_tim) + { + lnk[pos] = -1; + tim[pos] = sys_tim; + } + e[e_tot] = (Hash){lnk[pos], idx, tot}; + lnk[pos] = e_tot++; + m_lnk[tot] = -1; + ban[tot] = 0; + return tot++; +} +inline int find(int idx) +{ + int pos = idx % mod; + if(tim[pos] != sys_tim) + { + lnk[pos] = -1; + tim[pos] = sys_tim; + } + for(int it = lnk[pos]; it != -1; it = e[it].nxt) + if(e[it].idx == idx) + return e[it].val; + return -1; +} +int main() +{ + scanf("%d ", &t); + while(t--) + { + ++sys_tim; + e_tot = m_tot = 0; + m_lnk[0] = -1; + ban[0] = f[0] = 0; + tot = 1; + scanf("%d ", &n); + while(n--) + { + int rt = 0, len; + scanf("%s ", str); + len = strlen(str); + for(int i = 0; i < len; ++i) + { + str[i] -= 'a'; + int idx = rt * maxd + str[i], nxt = find(idx); + if(nxt == -1) + { + nxt = insert(idx); + add_edge(rt, nxt, str[i]); + } + rt = nxt; + } + ban[rt] = len; + } + L = R = 0; + for(int it = m_lnk[0]; it != -1; it = m_e[it].nxt) + { + int v = m_e[it].v; + que[R++] = v; + f[v] = 0; + } + while(L < R) + { + int u = que[L++]; + for(int it = m_lnk[u]; it != -1; it = m_e[it].nxt) + { + int v = m_e[it].v, i = m_e[it].ch; + que[R++] = v; + for(f[v] = f[u]; f[v] && find(f[v] * maxd + i) == -1; f[v] = f[f[v]]); + int nxt = find(f[v] * maxd + i); + if(nxt != -1) + f[v] = nxt; + ban[v] = ban[v] ? ban[v] : ban[f[v]]; + } + } + gets(str); + n = strlen(str); + memset(s, 0, (n + 1) * sizeof(int)); + for(int i = 0, rt = 0; i < n; ++i) + if(str[i] >= 'a' && str[i] <= 'z' || str[i] >= 'A' && str[i] <= 'Z') + { + int ch = str[i] >= 'a' && str[i] <= 'z' ? str[i] - 'a' : str[i] - 'A'; + for( ; rt && find(rt * maxd + ch) == -1; rt = f[rt]); + int nxt; + if((nxt = find(rt * maxd + ch)) != -1) + rt = nxt; + if(ban[rt]) + { + ++s[i - ban[rt] + 1]; + --s[i + 1]; + } + } + else + rt = 0; + for(int i = 0; i < n; ++i) + { + if(i) + s[i] += s[i - 1]; + if(s[i] > 0) + str[i] = '*'; + } + puts(str); + } + return 0; +} diff --git a/Summer_Training/2016-09-17/E - Balanced Game - lzy.cpp b/Summer_Training/2016-09-17/E - Balanced Game - lzy.cpp new file mode 100644 index 00000000..78dfdd56 --- /dev/null +++ b/Summer_Training/2016-09-17/E - Balanced Game - lzy.cpp @@ -0,0 +1,11 @@ +#include + +int T, n; +int main() { + scanf("%d", &T); + while (T--) { + scanf("%d", &n); + puts(n & 1 ? "Balanced" : "Bad"); + } + return 0; +} diff --git a/Summer_Training/2016-09-17/F - The Best Path - lzy.cpp b/Summer_Training/2016-09-17/F - The Best Path - lzy.cpp new file mode 100644 index 00000000..57bf8e8c --- /dev/null +++ b/Summer_Training/2016-09-17/F - The Best Path - lzy.cpp @@ -0,0 +1,70 @@ +#include +#include + +using std::max; + +#define N 100000 +#define M 500000 + +int T, n, m, u, v, a[N + 9], f[N + 9]; +int d[N + 9]; + +void join(int a, int b) { + f[a] = b; +} + +int find(int e) { + return f[e] == e ? e : f[e] = find(f[e]); +} + +bool pan() { + int r = -1; + for (int i = 1; i <= n; ++i) { + if (r == - 1) { + if (d[i]) r = find(i); + continue; + } + if (d[i] && find(i) != r) return false; + } + return true; +} + +int main() { + scanf("%d", &T); + while (T--) { + scanf("%d%d", &n, &m); + for (int i = 1; i <= n; ++i) { + d[i] = 0; + f[i] = i; + scanf("%d", a + i); + } + + while (m--) { + scanf("%d%d", &u, &v); + join(find(u), find(v)); + ++d[u]; + ++d[v]; + } + int s = 0, c = 0, t = 0; + for (int i = 1; i <= n; ++i) { + if (d[i] & 1) { + ++d[i]; + ++c; + } + if (d[i] >> 1 & 1) s ^= a[i]; + } + if (c == 1 || c > 2 || !pan()) { + puts("Impossible"); + continue; + } + if (c) { + t = s; + } else { + for (int i = 1; i <= n; ++i) + if (d[i]) t = max(t, s ^ a[i]); + } + printf("%d\n", t); + } + + return 0; +} diff --git a/Summer_Training/2016-09-17/G - Sort - tjz.cpp b/Summer_Training/2016-09-17/G - Sort - tjz.cpp new file mode 100644 index 00000000..66054da6 --- /dev/null +++ b/Summer_Training/2016-09-17/G - Sort - tjz.cpp @@ -0,0 +1,84 @@ +#include +#include +#include +using namespace std; +typedef long long LL; +const int maxn = 200001; +int t, n, mx, a[maxn]; +struct Queue +{ + int que[maxn], L, R; + void clear() + { + L = R = 0; + } + void copy(const int &len, const int seq[]) + { + L = 0, R = len; + memcpy(que, seq, len * sizeof(int)); + } + int pop_front() + { + int ret = que[L++]; + if(L == maxn) + L = 0; + return ret; + } + void push_front(const int &x) + { + --L; + if(L < 0) + L = maxn - 1; + que[L] = x; + } + void push_back(const int &x) + { + que[R++] = x; + if(R == maxn) + R = 0; + } +} qA, qB; +bool check(int sz) +{ + int rem = n, cost = 0; + for(qA.copy(n, a); (rem - 1) % (sz - 1); ++rem) + qA.push_front(0); + qB.clear(); + while(rem > 1) + { + int tmp = 0; + for(int cnt = min(rem, sz); cnt; --cnt) + if(qB.L == qB.R || qA.L != qA.R && qA.que[qA.L] <= qB.que[qB.L]) + tmp += qA.pop_front(); + else + tmp += qB.pop_front(); + if(tmp > mx - cost) + return 0; + cost += tmp; + qB.push_back(tmp); + rem -= min(rem, sz) - 1; + } + return 1; +} +int main() +{ + scanf("%d", &t); + while(t--) + { + scanf("%d%d", &n, &mx); + for(int i = 0; i < n; ++i) + scanf("%d", a + i); + sort(a, a + n); + int L = 2, R = n; + while(L < R) + { + int M = L + R >> 1; + if(check(M)) + R = M; + else + L = M + 1; + } + printf("%d\n", L); + } + return 0; +} diff --git a/Summer_Training/2016-09-17/I - Tower Defence - tjz.cpp b/Summer_Training/2016-09-17/I - Tower Defence - tjz.cpp new file mode 100644 index 00000000..3fbf04aa --- /dev/null +++ b/Summer_Training/2016-09-17/I - Tower Defence - tjz.cpp @@ -0,0 +1,144 @@ +#include +#include +#include +typedef long long LL; +const int maxn = 100001; +int t, n, tot, lnk[maxn], f[maxn][3], fro[maxn][2], idx[maxn][2], down[maxn][2], up[maxn][2]; +LL ans; +struct Edge +{ + int nxt, v, w; +} e[maxn << 1]; +void pfs(int u, int fa) +{ + f[u][0] = f[u][1] = f[u][2] = 0; + fro[u][0] = fro[u][1] = idx[u][0] = idx[u][1] = u; + down[u][0] = -1; + for(int it = lnk[u]; it != -1; it = e[it].nxt) + { + int v = e[it].v, w = e[it].w; + if(v == fa) + continue; + pfs(v, u); + if(f[u][0] < f[v][0] + w) + { + f[u][2] = f[u][1]; + f[u][1] = f[u][0]; + fro[u][1] = fro[u][0]; + idx[u][1] = idx[u][0]; + f[u][0] = f[v][0] + w; + fro[u][0] = v; + idx[u][0] = idx[v][0]; + } + else if(f[u][1] < f[v][0] + w) + { + f[u][2] = f[u][1]; + f[u][1] = f[v][0] + w; + fro[u][1] = v; + idx[u][1] = idx[v][0]; + } + else if(f[u][2] < f[v][0] + w) + f[u][2] = f[v][0] + w; + if(down[u][0] < down[v][0]) + { + down[u][0] = down[v][0]; + down[u][1] = down[v][1]; + } + } + if(down[u][0] < f[u][0] + f[u][1]) + { + down[u][0] = f[u][0] + f[u][1]; + down[u][1] = idx[u][0]; + } + // printf("down %d : %d %d\n", u, down[u][0], down[u][1]); +} +void dfs(int u, int fa) +{ + int cur[2][2] = {}, cidx = u; + cur[0][0] = up[u][0]; + cur[0][1] = up[u][1]; + for(int it = lnk[u]; it != -1; it = e[it].nxt) + { + int v = e[it].v; + if(v == fa) + continue; + if(cur[0][0] < down[v][0]) + { + cur[1][0] = cur[0][0]; + cur[1][1] = cur[0][1]; + cur[0][0] = down[v][0]; + cur[0][1] = down[v][1]; + cidx = v; + } + else if(cur[1][0] < down[v][0]) + { + cur[1][0] = down[v][0]; + cur[1][1] = down[v][1]; + } + } + for(int it = lnk[u]; it != -1; it = e[it].nxt) + { + int v = e[it].v, w = e[it].w; + if(v == fa) + continue; + int i = fro[u][0] == v; + if(f[v][0] < f[u][i] + w) + { + f[v][2] = f[v][1]; + f[v][1] = f[v][0]; + fro[v][1] = fro[v][0]; + idx[v][1] = idx[v][0]; + f[v][0] = f[u][i] + w; + fro[v][0] = u; + idx[v][0] = idx[u][i]; + } + else if(f[v][1] < f[u][i] + w) + { + f[v][2] = f[v][1]; + f[v][1] = f[u][i] + w; + fro[v][1] = u; + idx[v][1] = idx[u][i]; + } + else if(f[v][2] < f[u][i] + w) + f[v][2] = f[u][i] + w; + int k = cidx == v; + up[v][0] = cur[k][0]; + up[v][1] = cur[k][1]; + int j = (i || fro[u][1] == v) + 1; + if(up[v][0] < f[u][i] + f[u][j]) + { + up[v][0] = f[u][i] + f[u][j]; + up[v][1] = idx[u][i]; + } + // printf("up %d : %d %d\n", v, up[v][0], up[v][1]); + ans += std::max(up[v][0], down[v][0]); + // printf("%d : %d %d\n", v, up[v][0], down[v][0]); + dfs(v, u); + } +} +int main() +{ + scanf("%d", &t); + while(t--) + { + scanf("%d", &n); + tot = 0; + memset(lnk + 1, -1, n * sizeof(int)); + for(int i = 1; i < n; ++i) + { + int u, v, w; + scanf("%d%d%d", &u, &v, &w); + e[tot] = (Edge){lnk[u], v, w}; + lnk[u] = tot++; + e[tot] = (Edge){lnk[v], u, w}; + lnk[v] = tot++; + } + pfs(1, -1); + up[1][0] = 0; + up[1][1] = 1; + ans = 0; + dfs(1, -1); + printf("%I64d\n", ans); + } + return 0; +} diff --git a/Summer_Training/2016-09-17/K - Barricade - lzy.cpp b/Summer_Training/2016-09-17/K - Barricade - lzy.cpp new file mode 100644 index 00000000..09058d20 --- /dev/null +++ b/Summer_Training/2016-09-17/K - Barricade - lzy.cpp @@ -0,0 +1,104 @@ +#include +#include + +#include + +using namespace std; + +#define N 1000 +#define M 10000 + +const int inf = 0x3fffffff; + +struct Edge {int v, w; Edge *x;} *e, *adj[N + 9], *lnk[N + 9], E[8 * M + 9]; + +int TT, n, m, u, v, w, S, T, s[N + 9]; +Edge *pre[N + 9]; +int fat[N + 9]; +int l, r, que[N + 9]; + +inline +void add(int u, int v, int w, Edge *c[]) { + *e = (Edge){v, w, c[u]}; + c[u] = e++; +} + +void bfs() { + memset(s, -1, sizeof s); + s[que[(r = 0)++] = S] = 0; + for (l = 0; l < r; ++l) { + u = que[l]; + //printf("u: %d\n", u); + for (Edge *p = adj[u]; p; p = p->x) { + //printf("v: %d %d\n", p->v, s[p->v]); + if (s[p->v] == -1) { + s[que[r++] = p->v] = s[u] + 1; + } + } + } +} + +bool bfs(int S, int T) { + for (int i = 1; i <= n; ++i) pre[i] = 0, fat[i] = 0; + fat[que[(r = 0)++] = S] = -1; + for (l = 0; l < r; ++l) { + u = que[l]; + //printf("u: %d\n", u); + for (Edge *p = lnk[u]; p; p = p->x) { + //printf("%d - %d %d\n", u, p->v, p->w); + if (!fat[p->v] && p->w) { + //printf("%d - %d %d\n", u, p->v, p->w); + pre[que[r++] = p->v] = p; + fat[p->v] = u; + } + } + if (pre[T]) return true; + } + return false; +} + +int main() { + scanf("%d", &TT); + while (TT--) { + scanf("%d%d", &n, &m); + + for (int i = 1; i <= n; ++i) { + adj[i] = 0; + lnk[i] = 0; + } + e = E; + while (m--) { + scanf("%d%d%d", &u, &v, &w); + add(u, v, w, adj); + add(v, u, w, adj); + } + S = n, T = 1; + bfs(); + for (u = 1; u <= n; ++u) { + for (Edge *p = adj[u]; p; p = p->x) + if (s[u] + 1 == s[p->v]) { + //printf("%d - %d %d\n", u, p->v, p->w); + add(u, p->v, p->w, lnk); + add(p->v, u, 0, lnk); + } + } + int mxf = 0, det; + while (bfs(S, T)) { + det = inf; + for (Edge *p = pre[T]; p; p = pre[fat[p->v]]) { + //printf("%d %d\n", fat[p->v], p->v); + det = min(det, p->w); + } + //printf("det: %d\n", det); + for (Edge *p = pre[T]; p; p = pre[fat[p->v]]) { + + p->w -= det; + E[(p - E) ^ 1].w += det; + } + mxf += det; + } + printf("%d\n", mxf); + } + return 0; +} + diff --git a/Summer_Training/2016-09-18/A - Resident Evil - tjz.cpp b/Summer_Training/2016-09-18/A - Resident Evil - tjz.cpp new file mode 100644 index 00000000..4c63b81c --- /dev/null +++ b/Summer_Training/2016-09-18/A - Resident Evil - tjz.cpp @@ -0,0 +1,85 @@ +#include +#include +#include +typedef long long LL; +const int maxn = 3001, maxd = 50; +int n, m; +LL bit[maxn][maxn][4]; +void upd(int x, int y, LL val) +{ + for(int i = x; i <= n; i += i & -i) + for(int j = y; j <= n; j += j & -j) + { + bit[i][j][0] ^= val; + if(x & 1) + bit[i][j][1] ^= val; + if(y & 1) + bit[i][j][2] ^= val; + if((x & 1) && (y & 1)) + bit[i][j][3] ^= val; + } +} +LL que(int x, int y) +{ + LL ret = 0; + for(int i = x; i > 0; i -= i & -i) + for(int j = y; j > 0; j -= j & -j) + { + if(!(x & 1) && !(y & 1)) + ret ^= bit[i][j][0]; + if(!(y & 1)) + ret ^= bit[i][j][1]; + if(!(x & 1)) + ret ^= bit[i][j][2]; + ret ^= bit[i][j][3]; + } + return ret; +} +inline void scan(int &x) +{ + register int ch; + while((ch = getchar()) < '0' || ch > '9'); + for(x = ch - '0'; (ch = getchar()) >= '0' && ch <= '9'; x = (x << 3) + (x << 1) + (ch - '0')); +} +int main() +{ + while(scanf("%d%d", &n, &m) == 2) + { + for(int i = 1; i <= n; ++i) + memset(bit[i] + 1, 0, (n << 2) * sizeof(LL)); + while(m--) + { + char op[2]; + int x1, y1, x2, y2; + LL mask = 0; + scanf("%s", op); scan(x1); scan(y1); scan(x2); scan(y2); + if(x1 > x2) + std::swap(x1, x2); + if(y1 > y2) + std::swap(y1, y2); + if(op[0] == 'P') + { + int k, a, b; + scan(k); + while(k--) + { + scan(a); scan(b); + if(b & 1) + mask ^= 1LL << --a; + } + upd(x1, y1, mask); + upd(x2 + 1, y1, mask); + upd(x1, y2 + 1, mask); + upd(x2 + 1, y2 + 1, mask); + } + else + { + mask = que(x2, y2) ^ que(x1 - 1, y2) ^ que(x2, y1 - 1) ^ que(x1 - 1, y1 - 1); + for(int i = 0; i < maxd; ++i) + printf("%d ", (mask >> i) & 1 ? 2 : 1); + putchar('\n'); + } + } + } + return 0; +} diff --git "a/Summer_Training/2016-09-18/C - hannnnah_j\342\200\231s Biological Test - tjz.cpp" "b/Summer_Training/2016-09-18/C - hannnnah_j\342\200\231s Biological Test - tjz.cpp" new file mode 100644 index 00000000..97da5587 --- /dev/null +++ "b/Summer_Training/2016-09-18/C - hannnnah_j\342\200\231s Biological Test - tjz.cpp" @@ -0,0 +1,34 @@ +#include +typedef long long LL; +const int maxn = 1000001, mod = 1000000007; +int t, n, m, k, fact[maxn], iact[maxn]; +int main() +{ + iact[1] = 1; + for(int i = 2; i < maxn; ++i) + iact[i] = mod - (int)(mod / i * (LL)iact[mod % i] % mod); + fact[0] = iact[0] = 1; + for(int i = 1; i < maxn; ++i) + { + fact[i] = (LL)fact[i - 1] * i % mod; + iact[i] = (LL)iact[i - 1] * iact[i] % mod; + } + scanf("%d", &t); + while(t--) + { + scanf("%d%d%d", &n, &m, &k); + if(m == 1) + { + printf("%d\n", n); + continue; + } + if(m - 1 > n - m * k - 1) + { + puts("0"); + continue; + } + int ans = (LL)n % mod * fact[n - m * k - 1] % mod * iact[m] % mod * iact[n - m * k - m] % mod; + printf("%d\n", ans); + } + return 0; +} diff --git a/Summer_Training/2016-09-18/D - Mathematician QSC - tjz.cpp b/Summer_Training/2016-09-18/D - Mathematician QSC - tjz.cpp new file mode 100644 index 00000000..5a63fa67 --- /dev/null +++ b/Summer_Training/2016-09-18/D - Mathematician QSC - tjz.cpp @@ -0,0 +1,87 @@ +#include +typedef long long LL; +const int maxn = 14, maxp = 31623; +int p; +struct Matrix +{ + int num1, num2; + Matrix operator * (const Matrix &x) const + { + return (Matrix){((LL)num1 * x.num1 + (LL)num2 * x.num2) % p, + ((LL)num1 * x.num2 + (LL)num2 * (x.num1 + (x.num2 << 1))) % p}; + } + Matrix pow(LL k) + { + Matrix ret = (Matrix){1, 0}, tmp = *this; + while(k) + { + if(k & 1) + ret = ret * tmp; + tmp = tmp * tmp; + k >>= 1; + } + return ret; + } +} A = {0, 1}; +int f[maxn], g[maxn], tot, prime[maxp], t, n, y, x, s; +bool vis[maxp]; +int mod_pow(int x, int k, int p) +{ + int ret = 1 % p; + for(x %= p; k > 0; k >>= 1, x = (LL)x * x % p) + if(k & 1) + ret = (LL)ret * x % p; + return ret; +} +int phi(int x) +{ + int ret = x; + for(int i = 0; i < tot && prime[i] * prime[i] <= x; ++i) + if(x % prime[i] == 0) + { + ret -= ret / prime[i]; + for(x /= prime[i]; x % prime[i] == 0; x /= prime[i]); + } + if(x > 1) + ret -= ret / x; + return ret; +} +int main() +{ + f[1] = g[1] = 1; + for(int i = 2; i < maxn; ++i) + { + f[i] = f[i - 2] + (f[i - 1] << 1); + g[i] = g[i - 1] + f[i] * f[i]; + } + for(int i = 2; i < maxp; ++i) + { + if(!vis[i]) + prime[tot++] = i; + for(int j = 0, o; j < tot && (o = i * prime[j]) < maxp; ++j) + { + vis[o] = 1; + if(i % prime[j] == 0) + break; + } + } + scanf("%d", &t); + while(t--) + { + scanf("%d%d%d%d", &n, &y, &x, &s); + ++s; + int tmp = phi(s), val; + if((LL)n * y < maxn) + val = g[n * y]; + else + { + p = tmp << 1; + Matrix tmp2 = A.pow((LL)n * y + 1); + val = ((LL)tmp2.num1 * tmp2.num2 % p) >> 1; + val += tmp; + } + val = mod_pow(x, val, s); + printf("%d\n", val); + } + return 0; +} diff --git a/Summer_Training/2016-09-18/E - Running King - tjz.cpp b/Summer_Training/2016-09-18/E - Running King - tjz.cpp new file mode 100644 index 00000000..b15a7516 --- /dev/null +++ b/Summer_Training/2016-09-18/E - Running King - tjz.cpp @@ -0,0 +1,112 @@ +#include +#include +typedef long long LL; +const int maxn = 200001, maxlen = 18, maxm = 1 << maxlen, mod = 1004535809, gen = 3; +int w[maxm], inv2[maxlen + 1]; +int inv[maxn], f[maxn], g[maxn]; +inline int mod_pow(int x, int k) +{ + int ret = 1; + for( ; k; k >>= 1, x = (LL)x * x % mod) + if(k & 1) + ret = (LL)ret * x % mod; + return ret; +} +inline int mod_add(int x, int y) +{ + return x + y >= mod ? x + y - mod : x + y; +} +inline int mod_sub(int x, int y) +{ + return x - y < 0 ? x - y + mod : x - y; +} +inline void NTT(int len, int x[], int flag) +{ + for(int i = 0, j = 0; i < len; ++i) + { + if(i > j) + std::swap(x[i], x[j]); + for(int l = len >> 1; (j ^= l) < l; l >>= 1); + } + for(int i = 1, d = 1; d < len; ++i, d <<= 1) + for(int j = 0; j < len; j += d << 1) + for(int k = 0; k < d; ++k) + { + int t = (LL)w[(maxm >> i) * k] * x[j + k + d] % mod; + x[j + d + k] = mod_sub(x[j + k], t); + x[j + k] = mod_add(x[j + k], t); + } + if(flag == -1) + { + std::reverse(x + 1, x + len); + int bitLen = 0; + for( ; 1 << bitLen < len; ++bitLen); + int val = inv2[bitLen]; + for(int i = 0; i < len; ++i) + x[i] = (LL)x[i] * val % mod; + } +} +int B[maxm], C[maxm]; +void cdq(int L, int R) +{ + if(L == R) + { + if(L) + f[L] = (LL)f[L] * inv[L] % mod; + return; + } + int M = (L + R) >> 1, len, plen = R - L + 1; + cdq(L, M); + for(len = 1; len < plen; len <<= 1); + for(int i = 0; i < len; ++i) + { + B[i] = L + i <= M ? f[L + i] : 0; + C[i] = i < plen ? g[i] : 0; + } + NTT(len, B, 1); + NTT(len, C, 1); + for(int i = 0; i < len; ++i) + B[i] = (LL)B[i] * C[i] % mod; + NTT(len, B, -1); + for(int i = M + 1; i <= R; ++i) + if((f[i] += B[i - L - 1]) >= mod) + f[i] -= mod; + cdq(M + 1, R); +} +int t, n; +int main() +{ + w[0] = 1; + w[1] = mod_pow(gen, (mod - 1) >> maxlen); // make sure that mod = 2 ^ maxlen * k + 1 + for(int i = 2; i < maxm; ++i) + w[i] = (LL)w[i - 1] * w[1] % mod; + inv2[0] = 1; + inv2[1] = (mod + 1) >> 1; + for(int i = 2; i <= maxlen; ++i) + inv2[i] = (LL)inv2[i - 1] * inv2[1] % mod; + inv[1] = 1; + for(int i = 2; i < maxn; ++i) + inv[i] = mod - (int)(mod / i * (LL)inv[mod % i] % mod); + f[0] = g[0] = 1; + for(int i = 1, iact = 1; i < maxn; ++i) + { + iact = (LL)iact * inv[i] % mod; + g[i] = (LL)mod_pow(i + 1, i - 1) * iact % mod; + } + cdq(0, maxn - 1); + for(int i = 1, fact = 1; i < maxn; ++i) + { + fact = (LL)fact * i % mod; + f[i] = (LL)f[i] * fact % mod; + } + scanf("%d", &t); + while(t--) + { + scanf("%d", &n); + int ans = mod_pow(2, ((LL)n * (n - 1) >> 1) % (mod - 1)) - f[n]; + if(ans < 0) + ans += mod; + printf("%d\n", ans); + } + return 0; +} diff --git a/Summer_Training/2016-09-18/G - odd-even number - lzy.cpp b/Summer_Training/2016-09-18/G - odd-even number - lzy.cpp new file mode 100644 index 00000000..92aa23b7 --- /dev/null +++ b/Summer_Training/2016-09-18/G - odd-even number - lzy.cpp @@ -0,0 +1,84 @@ +#include + +#include + +using namespace std; + + +typedef long long i64; + +int T, a[19]; +i64 f[19][2][2], g[19][2][2]; + +i64 calc(i64 x) { + int m = 0; + if (x == 0) return 0; // 0, 1 + while (x) { + a[m++] = x % 10; + x /= 10; + } + i64 sum = 0; + for (int n = 1; n <= m; ++n) { + memset(f, 0, sizeof f); + memset(g, 0, sizeof g); + if (n == m) { + //f[n][0][0] = 1; + g[n][0][0] = 1; + } else { + //f[n][1][0] = 1; + g[n][1][0] = 1; + } + for (int i = n - 1; i >= 0; --i) { + for (int j = 0; j <= 9; ++j) { + if (i == n - 1 && j == 0) continue; + if (j < a[i]) { + if (j & 1) { + g[i][1][0] += g[i + 1][0][1]; + g[i][1][1] += g[i + 1][0][0]; + g[i][1][1] += f[i + 1][0][1]; + } else { + f[i][1][0] += f[i + 1][0][1]; + f[i][1][1] += f[i + 1][0][0]; + f[i][1][1] += g[i + 1][0][0]; + } + } + if (j == a[i]) { + if (j & 1) { + g[i][0][0] += g[i + 1][0][1]; + g[i][0][1] += g[i + 1][0][0]; + g[i][0][1] += f[i + 1][0][1]; + } else { + f[i][0][0] += f[i + 1][0][1]; + f[i][0][1] += f[i + 1][0][0]; + f[i][0][1] += g[i + 1][0][0]; + } + } + if (j & 1) { + g[i][1][0] += g[i + 1][1][1]; + g[i][1][1] += g[i + 1][1][0]; + g[i][1][1] += f[i + 1][1][1]; + } else { + f[i][1][0] += f[i + 1][1][1]; + f[i][1][1] += f[i + 1][1][0]; + f[i][1][1] += g[i + 1][1][0]; + } + } + } + sum += f[0][0][1] + f[0][1][1] + g[0][0][0] + g[0][1][0]; + //cout << f[0][0][1] << ' ' << f[0][1][1] << ' ' << g[0][0][0] << ' ' << g[0][1][0] << endl; + //cout << "sum: " << sum << endl; + } + return sum; +} + +int main() { + cin >> T; + for (int t = 1; t <= T; ++t) { + i64 l, r; + cin >> l >> r; + //cout << "r: " << calc(r) << endl; + cout << "Case #" << t << ": "; + cout << calc(r) - calc(l - 1) << endl; + } + return 0; +} diff --git a/Summer_Training/2016-09-18/H - oasis in desert - tjz.cpp b/Summer_Training/2016-09-18/H - oasis in desert - tjz.cpp new file mode 100644 index 00000000..6d4eb2f0 --- /dev/null +++ b/Summer_Training/2016-09-18/H - oasis in desert - tjz.cpp @@ -0,0 +1,171 @@ +#include +#include +#include +using namespace std; +const int maxn = 510, INF = 0x3f3f3f3f; +int t, n, m, lim, g[maxn][maxn], col[maxn], dsu[maxn], cnt[maxn], tot, ans[maxn]; +bool vis[maxn]; +inline void upd(int &x, int y) +{ + if(x > y) + x = y; +} +int dsu_find(int u) +{ + return dsu[u] < 0 ? u : dsu[u] = dsu_find(dsu[u]); +} +void dsu_merge(int u, int v) +{ + u = dsu_find(u); + v = dsu_find(v); + if(u == v) + return; + if(dsu[u] < dsu[v]) + dsu[v] = u; + else if(dsu[u] > dsu[v]) + dsu[u] = v; + else + { + dsu[v] = u; + --dsu[u]; + } +} +bool dfs(int u) +{ + for(int v = 0; v < n; ++v) + if(u != v && g[u][v] <= lim) + { + if(col[v] == -1) + { + dsu_merge(u, v); + col[v] = 1 - col[u]; + if(!dfs(v)) + return 0; + } + else if(col[v] + col[u] != 1) + return 0; + } + return 1; +} +int match[maxn]; +bool p_vis[maxn]; +bool path(int u) +{ + for(int v = 0; v < n; ++v) + if(g[u][v] <= lim && col[u] + col[v] == 1 && !p_vis[v]) + { + p_vis[v] = 1; + if(match[v] == -1 || path(match[v])) + { + match[v] = u; + return 1; + } + } + return 0; +} +int main() +{ + scanf("%d", &t); + while(t--) + { + scanf("%d%d%d", &n, &m, &lim); + for(int i = 0; i < n; ++i) + { + memset(g[i], 0x3f, n * sizeof(int)); + g[i][i] = 0; + } + while(m--) + { + int u, v, w; + scanf("%d%d%d", &u, &v, &w); + --u, --v; + if(g[u][v] > w) + g[u][v] = g[v][u] = w; + } + if(!n) + { + puts("0"); + continue; + } + /* + if(n & 1) + { + puts("Impossible"); + continue; + } + */ + for(int k = 0; k < n; ++k) + for(int i = 0; i < n; ++i) + for(int j = 0; j < n; ++j) + upd(g[i][j], g[i][k] + g[k][j]); + memset(dsu, -1, n * sizeof(int)); + memset(col, -1, n * sizeof(int)); + bool flag = 1; + for(int i = 0; i < n; ++i) + if(col[i] == -1) + { + col[i] = 0; + if(!dfs(i)) + { + flag = 0; + break; + } + } + if(!flag) + { + puts("Impossible"); + continue; + } + memset(cnt, 0, n * sizeof(int)); + for(int i = 0; i < n; ++i) + col[i] == 1 ? ++cnt[dsu_find(i)] : --cnt[dsu_find(i)]; + for(int i = 0; i < n; ++i) + if(i == dsu_find(i) && cnt[i] != 0) + { + flag = 0; + break; + } + if(!flag) + { + puts("Impossible"); + continue; + } + tot = 0; + memset(vis, 0, n * sizeof(bool)); + memset(match, -1, n * sizeof(int)); + for(int i = 0; i < n; ++i) + if(!vis[i]) + { + int color = col[i]; + for(int j = 0; j < n; ++j) + if(dsu_find(j) == dsu_find(i) && col[j] == color) + { + memset(p_vis, 0, n * sizeof(bool)); + if(!path(j)) + { + flag = 0; + break; + } + } + if(!flag) + break; + for(int j = 0; j < n; ++j) + if(dsu_find(j) == dsu_find(i)) + { + vis[j] = 1; + if(col[j] == color) + ans[tot++] = j; + } + } + if(!flag) + { + puts("Impossible"); + continue; + } + sort(ans, ans + tot); + printf("%d\n", tot); + for(int i = 0; i < tot; ++i) + printf("%d%c", ans[i] + 1, " \n"[i == tot - 1]); + } + return 0; +} diff --git a/Summer_Training/2016-09-18/I - QSC and Master - lzy.cpp b/Summer_Training/2016-09-18/I - QSC and Master - lzy.cpp new file mode 100644 index 00000000..be70db56 --- /dev/null +++ b/Summer_Training/2016-09-18/I - QSC and Master - lzy.cpp @@ -0,0 +1,54 @@ +#include +#include +#include + +using namespace std; + +#define N 300 + +typedef long long i64; + +const i64 inf = 0x3ffffffffffff; + +int T, n; +int k[N + 9], v[N + 9]; +i64 f[N + 9][N + 9][2]; +bool g[N + 9][N + 9]; + +void upd(i64 &a, i64 b) { + if (a < b) a = b; +} + +int main() { + scanf("%d", &T); + while (T--) { + scanf("%d", &n); + for (int i = 1; i <= n; ++i) + scanf("%d", k + i); + for (int i = 1; i <= n; ++i) + scanf("%d", v + i); + for (int i = 1; i <= n; ++i) + for (int j = 1; j <= n; ++j) + f[i][j][0] = f[i][j][1] = -inf; + memset(g, 0, sizeof g); + for (int i = 1; i <= n; ++i) + for (int j = i + 1; j <= n; ++j) + g[i][j] = __gcd(k[i], k[j]) > 1; + for (int l = n; l >= 1; --l) { + f[l][l][0] = 0; + f[l][l - 1][1] = 0; + for (int r = l + 1; r <= n; ++r) { + for (int i = l; i < r; ++i) { + upd(f[l][r][0], f[l][i][0] + f[i + 1][r][0]); + upd(f[l][r][1], f[l][i][1] + f[i + 1][r][1]); + } + if (g[l][r]) { + upd(f[l][r][0], f[l + 1][r - 1][1] + v[l] + v[r]); + upd(f[l][r][1], f[l + 1][r - 1][1] + v[l] + v[r]); + } + } + } + printf("%I64d\n", f[1][n][0]); + } + return 0; +} diff --git a/Summer_Training/2016-09-18/J - Count primes - tjz.cpp b/Summer_Training/2016-09-18/J - Count primes - tjz.cpp new file mode 100644 index 00000000..55359a1c --- /dev/null +++ b/Summer_Training/2016-09-18/J - Count primes - tjz.cpp @@ -0,0 +1,43 @@ +#include +#include +typedef long long LL; +const int maxp = 1000001; +int tot, prime[maxp], s[maxp]; +LL solve(LL n, int k) +{ + if(k <= 1) + return n - (k ? (n >> 1) : 0); + if(n <= prime[k - 1]) + return 1; + if(n < maxp) + { + if(s[(int)n] <= k) + return 1; + if(s[(int)sqrt(n)] <= k) + return s[n] - k + 1; + } + return solve(n, k - 1) - solve(n / prime[k - 1], k - 1); +} +int main() +{ + for(int i = 2; i < maxp; ++i) + { + if(!s[i]) + prime[tot++] = i; + for(int j = 0, k = (maxp - 1) / i; j < tot && prime[j] <= k; ++j) + { + s[i * prime[j]] = 1; + if(i % prime[j] == 0) + break; + } + s[i] = s[i - 1] + (!s[i]); + } + LL n, ans; + while(scanf("%I64d", &n) == 1) + { + int sqn = (int)sqrt(n); + ans = solve(n, s[sqn]) + s[sqn] - 1; + printf("%I64d\n", ans); + } + return 0; +} diff --git a/Summer_Training/2016-09-19/A - Xiongnu's Land - lzy.cpp b/Summer_Training/2016-09-19/A - Xiongnu's Land - lzy.cpp new file mode 100644 index 00000000..b5c50b3b --- /dev/null +++ b/Summer_Training/2016-09-19/A - Xiongnu's Land - lzy.cpp @@ -0,0 +1,48 @@ +#include +#include + +using namespace std; + +#define N 10000 + +typedef long long i64; +struct Rect {int x, len;}; + +int T, R, n, m, x, y, w, h, cur; +i64 sum, all, now; +Rect r[2 * N + 9]; + +bool cmp(const Rect &a, const Rect &b) { + return a.x < b.x; +} + +int main() { + scanf("%d", &T); + while (T--) { + scanf("%d", &R); + scanf("%d", &n); + m = 0; + all = 0; + while (n--) { + scanf("%d%d%d%d", &x, &y, &w, &h); + all += (i64)w * h; + r[m++] = (Rect){x, h}; + r[m++] = (Rect){x + w, -h}; + } + sort(r, r + m, cmp); + n = 0; + cur = 0, sum = 0, now = -1; + int ans = R; + for (int i = 0; i <= R; ++i) { + if (sum * 2 >= all) { + if (now == -1) now = sum; + if (now == sum) ans = i; + } + //printf("%d %d %lld %d %lld\n", i, ans, sum, cur, now); + while (n < m && r[n].x == i) cur += r[n++].len; + sum += cur; + } + printf("%d\n", ans); + } + return 0; +} diff --git a/Summer_Training/2016-09-19/E - Stamps - tjz.cpp b/Summer_Training/2016-09-19/E - Stamps - tjz.cpp new file mode 100644 index 00000000..01266887 --- /dev/null +++ b/Summer_Training/2016-09-19/E - Stamps - tjz.cpp @@ -0,0 +1,46 @@ +#include +#include +typedef long long LL; +const int maxn = 100001, maxk = 11, mod = 1000003; +int mod_pow(int x, int k) +{ + int ret = 1; + for( ; k > 0; k >>= 1, x = (LL)x * x % mod) + if(k & 1) + ret = (LL)ret * x % mod; + return ret; +} +int mod_inv(int x) +{ + return x <= 1 ? x : mod - mod / x * (LL)mod_inv(mod % x) % mod; +} +int fact[maxn], inv[maxn], f[maxk][maxn], g[maxn]; +int main() +{ + inv[1] = 1; + for(int i = 2; i < maxn; ++i) + inv[i] = mod - (int)(mod / i * (LL)inv[mod % i] % mod); + fact[0] = 1; + for(int i = 1; i < maxn; ++i) + { + f[0][i] = g[i] = 1; + fact[i] = fact[i - 1]; + for(int j = 0; j < 10; ++j) + fact[i] = (LL)fact[i] * i % mod; + } + for(int i = 1; i < maxk; ++i) + { + memcpy(f[i], f[i - 1], sizeof f[0]); + for(int j = 2; j < maxn; ++j) + { + g[j] = g[j] * (1LL + inv[j - 1]) % mod; + f[i][j] = (f[i][j] + (LL)g[j] * f[i][j - 1]) % mod; + } + } + for(int n, k, ans; scanf("%d%d", &n, &k) == 2 && !(!n && !k); ) + { + ans = (LL)f[k + 1][n] * fact[n] % mod; + printf("%d\n", ans); + } + return 0; +} diff --git a/Summer_Training/2016-09-19/G - Mysterious Antiques in Sackler Museum - lzy.cpp b/Summer_Training/2016-09-19/G - Mysterious Antiques in Sackler Museum - lzy.cpp new file mode 100644 index 00000000..1c7795a4 --- /dev/null +++ b/Summer_Training/2016-09-19/G - Mysterious Antiques in Sackler Museum - lzy.cpp @@ -0,0 +1,68 @@ +#include +#include + +using namespace std; + +struct Rect {int x, y;}; + +int T; +Rect r[4], s[3]; + +bool get(Rect a, Rect b, Rect c) { + if (a.x == b.x) { + if (a.x == c.x) return true; + if (a.x == c.y) return true; + if (a.y + b.y == c.x) return true; + if (a.y + b.y == c.y) return true; + } + swap(a.x, a.y); + if (a.x == b.x) { + if (a.x == c.x) return true; + if (a.x == c.y) return true; + if (a.y + b.y == c.x) return true; + if (a.y + b.y == c.y) return true; + } + swap(b.x, b.y); + if (a.x == b.x) { + if (a.x == c.x) return true; + if (a.x == c.y) return true; + if (a.y + b.y == c.x) return true; + if (a.y + b.y == c.y) return true; + } + swap(a.x, a.y); + if (a.x == b.x) { + if (a.x == c.x) return true; + if (a.x == c.y) return true; + if (a.y + b.y == c.x) return true; + if (a.y + b.y == c.y) return true; + } + return false; +} + +bool gao(Rect r[]) { + if (get(r[0], r[1], r[2])) return true; + if (get(r[1], r[2], r[0])) return true; + if (get(r[2], r[0], r[1])) return true; + return false; +} + +bool gao() { + int n; + for (int i = 0; i < 4; ++i) { + n = 0; + for (int j = 0; j < 4; ++j) + if (j != i) s[n++] = r[j]; + if (gao(s)) return true; + } + return false; +} + +int main() { + cin >> T; + while (T--) { + for (int i = 0; i < 4; ++i) + cin >> r[i].x >> r[i].y; + puts(gao() ? "Yes" : "No"); + } + return 0; +} diff --git a/Summer_Training/2016-09-19/J - Osu! Master - tjz.cpp b/Summer_Training/2016-09-19/J - Osu! Master - tjz.cpp new file mode 100644 index 00000000..6f7b8a85 --- /dev/null +++ b/Summer_Training/2016-09-19/J - Osu! Master - tjz.cpp @@ -0,0 +1,32 @@ +#include +int n, p, x, ans; +char op[2]; +int main() +{ + while(scanf("%d", &n) == 1) + { + ans = 0; + p = 0; + while(n--) + { + scanf("%s", op); + if(op[0] == 'S') + { + if(p != 0) + ++ans; + p = -1; + } + else + { + scanf("%d", &x); + if(x != p + 1) + ++ans; + p = x; + } + } + if(p != 0) + ++ans; + printf("%d\n", ans); + } + return 0; +} diff --git a/Summer_Training/2016-09-19/K - A Math Problem - tjz.cpp b/Summer_Training/2016-09-19/K - A Math Problem - tjz.cpp new file mode 100644 index 00000000..f8280afd --- /dev/null +++ b/Summer_Training/2016-09-19/K - A Math Problem - tjz.cpp @@ -0,0 +1,55 @@ +#include +#include +typedef long long LL; +const int maxp = 5, prime[maxp] = {3, 5, 17, 257, 65537}; +const int maxl = 60, maxv = 65537; +int pw[maxp][maxl], t, id, len; +LL cnt[maxp][maxl][maxv], n, f[maxv]; +int main() +{ + for(int o = 0; o < maxp; ++o) + { + const int &mod = prime[o]; + pw[o][0] = cnt[o][0][0] = 1; + for(int i = 1; i < maxl; ++i) + { + pw[o][i] = 3 * pw[o][i - 1] % mod; + for(int j = 0; j < mod; ++j) + { + cnt[o][i][3 * j % mod] += cnt[o][i - 1][j]; + cnt[o][i][(3 * j + 1) % mod] += cnt[o][i - 1][j]; + } + } + } + scanf("%d", &t); + while(t--) + { + scanf("%lld%d", &n, &id); + ++n; + for(int o = 0; o < maxp; ++o) + if(id == prime[o]) + { + id = o; + break; + } + const int &mod = prime[id]; + memset(f, 0, mod * sizeof(LL)); + for(len = 1; 1LL << len <= n; ++len); + for(int i = len - 1, val = 0; i >= 0; --i) + { + if((n >> i) & 1) + { + int delta = 3LL * val * pw[id][i] % mod; + for(int j = 0, k = delta; j < mod; ++j, k = k + 1 < mod ? k + 1 : 0) + f[k] += cnt[id][i][j]; + } + val = (3 * val + ((n >> i) & 1)) % mod; + } + --f[0]; + LL ans = 0; + for(int i = 0; i < mod; ++i) + ans ^= f[i]; + printf("%lld\n", ans); + } + return 0; +} diff --git a/Summer_Training/2016-09-19/upsolved/I - Snake Carpet - tjz.cpp b/Summer_Training/2016-09-19/upsolved/I - Snake Carpet - tjz.cpp new file mode 100644 index 00000000..c93d6a77 --- /dev/null +++ b/Summer_Training/2016-09-19/upsolved/I - Snake Carpet - tjz.cpp @@ -0,0 +1,45 @@ +#include +void print(int n) +{ + if(n == 2) + { + puts("1 1"); + puts("1 2 1 3"); + } + else if(n == 3) + { + puts("1 1"); + puts("2 1 2 2"); + puts("1 2 1 3 2 3"); + } + else if(n & 1) + { + print(n - 3); + for(int i = n + 1 >> 1; i <= n - 2; ++i) + printf("%d %d ", n - 1 >> 1, i); + for(int i = n - 1 >> 1; i >= 1; --i) + printf("%d %d%c", i, n - 1, " \n"[i == 1]); + for(int i = 1; i <= n - 1 >> 1; ++i) + printf("%d %d ", n - 1 >> 1, i); + for(int i = n - 1 >> 1; i >= 1; --i) + printf("%d %d%c", n + 1 >> 1, i, " \n"[i == 1]); + for(int i = n + 1 >> 1; i <= n - 1; ++i) + printf("%d %d ", n + 1 >> 1, i); + for(int i = n + 1 >> 1; i >= 1; --i) + printf("%d %d%c", i, n, " \n"[i == 1]); + } + else + { + print(n - 1); + for(int i = 1; i <= n >> 1; ++i) + printf("%d %d ", i, n); + for(int i = n >> 1; i >= 1; --i) + printf("%d %d%c", i, n + 1, " \n"[i == 1]); + } +} +int main() +{ + for(int n; scanf("%d", &n) == 1; n == 1 ? (void)puts("1 1") : print(n)) + printf("%d %d\n", n + 1 >> 1, n | 1); + return 0; +} diff --git a/Summer_Training/2016-09-24/A - The Book List - tjz.cpp b/Summer_Training/2016-09-24/A - The Book List - tjz.cpp new file mode 100644 index 00000000..484b5819 --- /dev/null +++ b/Summer_Training/2016-09-24/A - The Book List - tjz.cpp @@ -0,0 +1,66 @@ +#include +#include +#include +#include +#include +#include +using namespace std; +const int maxn = 3010, maxs = 110; +int tot; +struct Trie +{ + map nxt; + set leaf; +} e[maxn]; +char str[maxs]; +int newnode() +{ + map().swap(e[tot].nxt); + set().swap(e[tot].leaf); + return tot++; +} +void dfs(int u, int dep) +{ + for(map::iterator it = e[u].nxt.begin(); it != e[u].nxt.end(); ++it) + { + for(int i = 0; i < dep << 2; ++i) + putchar(' '); + printf("%s\n", (it -> first).c_str()); + dfs(it -> second, dep + 1); + } + for(set::iterator it = e[u].leaf.begin(); it != e[u].leaf.end(); ++it) + { + for(int i = 0; i < dep << 2; ++i) + putchar(' '); + printf("%s\n", (*it).c_str()); + } +} +int main() +{ + for(int Case = 1; gets(str); ++Case) + { + tot = 0; + newnode(); + do + { + int len = strlen(str), i, p, rt = 0; + for(i = 0, p = -1; i < len; ++i) + if(str[i] == '/') + { + str[i] = '\0'; + string edge = str + p + 1; + if(!e[rt].nxt.count(edge)) + e[rt].nxt[edge] = newnode(); + rt = e[rt].nxt[edge]; + str[i] = '/'; + p = i; + } + string book = str + p + 1; + e[rt].leaf.insert(book); + gets(str); + } while(strcmp(str, "0")); + printf("Case %d:\n", Case); + dfs(0, 0); + } + return 0; +} diff --git a/Summer_Training/2016-09-24/C - A Simple Job - tjz.cpp b/Summer_Training/2016-09-24/C - A Simple Job - tjz.cpp new file mode 100644 index 00000000..c1b65af0 --- /dev/null +++ b/Summer_Training/2016-09-24/C - A Simple Job - tjz.cpp @@ -0,0 +1,70 @@ +#include +#include +#include +#include +using namespace std; +const int maxs = 510 * 51; +map Hash; +int len; +char str[maxs]; +int main() +{ + while(gets(str)) + { + len = strlen(str); + str[len++] = '\n'; + str[len] = '\0'; + while(gets(str + len) && strcmp(str + len, "####")) + { + len += strlen(str + len); + str[len++] = '\n'; + str[len] = '\0'; + } + int tlen = 0; + for(int i = 0; i < len; ++i) + if(str[i] >= 'a' && str[i] <= 'z') + str[tlen++] = str[i]; + else if(str[i] == ' ') + { + if(tlen && str[tlen - 1] >= 'a' && str[tlen - 1] <= 'z') + str[tlen++] = ' '; + } + else + { + if(tlen) + { + if(str[tlen - 1] >= 'a' && str[tlen - 1] <= 'z') + str[tlen++] = '#'; + else if(str[tlen - 1] == ' ') + str[tlen - 1] = '#'; + } + } + str[tlen] = '\0'; + // puts(str); + len = tlen; + map().swap(Hash); + for(int i = 0, p1 = -1, p2 = -1; i < len; ++i) + if(str[i] < 'a' || str[i] > 'z') + { + if(p2 != -1 && str[p2] == ' ') + { + char ch = str[i]; + str[i] = '\0'; + ++Hash[(string)(str + p1 + 1)]; + str[i] = ch; + } + p1 = p2; + p2 = i; + } + int cnt = 0; + map::iterator ans; + for(map::iterator it = Hash.begin(); it != Hash.end(); ++it) + if(cnt < it -> second) + { + cnt = it -> second; + ans = it; + } + printf("%s:%d\n", (ans -> first).c_str(), cnt); + } + return 0; +} diff --git a/Summer_Training/2016-09-24/F - Periodic Signal - tjz.cpp b/Summer_Training/2016-09-24/F - Periodic Signal - tjz.cpp new file mode 100644 index 00000000..31e4fcf8 --- /dev/null +++ b/Summer_Training/2016-09-24/F - Periodic Signal - tjz.cpp @@ -0,0 +1,124 @@ +#include +#include +#include +#include +typedef long long LL; +const int maxm = 1 << 18; +const long double pi = acos(-1.0); +struct complex +{ + long double r, i; + complex() {} + complex(long double x, long double y) : r(x), i(y) {} + friend complex operator + (const complex &a, const complex &b) { return complex(a.r + b.r, a.i + b.i); } + friend complex operator - (const complex &a, const complex &b) { return complex(a.r - b.r, a.i - b.i); } + friend complex operator * (const complex &a, const complex &b) { return complex(a.r * b.r - a.i * b.i, a.r * b.i + a.i * b.r); } + complex conj() { return complex(r, -i); } +} X[maxm], Y[maxm]; +void FFT(complex a[], int n, int flag) +{ + for(int i = 1, j = n >> 1, k; i < n - 1; ++i) + { + if(i < j) + std::swap(a[i], a[j]); + for(k = n >> 1; j >= k; k >>= 1) + j -= k; + if(j < k) + j += k; + } + for(int i = 1; i < n; i <<= 1) + { + complex wn(cos(pi / i), flag * sin(pi / i)); + for(int j = 0; j < n; j += i << 1) + { + complex w(1, 0); + for(int k = 0; k < i; ++k, w = w * wn) + { + complex t = w * a[j + k + i]; + a[j + k + i] = a[j + k] - t; + a[j + k] = a[j + k] + t; + } + } + } + if(flag == -1) + for(int i = 0; i < n; ++i) + a[i].r /= n; +} +int t, n, len, a[maxm], b[maxm]; +LL f[maxm], ans; +int main() +{ + scanf("%d", &t); + while(t--) + { + scanf("%d", &n); + for(len = 1; len < n << 1; len <<= 1); + for(int i = 0; i < n; ++i) + scanf("%d", a + i); + std::reverse(a, a + n); + memset(a + n, 0, (len - n) * sizeof(int)); + for(int i = 0; i < n; ++i) + scanf("%d", b + i); + memcpy(b + n, b, n * sizeof(int)); + memset(b + (n << 1), 0, (len - (n << 1)) * sizeof(int)); + // large + for(int i = 0; i < len; ++i) + X[i] = complex(a[i] >> 10, b[i] >> 10); + FFT(X, len, 1); + for(int i = 0; i < len; ++i) + { + int j = (len - i) & (len - 1); + Y[i] = (X[i] * X[i] - (X[j] * X[j]).conj()) * complex(0, -0.25); + } + FFT(Y, len, -1); + for(int i = 0, j = n - 1; i < n; ++i, ++j) + f[i] = (LL)(Y[j].r + 0.5) << 20; + // small + for(int i = 0; i < len; ++i) + X[i] = complex(a[i] & ((1 << 10) - 1), b[i] & ((1 << 10) - 1)); + FFT(X, len, 1); + for(int i = 0; i < len; ++i) + { + int j = (len - i) & (len - 1); + Y[i] = (X[i] * X[i] - (X[j] * X[j]).conj()) * complex(0, -0.25); + } + FFT(Y, len, -1); + for(int i = 0, j = n - 1; i < n; ++i, ++j) + f[i] += (LL)(Y[j].r + 0.5); + // middle 1 + for(int i = 0; i < len; ++i) + X[i] = complex(a[i] >> 10, b[i] & ((1 << 10) - 1)); + FFT(X, len, 1); + for(int i = 0; i < len; ++i) + { + int j = (len - i) & (len - 1); + Y[i] = (X[i] * X[i] - (X[j] * X[j]).conj()) * complex(0, -0.25); + } + FFT(Y, len, -1); + for(int i = 0, j = n - 1; i < n; ++i, ++j) + f[i] += (LL)(Y[j].r + 0.5) << 10; + // middle 2 + for(int i = 0; i < len; ++i) + X[i] = complex(a[i] & ((1 << 10) - 1), b[i] >> 10); + FFT(X, len, 1); + for(int i = 0; i < len; ++i) + { + int j = (len - i) & (len - 1); + Y[i] = (X[i] * X[i] - (X[j] * X[j]).conj()) * complex(0, -0.25); + } + FFT(Y, len, -1); + ans = 0; + for(int i = 0, j = n - 1; i < n; ++i, ++j) + { + f[i] += (LL)(Y[j].r + 0.5) << 10; + if(ans < f[i]) + ans = f[i]; + } + // calc + ans = -ans << 1; + for(int i = 0; i < n; ++i) + ans += (LL)a[i] * a[i] + (LL)b[i] * b[i]; + printf("%lld\n", ans); + } + return 0; +} diff --git a/Summer_Training/2016-10-02/B - Inspection - liuzy.cpp b/Summer_Training/2016-10-02/B - Inspection - liuzy.cpp new file mode 100644 index 00000000..72307cad --- /dev/null +++ b/Summer_Training/2016-10-02/B - Inspection - liuzy.cpp @@ -0,0 +1,78 @@ +#include +#include +#include +#include +#include + +using namespace std; + +const int NMAX = 110000, EMAX = 110000, VMAX = 110000; + +struct Edge +{ + int y; + int next; +}; + +int N, M; +int V[VMAX], eid, indegree[NMAX]; +Edge E[EMAX]; +deque Q; + +void insert(int x, int y) +{ + E[++eid] = (Edge){y, V[x]}; + V[x] = eid; + indegree[y] += 1; +} + +bool findCycle() +{ + int i, x, left = N; + for(i = 1;i <= N;i += 1) + { + if(!indegree[i]) + { + Q.push_back(i); + left -= 1; + } + } + while(!Q.empty()) + { + x = Q.front(); + Q.pop_front(); + for(i = V[x];i;i = E[i].next) + { + indegree[E[i].y] -= 1; + if(!indegree[E[i].y]) + { + left -= 1; + Q.push_back(E[i].y); + } + } + } + return left > 0; +} + +int main() +{ + int i, x, y; + scanf("%d %d", &N, &M); + for(i = 1;i <= M;i += 1) + { + scanf("%d %d", &x, &y); + insert(x, y); + } + if(N <= 1) + printf("-1\n"); + else if(!M) + printf("2\n"); + else + { + if(findCycle()) + printf("0\n"); + else + printf("1\n"); + } + exit(0); +} diff --git a/Summer_Training/2016-10-02/C - Karmon Go - lzy.cpp b/Summer_Training/2016-10-02/C - Karmon Go - lzy.cpp new file mode 100644 index 00000000..4648ca69 --- /dev/null +++ b/Summer_Training/2016-10-02/C - Karmon Go - lzy.cpp @@ -0,0 +1,17 @@ +#include + +typedef long long i64; + +int k0, k1; +double cc; +i64 a, b; + +int main() { + scanf("%*s%d%*s%lf%d", &k0, &cc, &k1); + a = k0 * (i64)(cc * 100000 + .5); + b = k1 * 100000LL; + if (a == b) puts("Whatever"); + else if (a < b) puts("Evolve, Power up"); + else puts("Power up, Evolve"); + return 0; +} diff --git a/Summer_Training/2016-10-02/E - Autocomplete - lzy.cpp b/Summer_Training/2016-10-02/E - Autocomplete - lzy.cpp new file mode 100644 index 00000000..67ed7a2a --- /dev/null +++ b/Summer_Training/2016-10-02/E - Autocomplete - lzy.cpp @@ -0,0 +1,70 @@ +#include +#include +#include + +#include +#include + +using namespace std; + +#define N 1000 +#define L 2000 + +typedef unsigned long long u64; + +int k, n, m, l; +int len[N + 9]; +char q[L + 9]; +int o[N + 9]; +u64 h[N + 9]; +bitset d[N + 9], c; + +#define MAGIC 23333 +u64 Hash(const char s[]) { + u64 h = 0; + while (*s) h = h * MAGIC + (tolower(*s++) - 'a'); + return h; +} + +//~ bool eql(const char s[], const char t[]) { + //~ auto n = strlen(s); + //~ if (n != strlen(t)) return false; + //~ for (auto i = 0u; i < n; ++i) + //~ if (tolower(s[i]) != tolower(t[i])) return false; + //~ return true; +//~ } + +bool cmp(int a, int b) { + return h[a] < h[b]; +} + +int main() { + scanf("%d", &k); + scanf("%d", &n); + for (int i = 0; i < n; ++i) { + scanf("%s", q); + len[i] = strlen(q); + h[i] = Hash(q); + for (int j = 0; j < len[i]; ++j) + if (isupper(q[j])) d[i].set(j); + } + for (int i = 0; i <= n; ++i) o[i] = i; + sort(o, o + n, cmp); + scanf("%d", &m); + while (m--) { + scanf("%s", q); + l = strlen(q); + h[n] = Hash(q); + c.reset(); + for (int j = 0; j < l; ++j) + if (isupper(q[j])) c.set(j); + int ans = 0; + auto rg = equal_range(o, o + n, o[n], cmp); + for (auto i = rg.first; i < rg.second; ++i) { + //if (!eql(dict[i], q)) continue; + ans += (int)(d[*i] ^ c).count() <= k; + } + printf("%d\n", ans); + } + return 0; +} diff --git a/Summer_Training/2016-10-02/F - Amazing Divisibility - lzy.cpp b/Summer_Training/2016-10-02/F - Amazing Divisibility - lzy.cpp new file mode 100644 index 00000000..b3fe3a10 --- /dev/null +++ b/Summer_Training/2016-10-02/F - Amazing Divisibility - lzy.cpp @@ -0,0 +1,50 @@ +#include +#include + +#define N 100000 + +typedef long long i64; + +int n, a[N + 9]; +int c[11][7]; +i64 ans; + +int calc(int x) { + i64 n = 1; + int m = 0; + while (n <= x) n *= 10, ++m; + //printf("%d %d %d\n", m, (7 - x % 7) % 7, c[m][(7 - x % 7) % 7]); + return c[m][(7 - x % 7) % 7]; +} + +int main() { + scanf("%d", &n); + for (int i = 0; i < n; ++i) + scanf("%d", a + i); + memset(c, 0, sizeof c); + for (int i = 0; i < n; ++i) { + ans += calc(a[i]); + + //printf("%lld\n", ans); + ++c[0][a[i] % 7]; + i64 d = 1; + for (int j = 1; j <= 10; ++j) { + d = d * 10 % 7; + ++c[j][a[i] * d % 7]; + } + + } + memset(c, 0, sizeof c); + for (int i = n - 1; i >= 0; --i) { + ans += calc(a[i]); + //printf("%lld\n", ans); + ++c[0][a[i] % 7]; + i64 d = 1; + for (int j = 1; j <= 10; ++j) { + d = d * 10 % 7; + ++c[j][a[i] * d % 7]; + } + } + printf("%lld\n", ans); + return 0; +} diff --git a/Summer_Training/2016-10-02/G - Group Tournament - lzy.cpp b/Summer_Training/2016-10-02/G - Group Tournament - lzy.cpp new file mode 100644 index 00000000..b2ba2c96 --- /dev/null +++ b/Summer_Training/2016-10-02/G - Group Tournament - lzy.cpp @@ -0,0 +1,113 @@ +#include + +#include + +using namespace std; + +#define N 100 + + +struct Edge {int v, w; Edge *x, *inv;} *lnk[2 * N * N + 9], *e, E[64 * N * N + 9]; + +const int inf = 0x3fffffff; + +int n, num, S, T, L, R; +char str[N + 9][N + 9]; +Edge *fuck[N + 9][N + 9]; +Edge *pre[2 * N * N + 9]; +int fat[2 * N * N + 9]; +int l, r, que[2 * N * N + 9]; + +void add(int u, int v, int w) { + *e = (Edge) {v, w, lnk[u], 0}; + lnk[u] = e++; +} + +void arc(int u, int v, int w) { + add(u, v, w); + add(v, u, 0); + lnk[u]->inv = lnk[v]; + lnk[v]->inv = lnk[u]; +} + +bool bfs(int S, int T) { + for (int i = 1; i <= T; ++i) pre[i] = 0, fat[i] = 0; + fat[que[(r = 0)++] = S] = -1; + for (l = 0; l < r; ++l) { + int u = que[l]; + //printf("u: %d\n", u); + for (Edge *p = lnk[u]; p; p = p->x) { + //printf("%d - %d %d\n", u, p->v, p->w); + if (!fat[p->v] && p->w) { + //printf("%d - %d %d\n", u, p->v, p->w); + pre[que[r++] = p->v] = p; + fat[p->v] = u; + } + } + if (pre[T]) return true; + } + return false; +} + +int main() { + scanf("%d", &n); + L = R = n + 1; + e = E; + for (int i = 1; i <= n; ++i) { + scanf("%s", str[i] + 1); + for (int j = 1; j < i; ++j) { + if (str[i][j] == '.') { + arc(R, i, 3); + arc(R, j, 3); + } else if (str[i][j] == 'l') { + arc(R, i, 1); + arc(R, j, 2); + } else if (str[i][j] == 'L') { + arc(R, i, 0); + arc(R, j, 3); + } else if (str[i][j] == 'w') { + arc(R, i, 2); + arc(R, j, 1); + } else if (str[i][j] == 'W') { + arc(R, i, 3); + arc(R, j, 0); + } + fuck[i][j] = lnk[R++]; + } + } + S = R, T = S + 1; + for (int i = L; i < R; ++i) + arc(S, i, 3); + for (int i = 1; i <= n; ++i) { + int c; + scanf("%d", &c); + arc(i, T, c); + } + int det; + while (bfs(S, T)) { + det = inf; + for (Edge *p = pre[T]; p; p = pre[fat[p->v]]) { + //printf("%d %d\n", fat[p->v], p->v); + det = min(det, p->w); + } + //printf("det: %d\n", det); + for (Edge *p = pre[T]; p; p = pre[fat[p->v]]) { + //printf("%p %p\n", p, p->inv); + p->w -= det; + p->inv->w += det; + } + } + for (int i = 1; i <= n; ++i) { + for (int j = 1; j < i; ++j) { + //printf("R: %d %d %d %d\n", R, i, j, fuck[i][j]->w); + if (str[i][j] == '.') { + if (fuck[i][j]->w == 3) str[j][i] = 'L', str[i][j] = 'W'; + if (fuck[i][j]->w == 2) str[j][i] = 'l', str[i][j] = 'w'; + if (fuck[i][j]->w == 1) str[j][i] = 'w', str[i][j] = 'l'; + if (fuck[i][j]->w == 0) str[j][i] = 'W', str[i][j] = 'L'; + } + } + } + for (int i = 1; i <= n; ++i) puts(str[i] + 1); + return 0; +} diff --git a/Summer_Training/2016-10-02/J - Carts - tjz.cpp b/Summer_Training/2016-10-02/J - Carts - tjz.cpp new file mode 100644 index 00000000..6504ed61 --- /dev/null +++ b/Summer_Training/2016-10-02/J - Carts - tjz.cpp @@ -0,0 +1,36 @@ +#include +#include +using namespace std; +typedef long long LL; +const int maxn = 100001; +int n, x[maxn], y[maxn]; +LL ans[2]; +int main() +{ + int px, py; + scanf("%d", &n); + for(int i = 0; i < n; ++i) + scanf("%d%d", x + i, y + i); + sort(x, x + n); + sort(y, y + n); + px = x[n >> 1]; + py = y[n >> 1]; + for(int i = 0; i < n; ++i) + { + ans[0] += abs(x[i] - px); + ans[1] += abs(y[i] - py); + x[i] -= i; + y[i] -= i; + } + sort(x, x + n); + sort(y, y + n); + px = x[n >> 1]; + py = y[n >> 1]; + for(int i = 0; i < n; ++i) + { + ans[1] += abs(x[i] - px); + ans[0] += abs(y[i] - py); + } + printf("%lld\n", min(ans[0], ans[1])); + return 0; +} diff --git a/Summer_Training/2016-10-02/upsolved/I - Guess The Modulo - tjz.cpp b/Summer_Training/2016-10-02/upsolved/I - Guess The Modulo - tjz.cpp new file mode 100644 index 00000000..3c9f615f --- /dev/null +++ b/Summer_Training/2016-10-02/upsolved/I - Guess The Modulo - tjz.cpp @@ -0,0 +1,138 @@ +#include +#include +#include +#include +#include +using namespace std; +const int maxn = 101, maxv = 1000000000, maxs = 32000; +int n, a[maxn], idx, low = 1, mx; +long long sum; +void insert(int x) +{ + sum -= a[idx]; + a[idx] = x; + sum += a[idx++]; + if(idx == n) + idx = 0; +} +int main() +{ + // typedef long long LL; + // unsigned seed = time(0); + // printf("seed : %d\n", seed); + // srand(seed); + // int M = ((LL)rand() << 15 | rand()) % (maxv - 2 + 1) + 2; + // printf("modulo : %d\n", M); + int x, y; + // n = rand() % (100 - 2 + 1) + 1; printf("n : %d\n", n); + scanf("%d", &n); + // printf("seq :"); + for(int i = 0; i < n - 1; ++i) + { + //x = ((LL)rand() << 15 | rand()) % (maxv + 1); printf(" %d", x); + scanf("%d", &x); + insert(x); + } + // putchar('\n'); + { // first + int y1, y2; + insert(0); + printf("? %d\n", 0); + fflush(stdout); + //y1 = sum % M; printf("ret : %d\n", y1); + scanf("%d", &y1); + int tmp = (y1 << 1) - a[idx]; + insert(y1); + low = max(low, y1); + tmp -= a[idx]; + if(tmp < 0) + x = min(-tmp - 1, maxv); + else + x = max(maxv - tmp, 0); + tmp += x; + insert(x); + printf("? %d\n", x); + fflush(stdout); + //y2 = sum % M; printf("ret : %d\n", y2); + scanf("%d", &y2); + insert(y2); + low = max(low, y2); + if(tmp < 0) + mx = y2 - tmp; + else + mx = tmp - y2; + y = y2; + } + if(mx > maxv) // first divide + { + int lim = (int)ceil(sqrt(mx)); + static int tot = 0, prime[maxs]; + static bool vis[maxs] = {}; + for(int i = 2; i <= lim; ++i) + { + if(!vis[i]) + prime[tot++] = i; + for(int j = 0; j < tot && i * prime[j] <= lim; ++j) + { + vis[i * prime[j]] = 1; + if(i % prime[j] == 0) + break; + } + } + int tmp = mx; + for(int i = 0; i < tot && prime[i] * prime[i] <= tmp; ++i) + if(tmp % prime[i] == 0) + { + int temp = mx / prime[i]; + if(temp > low) + { + int x = temp - (sum - a[idx]) % temp; + insert(x); + printf("? %d\n", x); + fflush(stdout); + //y = sum % M; printf("ret : %d\n", y); + scanf("%d", &y); + if(!y) + mx /= prime[i]; + else + low = max(low, y); + insert(y); + } + for(tmp /= prime[i]; tmp % prime[i] == 0; tmp /= prime[i]); + } + if(tmp > 1) + { + int temp = mx / tmp; + if(temp > low) + { + int x = temp - (sum - a[idx]) % temp; + insert(x); + printf("? %d\n", x); + fflush(stdout); + //y = sum % M; printf("ret : %d\n", y); + scanf("%d", &y); + if(!y) + mx /= tmp; + else + low = max(low, y); + insert(y); + } + } + } + while(low + 1 < mx) // still divide + { + int temp = mx, x = temp - 1 - (sum - a[idx]) % temp; + insert(x); + printf("? %d\n", x); + fflush(stdout); + //y = sum % M; printf("ret : %d\n", y); + scanf("%d", &y); + if(y < temp - 1) + mx = __gcd(mx, (int)abs(temp - 1 - y)); + low = max(low, y); + insert(y); + } + printf("! %d\n", mx); + //puts(mx == M ? "YES" : "NO"); + return 0; +} diff --git a/Summer_Training/2016-10-04/A - Nearest Neighbor Search - lzy.cpp b/Summer_Training/2016-10-04/A - Nearest Neighbor Search - lzy.cpp new file mode 100644 index 00000000..5faf3b2d --- /dev/null +++ b/Summer_Training/2016-10-04/A - Nearest Neighbor Search - lzy.cpp @@ -0,0 +1,83 @@ +#include +#include + +using std::min; + +const int inf = 0x7fffffff; + +int x0, y0, z0; +int x1, y1, z1; +int x2, y2, z2; + +int ans = inf; + +bool x, y, z; + +int sqr(int x) { + return x * x; +} + +int main() { + scanf("%d%d%d", &x0, &y0, &z0); + scanf("%d%d%d", &x1, &y1, &z1); + scanf("%d%d%d", &x2, &y2, &z2); + x = x1 <= x0 && x0 <= x2; + y = y1 <= y0 && y0 <= y2; + z = z1 <= z0 && z0 <= z2; + if (x && y && z) { + puts("0"); + return 0; + } + if (x && y) { + ans = min(sqr(z0 - z1), sqr(z0 - z2)); + printf("%d\n", ans); + return 0; + } + if (x && z) { + ans = min(sqr(y0 - y1), sqr(y0 - y2)); + printf("%d\n", ans); + return 0; + } + if (y && z) { + ans = min(sqr(x0 - x1), sqr(x0 - x2)); + printf("%d\n", ans); + return 0; + } + if (x) { + ans = min(ans, sqr(y0 - y1) + sqr(z0 - z1)); + ans = min(ans, sqr(y0 - y1) + sqr(z0 - z2)); + ans = min(ans, sqr(y0 - y2) + sqr(z0 - z1)); + ans = min(ans, sqr(y0 - y2) + sqr(z0 - z2)); + printf("%d\n", ans); + return 0; + } + if (y) { + ans = min(ans, sqr(x0 - x1) + sqr(z0 - z1)); + ans = min(ans, sqr(x0 - x1) + sqr(z0 - z2)); + ans = min(ans, sqr(x0 - x2) + sqr(z0 - z1)); + ans = min(ans, sqr(x0 - x2) + sqr(z0 - z2)); + printf("%d\n", ans); + return 0; + } + if (z) { + ans = min(ans, sqr(y0 - y1) + sqr(x0 - x1)); + ans = min(ans, sqr(y0 - y1) + sqr(x0 - x2)); + ans = min(ans, sqr(y0 - y2) + sqr(x0 - x1)); + ans = min(ans, sqr(y0 - y2) + sqr(x0 - x2)); + printf("%d\n", ans); + return 0; + } + if (!x && !y && !z) { + ans = min(ans, sqr(x0 - x1) + sqr(y0 - y1) + sqr(z0 - z1)); + ans = min(ans, sqr(x0 - x1) + sqr(y0 - y1) + sqr(z0 - z2)); + ans = min(ans, sqr(x0 - x1) + sqr(y0 - y2) + sqr(z0 - z1)); + ans = min(ans, sqr(x0 - x1) + sqr(y0 - y2) + sqr(z0 - z2)); + ans = min(ans, sqr(x0 - x2) + sqr(y0 - y1) + sqr(z0 - z1)); + ans = min(ans, sqr(x0 - x2) + sqr(y0 - y1) + sqr(z0 - z2)); + ans = min(ans, sqr(x0 - x2) + sqr(y0 - y2) + sqr(z0 - z1)); + ans = min(ans, sqr(x0 - x2) + sqr(y0 - y2) + sqr(z0 - z2)); + printf("%d\n", ans); + return 0; + } + return 0; +} diff --git a/Summer_Training/2016-10-04/E - Coins - liuzy.cpp b/Summer_Training/2016-10-04/E - Coins - liuzy.cpp new file mode 100644 index 00000000..cc4c8f46 --- /dev/null +++ b/Summer_Training/2016-10-04/E - Coins - liuzy.cpp @@ -0,0 +1,35 @@ +#include +#include +#include +#include + +using namespace std; + +long long a1, a2, a3, ans; + +int main() +{ + scanf("%lld %lld %lld", &a1, &a2, &a3); + if(a1) + { + if(a1 + 2 * a2 >= 2) + ans = a1 + 2 * a2 + 3 * a3; + else + ans = 2 * a3 + 1; + } + else + { + if(a3) + { + if(a2 >= 2) + ans = 2 * (a2 - 1) + 3 * a3; + else if(a2) + ans = 2 * a3 + 1; + else + ans = a3; + } + else + ans = a2; + } + printf("%lld\n", ans); +} diff --git a/Summer_Training/2016-10-04/F - Floyd-Warshall - lzy.cpp b/Summer_Training/2016-10-04/F - Floyd-Warshall - lzy.cpp new file mode 100644 index 00000000..351f2219 --- /dev/null +++ b/Summer_Training/2016-10-04/F - Floyd-Warshall - lzy.cpp @@ -0,0 +1,131 @@ +#include +#include +#include + +#define N 100000 +#define M 100200 + +using namespace std; + +const int inf = 0x3fffffff; + +struct Edge {int v; Edge *x;} *lnk[N + 9], *e, E[2 * M + 9]; + +int n, m, q, u, v; +int d[N + 9]; +int f[19][N + 9]; +bool vis[N + 9]; +int tot, key[N + 9], s[209][N + 9]; +int l, r, que[N + 9]; +pair g[M + 9]; + +inline +void add(int u, int v) { + *e = (Edge){v, lnk[u]}; + lnk[u] = e++; +} + +void dfs(int fa, int u) { + vis[u] = true; + f[0][u] = fa; + for (int i = 1; i < 19; ++i) f[i][u] = f[i - 1][f[i - 1][u]]; + for (Edge *p = lnk[u]; p; p = p->x) { + if (p->v == fa) continue; + if (vis[p->v]) { + key[tot++] = u; + key[tot++] = p->v; + } + else { + d[p->v] = d[u] + 1; + dfs(u, p->v); + } + } +} + +void bfs(int u, int s[]) { + for (int i = 1; i <= n; ++i) vis[i] = false; + s[u] = 0; + vis[que[(r = 0)++] = u] = true; + for (l = 0; l < r; ++l) { + u = que[l]; + for (Edge *p = lnk[u]; p; p = p->x) + if (!vis[p->v]) { + s[p->v] = s[u] + 1; + vis[que[r++] = p->v] = true; + } + } +} + +inline +int lca(int u, int v) { + if (d[u] > d[v]) swap(u, v); + int det = d[v] - d[u]; + for (int i = 0; i < 19; ++i) + if (det & 1 << i) v = f[i][v]; + for (int i = 18; i >= 0; --i) { + if (f[i][u] != f[i][v]) { + u = f[i][u]; + v = f[i][v]; + } + } + if (u != v) u = f[0][u]; + return u; +} + +inline +int alice(int u, int v) { + int a = lca(u, v); + //printf("lca: %d %d %d\n", u, v, a); + //printf("lca: %d %d %d\n", d[u], d[v], d[a]); + return d[u] - d[a] + d[v] - d[a]; +} + +inline +int bob(int u, int v) { + int ret = inf; + for (int i = 0; i < tot; ++i) + ret = min(ret, s[i][u] + s[i][v]); + return ret; +} + +//~ void get(int &x) { + //~ int c; + //~ while (!isdigit(c = getchar())); + //~ x = c - '0'; + //~ while (isdigit(c = getchar())) x = x * 10 + c - '0'; +//~ } + +int main() { + //get(n); + //get(m); + //get(q); + scanf("%d%d%d", &n, &m, &q); + while (m--) { + //get(u), get(v); + scanf("%d%d", &u, &v); + if (u != v) { + if (u > v) swap(u, v); + g[tot++] = make_pair(u, v); + } + } + tot = unique(g, g + tot) - g; + e = E; + for (int i = 0; i < tot; ++i) { + u = g[i].first, v = g[i].second; + add(u, v); + add(v, u); + } + tot = 0; + + dfs(0, 1); + sort(key, key + tot); + tot = unique(key, key + tot) - key; + for (int i = 0; i < tot; ++i) + bfs(key[i], s[i]); + while (q--) { + //get(u), get(v); + scanf("%d%d", &u, &v); + printf("%d\n", min(alice(u, v), bob(u, v))); + } + return 0; +} diff --git a/Summer_Training/2016-10-04/G - Road History - tjz.cpp b/Summer_Training/2016-10-04/G - Road History - tjz.cpp new file mode 100644 index 00000000..a7b2d0d8 --- /dev/null +++ b/Summer_Training/2016-10-04/G - Road History - tjz.cpp @@ -0,0 +1,70 @@ +#include +#include +typedef long long LL; +const int maxn = 100001; +int n, m, dsu[maxn], dis[maxn], cnt[maxn][2]; +bool vis[maxn]; +LL ans; +int find(int x) +{ + if(dsu[x] < 0) + return x; + int tmp = dsu[x]; + dsu[x] = find(dsu[x]); + dis[x] ^= dis[tmp]; + return dsu[x]; +} +int main() +{ + scanf("%d%d", &n, &m); + for(int i = 1; i <= n; ++i) + { + dsu[i] = -1; + dis[i] = 0; + cnt[i][0] = 1; + } + while(m--) + { + int u, v; + scanf("%d%d", &u, &v); + int x = find(u), y = find(v); + if(x != y) + { + ans -= vis[x] ? cnt[x][0] * (cnt[x][0] - 1LL) >> 1 : (LL)cnt[x][0] * cnt[x][1]; + ans -= vis[y] ? cnt[y][0] * (cnt[y][0] - 1LL) >> 1 : (LL)cnt[y][0] * cnt[y][1]; + if(dsu[x] > dsu[y]) + { + std::swap(x, y); + std::swap(u, v); + } + if(dsu[x] == dsu[y]) + --dsu[x]; + dsu[y] = x; + vis[x] |= vis[y]; + if(!vis[x]) + { + dis[y] = dis[u] ^ dis[v] ^ 1; + cnt[x][0] += cnt[y][dis[y]]; + cnt[x][1] += cnt[y][dis[y] ^ 1]; + cnt[y][0] = cnt[y][1] = 0; + } + else + { + int tmp = cnt[x][0] + cnt[x][1] + cnt[y][0] + cnt[y][1]; + cnt[x][0] = tmp; + cnt[x][1] = 0; + } + ans += vis[x] ? cnt[x][0] * (cnt[x][0] - 1LL) >> 1 : (LL)cnt[x][0] * cnt[x][1]; + } + else if(!vis[x] && dis[u] == dis[v]) + { + ans -= (LL)cnt[x][0] * cnt[x][1]; + cnt[x][0] += cnt[x][1]; + cnt[x][1] = 0; + vis[x] = 1; + ans += cnt[x][0] * (cnt[x][0] - 1LL) >> 1; + } + printf("%lld\n", ans); + } + return 0; +} diff --git a/Summer_Training/2016-10-04/H - Around the World - tjz.cpp b/Summer_Training/2016-10-04/H - Around the World - tjz.cpp new file mode 100644 index 00000000..db8d9ae5 --- /dev/null +++ b/Summer_Training/2016-10-04/H - Around the World - tjz.cpp @@ -0,0 +1,31 @@ +#include +typedef long long LL; +const int maxn = 100001, maxm = 1000001, mod = 1000000007; +int fact[maxm << 1], iact[maxm]; +int n, deg[maxn], ans; +int main() +{ + fact[0] = 1; + for(int i = 1; i < maxm << 1; ++i) + fact[i] = (LL)fact[i - 1] * i % mod; + iact[1] = 1; + for(int i = 2; i < maxm; ++i) + iact[i] = mod - mod / i * (LL)iact[mod % i] % mod; + iact[0] = 1; + for(int i = 1; i < maxm; ++i) + iact[i] = (LL)iact[i - 1] * iact[i] % mod; + scanf("%d", &n); + ans = 1; + for(int i = 1; i < n; ++i) + { + int u, v, w; + scanf("%d%d%d", &u, &v, &w); + deg[u] += w; + deg[v] += w; + ans = (LL)ans * fact[w << 1] % mod * iact[w] % mod * iact[w] % mod * w % mod; + } + for(int i = 1; i <= n; ++i) + ans = (LL)ans * fact[deg[i] - (i > 1)] % mod; + printf("%d\n", ans); + return 0; +} diff --git a/Summer_Training/2016-10-04/I - Longest Increasing Subsequence - tjz.cpp b/Summer_Training/2016-10-04/I - Longest Increasing Subsequence - tjz.cpp new file mode 100644 index 00000000..49f2126a --- /dev/null +++ b/Summer_Training/2016-10-04/I - Longest Increasing Subsequence - tjz.cpp @@ -0,0 +1,64 @@ +#include +#include +#include +const int maxn = 6, maxm = 1001; +int n, L[maxn], R[maxn], idx[maxn], op[maxn], seq[maxn], f[maxn]; +bool vis[maxn]; +long long g[maxn][maxm], h[maxn][maxm], ans[maxn]; +void dfs(int cur, int dep) +{ + if(cur > n) + { + int len = 1; + for(int i = 1; i <= n; ++i) + { + f[i] = 1; + for(int j = 0; j < i; ++j) + if(seq[j] < seq[i] && f[i] < f[j] + 1) + f[i] = f[j] + 1; + if(len < f[i]) + len = f[i]; + } + g[0][0] = h[0][0] = 1; + for(int i = 1; i <= n; ++i) + { + int pid = idx[i - 1], cid = idx[i], diff = op[i]; + if(diff) + for(int j = L[cid]; j <= R[cid]; ++j) + g[i][j] = L[pid] < j ? h[i - 1][std::min(j - 1, R[pid])] : 0; + else + for(int j = L[cid]; j <= R[cid]; ++j) + g[i][j] = L[pid] <= j && j <= R[pid] ? g[i - 1][j] : 0; + for(int j = L[cid]; j <= R[cid]; ++j) + h[i][j] = (j > L[cid] ? h[i][j - 1] : 0) + g[i][j]; + } + ans[len] += h[n][R[idx[n]]]; + return; + } + for(int i = 1; i <= n; ++i) + if(!vis[i]) + { + vis[i] = 1; + idx[cur] = i; + op[cur] = 1; + seq[i] = dep; + dfs(cur + 1, dep + 1); + if(cur > 1 && idx[cur - 1] < i) + { + op[cur] = 0; + seq[i] = dep - 1; + dfs(cur + 1, dep); + } + vis[i] = 0; + } +} +int main() +{ + scanf("%d", &n); + for(int i = 1; i <= n; ++i) + scanf("%d%d", L + i, R + i); + dfs(1, 1); + for(int i = 1; i <= n; ++i) + printf("%lld%c", ans[i], " \n"[i == n]); + return 0; +} diff --git a/Summer_Training/2016-10-04/J - Matrix Transformation - tjz.cpp b/Summer_Training/2016-10-04/J - Matrix Transformation - tjz.cpp new file mode 100644 index 00000000..a257cebc --- /dev/null +++ b/Summer_Training/2016-10-04/J - Matrix Transformation - tjz.cpp @@ -0,0 +1,75 @@ +#include +const int maxn = 202; +int n, q, L[maxn * maxn], R[maxn * maxn], U[maxn * maxn], D[maxn * maxn]; +inline int idx(int x, int y) +{ + return x * (n + 2) + y; +} +int main() +{ + scanf("%d%d", &n, &q); + for(int i = 0; i <= n + 1; ++i) + for(int j = 0; j <= n + 1; ++j) + { + L[idx(i, j)] = j ? idx(i, j - 1) : 0; + R[idx(i, j)] = j <= n ? idx(i, j + 1) : 0; + U[idx(i, j)] = i ? idx(i - 1, j) : 0; + D[idx(i, j)] = i <= n ? idx(i + 1, j) : 0; + } + while(q--) + { + int t, l, r, d; + scanf("%d%d%d%d", &t, &l, &r, &d); + if(!d) + continue; + ++l, ++r; + if(t == 1) + { + int pL = idx(l, 0), pR = idx(l, n + 1), x = pL; + for(int i = 1; i <= d; ++i, x = R[x]); + int y = R[x]; + for(int i = l; i <= r; ++i, pL = D[pL], pR = D[pR], x = D[x], y = D[y]) + { + int s = R[pL], t = L[pR]; + R[pL] = y; + L[y] = pL; + R[x] = pR; + L[pR] = x; + L[s] = t; + R[t] = s; + } + for(int i = 1, u = R[idx(l - 1, 0)], v = R[idx(l, 0)]; i <= n; ++i, u = R[u], v = R[v]) + U[v] = u, D[u] = v; + for(int i = 1, u = R[idx(r, 0)], v = R[idx(r + 1, 0)]; i <= n; ++i, u = R[u], v = R[v]) + U[v] = u, D[u] = v; + } + else + { + int pU = idx(0, l), pD = idx(n + 1, l), x = pU; + for(int i = 1; i <= d; ++i, x = D[x]); + int y = D[x]; + for(int i = l; i <= r; ++i, pU = R[pU], pD = R[pD], x = R[x], y = R[y]) + { + int s = D[pU], t = U[pD]; + D[pU] = y; + U[y] = pU; + D[x] = pD; + U[pD] = x; + U[s] = t; + D[t] = s; + } + for(int i = 1, u = D[idx(0, l - 1)], v = D[idx(0, l)]; i <= n; ++i, u = D[u], v = D[v]) + L[v] = u, R[u] = v; + for(int i = 1, u = D[idx(0, r)], v = D[idx(0, r + 1)]; i <= n; ++i, u = D[u], v = D[v]) + L[v] = u, R[u] = v; + } + } + for(int i = 1; i <= n; ++i) + for(int j = 1, u = idx(i, 0); j <= n; ++j) + { + u = R[u]; + int x = u / (n + 2) - 1, y = u % (n + 2) - 1; + printf("%d%c", x * n + y, " \n"[j == n]); + } + return 0; +} diff --git a/Summer_Training/2016-10-04/upsolved/B - Odd Discount - tjz.cpp b/Summer_Training/2016-10-04/upsolved/B - Odd Discount - tjz.cpp new file mode 100644 index 00000000..ef1babb4 --- /dev/null +++ b/Summer_Training/2016-10-04/upsolved/B - Odd Discount - tjz.cpp @@ -0,0 +1,42 @@ +#include +#include +typedef long long LL; +const int maxn = 20, maxm = 100001, mod = 1000000007; +int n, m, f[2][1 << 20][2], cur, pre = 1, ans; +inline void mod_inc(int &x, int y) +{ + if((x += y) >= mod) + x -= y; +} +int main() +{ + scanf("%d%d", &n, &m); + for(int i = 1; i <= m; ++i) + { + int val, sz, x, msk = 0; + scanf("%d%d", &val, &sz); + while(sz--) + { + scanf("%d", &x); + msk |= 1 << (--x); + } + f[cur][msk][0] += val; + } + for(int i = 0; i < n; ++i) + { + pre ^= 1; + cur ^= 1; + memset(f[cur], 0, (1 << n + 1) * sizeof(int)); + for(int msk = 0; msk < 1 << n; ++msk) + { + mod_inc(f[cur][msk & ~(1 << i)][0], f[pre][msk][0]); + mod_inc(f[cur][msk & ~(1 << i)][1], f[pre][msk][1]); + mod_inc(f[cur][msk | (1 << i)][0], f[pre][msk][(msk >> i) & 1]); + mod_inc(f[cur][msk | (1 << i)][1], f[pre][msk][~(msk >> i) & 1]); + } + } + for(int i = 1; i < 1 << n; ++i) + ans = (ans + (LL)f[cur][i][1] * f[cur][i][1]) % mod; + printf("%d\n", ans); + return 0; +} diff --git a/Summer_Training/2016-10-04/upsolved/D - Longest Common Subsequence - tjz.cpp b/Summer_Training/2016-10-04/upsolved/D - Longest Common Subsequence - tjz.cpp new file mode 100644 index 00000000..8c4c28ad --- /dev/null +++ b/Summer_Training/2016-10-04/upsolved/D - Longest Common Subsequence - tjz.cpp @@ -0,0 +1,57 @@ +#include +#include +const int maxn = 3001; +int n, m, p, a[maxn], f[2][maxn], cur, pre = 1; +int tim[maxn], cnt[maxn], lim[maxn], tot, L[maxn], R[maxn]; +int main() +{ + scanf("%d%d%d", &n, &m, &p); + for(int i = 1; i <= n; ++i) + scanf("%d", a + i); + while(m--) + { + int len, x; + scanf("%d", &len); + memset(tim + 1, 0, p * sizeof(int)); + memset(lim + 1, 0, p * sizeof(int)); + for(int i = 0; i < len; ++i) + { + scanf("%d", &x); + ++lim[x]; + } + cur ^= 1; + pre ^= 1; + memcpy(f[cur] + 1, f[pre] + 1, n * sizeof(int)); + L[0] = R[0] = 0; + for(int i = 1; i <= n; ++i) + { + L[i] = 0; + R[i] = R[0]; + L[R[0]] = i; + R[0] = i; + for(int j = R[0], w = 0; j; j = R[j]) + { + if(tim[a[j]] != i) + { + tim[a[j]] = i; + cnt[a[j]] = 0; + } + if(cnt[a[j]] < lim[a[j]]) + { + ++cnt[a[j]]; + if(f[cur][i] < f[pre][j - 1] + (++w)) + f[cur][i] = f[pre][j - 1] + w; + } + else + { + L[R[j]] = L[j]; + R[L[j]] = R[j]; + j = L[j]; + } + } + // printf("%d%c", f[cur][i], " \n"[i == n]); + } + } + printf("%d\n", f[cur][n]); + return 0; +} diff --git a/TDL_training/2016-04-24/upsolved/G - Boxes - tjz.cpp b/TDL_training/2016-04-24/upsolved/G - Boxes - tjz.cpp new file mode 100644 index 00000000..8f3178fd --- /dev/null +++ b/TDL_training/2016-04-24/upsolved/G - Boxes - tjz.cpp @@ -0,0 +1,105 @@ +#include +using namespace std; +typedef long long LL; +typedef double DB; +typedef std::complex Complex; +const int maxn = 100001, maxm = 1 << 17 | 1, mod = (int)1e9 + 33, sp = 15, msk = (1 << sp) - 1; +const DB pi = acos(-1.0); +Complex w[maxm], A[maxm], B[maxm], C[maxm], D[maxm]; +int t, n, inv[maxn], f[maxn], g[maxn]; +int maxLen, a[maxm], b[maxm]; +void FFT(int n, Complex a[], int flag) { + static int bitLen = 0, bitRev[maxm] = {}; + if(n != (1 << bitLen)) { + for(bitLen = 0; 1 << bitLen < n; ++bitLen); + for(int i = 1; i < n; ++i) + bitRev[i] = (bitRev[i >> 1] >> 1) | ((i & 1) << (bitLen - 1)); + } + for(int i = 0; i < n; ++i) + if(i < bitRev[i]) + std::swap(a[i], a[bitRev[i]]); + for(int i = 1, d = 1; d < n; ++i, d <<= 1) + for(int j = 0; j < n; j += d << 1) + for(int k = 0; k < d; ++k) { + Complex &AL = a[j + k], &AH = a[j + k + d], TP = w[k << (maxLen - i)] * AH; + AH = AL - TP; + AL = AL + TP; + } + if(flag != -1) + return; + std::reverse(a + 1, a + n); + Complex tr((DB)1 / n, 0); + for(int i = 0; i < n; ++i) + a[i] *= tr; +} +void conv(int len, int a[], int b[]) { // a := a * b + for(int i = 0; i < len; ++i) { + A[i] = Complex(a[i] & msk, a[i] >> sp); + B[i] = Complex(b[i] & msk, b[i] >> sp); + } + FFT(len, A, 1); + FFT(len, B, 1); + Complex trL(0.5, 0), trH(0, -0.5), tr(0, 1); + for(int i = 0; i < len; ++i) { + int j = (len - i) & (len - 1); + Complex AL = (A[i] + std::conj(A[j])) * trL; + Complex AH = (A[i] - std::conj(A[j])) * trH; + Complex BL = (B[i] + std::conj(B[j])) * trL; + Complex BH = (B[i] - std::conj(B[j])) * trH; + C[i] = AL * (BL + BH * tr); + D[i] = AH * (BL + BH * tr); + } + FFT(len, C, -1); + FFT(len, D, -1); + for(int i = 0; i < len; ++i) { + int v11 = (LL)(C[i].real() + 0.5) % mod, v12 = (LL)(C[i].imag() + 0.5) % mod; + int v21 = (LL)(D[i].real() + 0.5) % mod, v22 = (LL)(D[i].imag() + 0.5) % mod; + a[i] = (((((LL)v22 << sp) + v12 + v21) << sp) + v11) % mod; + } +} +inline void fill(int aLen, int a[], int bLen, int b[]) { // assert(bLen <= aLen); + memcpy(a, b, bLen * sizeof(int)); + memset(a + bLen, 0, (aLen - bLen) * sizeof(int)); +} +void solve(int L, int R) { + if(L == R) { + g[L] = L ? (LL)g[L] * inv[L] % mod : 1; + return; + } + int M = (L + R) >> 1; + solve(L, M); + int len; + for(len = 1; len < R - L; len <<= 1); + fill(len, a, M - L + 1, g + L); + fill(len, b, R - L, f); + conv(len, a, b); + for(int i = M + 1, *_a = a - L - 1; i <= R; ++i) + if((g[i] += _a[i]) >= mod) + g[i] -= mod; + solve(M + 1, R); +} +int main() { + int k; + scanf("%d%d", &n, &k); + inv[1] = f[0] = 1; + for(int i = 2; i <= n; ++i) { + inv[i] = mod - (int)(mod / i * (LL)inv[mod % i] % mod); + f[i - 1] = 1; + } + while(k--) { + int x; + scanf("%d", &x); + f[x - 1] = 0; + } + for(maxLen = 1; 1 << maxLen <= n; ++maxLen); + for(int i = 0, ilim = 1 << maxLen; i < ilim; ++i) { + int j = i, k = ilim >> 1; // 2 pi / ilim + for( ; !(j & 1) && !(k & 1); j >>= 1, k >>= 1); + w[i] = std::complex(cos(pi / k * j), sin(pi / k * j)); + } + solve(0, n); + for(int i = 2; i <= n; ++i) + g[n] = (LL)g[n] * i % mod; + printf("%d\n", g[n]); + return 0; +} diff --git a/self_training/2017-04-30/4502A.cpp b/self_training/2017-04-30/4502A.cpp new file mode 100644 index 00000000..6626291f --- /dev/null +++ b/self_training/2017-04-30/4502A.cpp @@ -0,0 +1,17 @@ +#include +const int maxn = 200001; +int n, p[maxn], ans; +int main() +{ + scanf("%d", &n); + for(int i = 1, x; i <= n; ++i) + { + scanf("%d", &x); + p[x] = i; + } + p[0] = n + 1; + for(int i = 1; i <= n; ++i) + ans += p[i - 1] > p[i]; + printf("%d\n", ans); + return 0; +} diff --git a/self_training/2017-04-30/4502B.cpp b/self_training/2017-04-30/4502B.cpp new file mode 100644 index 00000000..c60ca5ab --- /dev/null +++ b/self_training/2017-04-30/4502B.cpp @@ -0,0 +1,56 @@ +#include +#include +#include +#include +#include +using namespace std; +const int maxn = 17, maxm = 101, maxs = 25; +map idx; +int n, sz[maxn], bit[maxn][maxs], m, tot, seq[maxn]; +char nam[maxn][maxm][maxs], str[maxs]; +int main() +{ + scanf("%d", &n); + for(int i = 0; i < n; ++i) + { + scanf("%s%d", str, sz + i); + idx[str] = i; + memset(bit[i], -1, sizeof bit[0]); + for(int j = 0; j < sz[i]; ++j) + { + scanf("%s%s", str, nam[i][j]); + for(int k = 0; str[k]; ++k) + if(str[k] == '.' && bit[i][k] == -1) + bit[i][k] = j; + } + } + scanf("%d", &m); + while(m--) + { + scanf("%d", &tot); + for(int i = 0; i < tot; ++i) + { + scanf("%s", str); + seq[i] = idx[str]; + } + int tim; + for(tim = 0; tim < 24; ++tim) + { + bool chk = 1; + for(int j = 0; j < tot && chk; ++j) + chk &= bit[seq[j]][tim] != -1; + if(chk) + break; + } + if(tim == 24) + puts("No"); + else + { + printf("Yes"); + for(int i = 0; i < tot; ++i) + printf(" %s", nam[seq[i]][bit[seq[i]][tim]]); + putchar('\n'); + } + } + return 0; +} diff --git a/self_training/2017-04-30/4502C.cpp b/self_training/2017-04-30/4502C.cpp new file mode 100644 index 00000000..f6425ed2 --- /dev/null +++ b/self_training/2017-04-30/4502C.cpp @@ -0,0 +1,24 @@ +#include +typedef long long LL; +const int maxn = 51; +int n, a[maxn], e[maxn][maxn], ans1; +LL ans2; +void dfs(int u, int dep) +{ + ans1 += a[u]; + ans2 += (LL)a[u] << dep; + for(int i = 1; i <= e[u][0]; ++i) + dfs(e[u][i], dep + 1); +} +int main() +{ + scanf("%d", &n); + for(int i = 1; i <= n; ++i) + scanf("%d", a + i); + for(int i = 1; i <= n; ++i) + for(int j = 0; j <= e[i][0]; ++j) + scanf("%d", e[i] + j); + dfs(1, 0); + printf("%d %lld\n", ans1, ans2); + return 0; +} diff --git a/self_training/2017-04-30/4502D.cpp b/self_training/2017-04-30/4502D.cpp new file mode 100644 index 00000000..85dbe997 --- /dev/null +++ b/self_training/2017-04-30/4502D.cpp @@ -0,0 +1,44 @@ +#include +#include +using namespace std; +typedef long long LL; +const int maxs = 101; +int t; +LL n; +bool solve(LL val, int cnt) +{ + int rem = __builtin_popcountll(val); + if(rem > cnt) + return 0; + int sz = 0; + static int seq[maxs]; + for(int i = 60; i >= 0; --i) + { + if(!((val >> i) & 1)) + continue; + int lim = (int)min(cnt - sz - rem + 1LL, 1LL << i), dt; + for(dt = 0; (1 << dt) <= lim; ++dt); + --dt; + for(int j = 0; j < (1 << dt); ++j) + seq[sz++] = i - dt; + --rem; + } + if(sz != cnt) + return 0; + printf("%d ", cnt); + for(int i = 0; i < cnt; ++i) + printf("%d%c", seq[i], " \n"[i == cnt - 1]); + return 1; +} +int main() +{ + scanf("%d", &t); + while(t--) + { + scanf("%lld", &n); + for(int i = 1; ; ++i) + if(solve(n * i, i)) + break; + } + return 0; +} diff --git a/self_training/2017-04-30/4502E.cpp b/self_training/2017-04-30/4502E.cpp new file mode 100644 index 00000000..b75d2548 --- /dev/null +++ b/self_training/2017-04-30/4502E.cpp @@ -0,0 +1,41 @@ +#include +typedef long long LL; +const int maxn = 1001, mod = 1000000007; +int n, fact[maxn], iact[maxn], inv2, inv3; +int main() +{ + scanf("%d", &n); + iact[1] = 1; + for(int i = 2; i <= n; ++i) + iact[i] = mod - (int)(mod / i * (LL)iact[mod % i] % mod); + inv2 = iact[2]; + inv3 = iact[3]; + fact[0] = iact[0] = 1; + for(int i = 1; i <= n; ++i) + { + fact[i] = (LL)fact[i - 1] * i % mod; + iact[i] = (LL)iact[i - 1] * iact[i] % mod; + } + int ans = 0; + for(int i = 0; i <= n - 1 - i; ++i) + { + if(i > 1) // && n - 1 - i > 1 + { // one degree is 4 + int ways = (LL)fact[n] * inv2 % mod * inv2 % mod; + if(i == n - 1 - i) + ways = (LL)ways * inv2 % mod; + (ans += ways) >= mod && (ans -= mod); + } + for(int j = i + !i; j <= n - 2 - i - j; ++j) // two degrees are 3 + { + int ways = (LL)fact[n] * inv2 % mod; + if(i == j && j == n - 2 - i - j) + ways = (LL)ways * inv2 % mod * inv3 % mod; + else if(i == j || j == n - 2 - i - j) + ways = (LL)ways * inv2 % mod; + (ans += ways) >= mod && (ans -= mod); + } + } + printf("%d\n", ans); + return 0; +} diff --git a/self_training/2017-04-30/4502F.cpp b/self_training/2017-04-30/4502F.cpp new file mode 100644 index 00000000..e5abff6a --- /dev/null +++ b/self_training/2017-04-30/4502F.cpp @@ -0,0 +1,56 @@ +#include +#include +const int maxn = 1000003; +int n, z[maxn], pL[maxn], pR[maxn], lnk[maxn]; +char str[maxn]; +long long ans; +struct Edge { + int nxt, v; +} e[maxn]; +int main() +{ + scanf("%s", str); + n = strlen(str); + memset(lnk, -1, (n + 1) * sizeof(int)); + int S = n, T = n + 1; + pR[S] = T; + pL[T] = S; + for(int i = 0, L = 0, R = 1, mx = 0; i < n; ++i) + { + if(z[i - L] < R - i) + z[i] = z[i - L]; + else + { + int x = i < R ? R : i; + for( ; str[x] == str[x - i]; ++x); + z[i] = x - i; + if(i < x) + { + L = i; + R = x; + } + } + for(int it = lnk[i]; it != -1; it = e[it].nxt) + { + int id = e[it].v; + pL[pR[id]] = pL[id]; + pR[pL[id]] = pR[id]; + } + int len = mx < z[i] ? mx : z[i]; + if(len > 0) + { + pL[i] = pL[T]; + pR[i] = T; + pR[pL[i]] = i; + pL[pR[i]] = i; + e[i] = (Edge){lnk[i + len], i}; + lnk[i + len] = i; + } + if(pR[S] != T) + ans += i - pR[S] + 1; + if(mx < z[i]) + mx = z[i]; + } + printf("%lld\n", ans); + return 0; +} diff --git a/self_training/2017-08-05/1001.cpp b/self_training/2017-08-05/1001.cpp new file mode 100644 index 00000000..da04d7f8 --- /dev/null +++ b/self_training/2017-08-05/1001.cpp @@ -0,0 +1,96 @@ +#include +#include +#include +using namespace std; +const int maxn = 501; +int n, m, f[maxn][maxn]; +bool g[maxn][maxn], vis[maxn][maxn]; +struct Point { + int x, y; + void read() { + scanf("%d%d", &x, &y); + } + bool operator == (Point const &t) const { + return x == t.x && y == t.y; + } + bool operator < (Point const &t) const { + return x < t.x || (x == t.x && y < t.y); + } + Point operator - (Point const &t) const { + return (Point){x - t.x, y - t.y}; + } + int dot(Point const &t) const { + return x * t.x + y * t.y; + } + int det(Point const &t) const { + return x * t.y - y * t.x; + } +} a[maxn], b[maxn], stk[maxn]; +inline int G(int L, int R) { + if(g[L][R]) + return 1; + if(vis[L][R]) + return f[L][R]; + vis[L][R] = 1; + int &ret = f[L][R]; + ret = maxn; + for(int i = 0; i < m; ++i) + if(i != L && i != R && g[L][i]) + ret = min(ret, G(i, R) + 1); + return ret; +} +int main() { + while(scanf("%d", &n) == 1) { + for(int i = 0; i < n; ++i) + a[i].read(); + sort(a, a + n); + n = unique(a, a + n) - a; + scanf("%d", &m); + int mm = m; + for(int i = 0; i < m; ++i) + b[i].read(); + sort(b, b + m); + m = unique(b, b + m) - b; + if(n == 1) { // check ans = 1 + int i; + for(i = 0; i < m; ++i) + if(a[0] == b[i]) + break; + if(i < m) { + printf("%d\n", mm - 1); + continue; + } + } + { // check ans = 2 + bool chk = 0; + for(int i = 0; i < m && !chk; ++i) + for(int j = i + 1; j < m && !chk; ++j) { + bool tmp = 1; + for(int k = 0; k < n && tmp; ++k) + tmp &= !(b[j] - b[i]).det(a[k] - b[i]) && (b[i] - a[k]).dot(b[j] - a[k]) <= 0; + chk |= tmp; + } + if(chk) { + printf("%d\n", mm - 2); + continue; + } + } + for(int i = 0; i < m; ++i) + for(int j = 0; j < m; ++j) { + g[i][j] = i != j; + for(int k = 0; k < n && g[i][j]; ++k) + g[i][j] &= (b[j] - b[i]).det(a[k] - b[i]) >= 0; + vis[i][j] = 0; + } + int ans = maxn; + for(int i = 0; i < m; ++i) + for(int j = 0; j < m; ++j) + if(g[i][j] && G(j, i) > 1) + ans = min(ans, G(j, i) + 1); + if(ans < maxn) + printf("%d\n", mm - ans); + else + puts("ToT"); + } + return 0; +} diff --git a/self_training/2017-08-05/1002.cpp b/self_training/2017-08-05/1002.cpp new file mode 100644 index 00000000..a5f4b25d --- /dev/null +++ b/self_training/2017-08-05/1002.cpp @@ -0,0 +1,126 @@ +#include +#include +#include +#include +using namespace std; +const int maxn = 3001, maxm = 100001, maxd = 13, INF = 0x3f3f3f3f, maxt = 10; +int n, m, e[maxn][maxn], dsu[maxn], dt[maxn], dep[maxn], fa[maxd][maxn], ans; +struct Edge { + int u, v, w; + bool used; + bool operator < (Edge const &t) const { + return w > t.w; + } +} g[maxm]; +int dsu_find(int x) { + return dsu[x] < 0 ? x : (dsu[x] = dsu_find(dsu[x])); +} +bool dsu_merge(int u, int v) { + u = dsu_find(u); + v = dsu_find(v); + if(u == v) + return 0; + if(dsu[u] < dsu[v]) + swap(u, v); + else if(dsu[u] == dsu[v]) + --dsu[v]; + dsu[u] = v; + return 1; +} +vector > ee[maxn]; +void pfs(int u) { + for(vector >::iterator it = ee[u].begin(); it != ee[u].end(); ++it) { + int v = it -> first; + if(v == fa[0][u]) + continue; + dt[v] = 0; + dep[v] = dep[u] + 1; + fa[0][v] = u; + pfs(v); + } +} +int lca(int u, int v) { + for(int i = 0, j = dep[u] - dep[v]; j > 0; ++i, j >>= 1) + (j & 1) && (u = fa[i][u]); + for(int i = 0, j = dep[v] - dep[u]; j > 0; ++i, j >>= 1) + (j & 1) && (v = fa[i][v]); + if(u == v) + return u; + for(int i = maxd - 1; i >= 0; --i) + if(fa[i][u] != fa[i][v]) { + u = fa[i][u]; + v = fa[i][v]; + } + return fa[0][u]; +} +void dfs(int u) { + for(vector >::iterator it = ee[u].begin(); it != ee[u].end(); ++it) { + int v = it -> first, w = it -> second; + if(v == fa[0][u]) + continue; + dfs(v); + ans = min(ans, w + dt[v]); + dt[u] += dt[v]; + } +} +#include +int perm[maxm]; +int main() { + while(scanf("%d%d", &n, &m) == 2) { + for(int i = 1; i <= n; ++i) + memset(e[i] + 1, 0, n * sizeof(int)); + while(m--) { + int u, v, w; + scanf("%d%d%d", &u, &v, &w); + if(u > v) + swap(u, v); + e[u][v] += w; + } + m = 0; + for(int i = 1; i <= n; ++i) + for(int j = i + 1; j <= n; ++j) + if(e[i][j]) + g[m++] = (Edge){i, j, e[i][j], 0}; + sort(g, g + m); + ans = INF; + for(int i = 0; i < m; ++i) + perm[i] = i; + for(int Case = 1; Case < maxt; ++Case) { + int blk = n; + memset(dsu + 1, -1, n * sizeof(int)); + for(int i = 1; i <= n; ++i) + vector >().swap(ee[i]); + for(int i = 0; i < m; ++i) + if((g[i].used = dsu_merge(g[i].u, g[i].v))) { + ee[g[i].u].push_back(make_pair(g[i].v, g[i].w)); + ee[g[i].v].push_back(make_pair(g[i].u, g[i].w)); + --blk; + // printf("tree %d %d %d\n", g[i].u, g[i].v, g[i].w); + } + if(blk > 1) { + ans = 0; + break; + } + pfs(1); + for(int i = 1; i < maxd; ++i) + for(int j = 1; j <= n; ++j) + fa[i][j] = fa[i - 1][fa[i - 1][j]]; + for(int i = 0; i < m; ++i) + if(!g[i].used) { + // printf("non-tree %d %d %d\n", g[i].u, g[i].v, g[i].w); + int pp = lca(g[i].u, g[i].v); + dt[g[i].u] += g[i].w; + dt[g[i].v] += g[i].w; + dt[pp] -= g[i].w << 1; + } + dfs(1); + for(int i = 0; i < m; ++i) { + swap(perm[i], perm[i + (rand() << 15 | rand()) % (m - i)]); + swap(g[i], g[perm[i]]); + } + } + printf("%d\n", ans); + } + return 0; +} + diff --git a/self_training/2017-08-05/1003.cpp b/self_training/2017-08-05/1003.cpp new file mode 100644 index 00000000..6d15a946 --- /dev/null +++ b/self_training/2017-08-05/1003.cpp @@ -0,0 +1,47 @@ +#include +#include +const int maxn = 100001, maxm = 1001, maxb = 11, INF = 0x3f3f3f3f; +int n, m, a[maxn], b[maxn], c[maxm], w[maxm], sz[maxb], f[maxb][maxm]; +int main() { + while(scanf("%d%d", &n, &m) == 2) { + memset(sz, 0, sizeof sz); + for(int i = 0; i < n; ++i) { + scanf("%d%d", a + i, b + i); + if(sz[b[i]] < a[i]) + sz[b[i]] = a[i]; + } + for(int i = 0; i < m; ++i) + scanf("%d%d", w + i, c + i); + for(int i = 0; i < maxb; ++i) { + if(!sz[i]) + continue; + int *ff = f[i], upp = sz[i]; + memset(ff + 1, 0x3f, sz[i] * sizeof(int)); + for(int j = 0; j < m; ++j) { + if(c[j] <= i) + continue; + int cc = c[j] - i; + for(int k = 0; k <= upp; ++k) { + if(ff[k] == INF) + continue; + int kk = k + cc <= upp ? k + cc : upp, vv = ff[k] + w[j]; + if(vv < ff[kk]) + ff[kk] = vv; + } + for(int k = upp; k > 0; --k) + if(ff[k - 1] > ff[k]) + ff[k - 1] = ff[k]; + } + } + long long ans = 0; + for(int i = 0; i < n; ++i) + if(f[b[i]][a[i]] < INF) { + ans += f[b[i]][a[i]]; + } else { + ans = -1; + break; + } + printf("%I64d\n", ans); + } + return 0; +} diff --git a/self_training/2017-08-05/1004.cpp b/self_training/2017-08-05/1004.cpp new file mode 100644 index 00000000..df63b3c7 --- /dev/null +++ b/self_training/2017-08-05/1004.cpp @@ -0,0 +1,44 @@ +#include +#include +const int maxn = 101, maxm = 1001; +int t, n, m, c[maxn], w[maxn], f[maxn][maxm], g[maxn][maxm]; +bool p[maxn][maxm]; +int main() { + scanf("%d", &t); + for(int Case = 1; Case <= t; ++Case) { + scanf("%d%d", &m, &n); + for(int i = 0; i < n; ++i) + scanf("%d%d", w + i, c + i); + for(int i = 0; i <= m; ++i) + f[n][i] = g[n][i] = 0; + for(int i = n - 1; i >= 0; --i) + for(int j = 0; j <= m; ++j) { + f[i][j] = f[i + 1][j]; + g[i][j] = g[i + 1][j]; + p[i][j] = 0; + if(j >= c[i]) { + int tf = f[i + 1][j - c[i]] + w[i], tg = g[i + 1][j - c[i]] + i + 1; + if(f[i][j] < tf || f[i][j] == tf && g[i][j] >= tg) { + f[i][j] = tf; + g[i][j] = tg; + p[i][j] = 1; + } + } + } + int cc = 0; + for(int i = 0, j = m, s = g[0][m]; i < n; ++i) + if(p[i][j]) { + j -= c[i]; + s -= i + 1; + cc += c[i]; + } + printf("Case #%d:\n%d %d\n", Case, f[0][m], cc); + for(int i = 0, j = m, s = g[0][m]; i < n; ++i) + if(p[i][j]) { + j -= c[i]; + s -= i + 1; + printf("%d%c", i + 1, " \n"[s == 0]); + } + } + return 0; +} diff --git a/self_training/2017-08-05/1005.cpp b/self_training/2017-08-05/1005.cpp new file mode 100644 index 00000000..fec5b2cf --- /dev/null +++ b/self_training/2017-08-05/1005.cpp @@ -0,0 +1,26 @@ +#include +typedef long long LL; +const int DELTA = 2000000, maxs = 100001, mod = (int)1e9 + 7; +const int fct[] = {1, 578095319, 259081142, 316220877, 251368199, 682498929, 95936601, 167332441, 336060741, 626497524, 491101308, 565768255, 968999, 638587686, 19426633, 76479948, 842267748, 485348706, 544075857, 798967520, 723816384, 721996174, 323604647, 398699886, 294587621, 67347853, 196447201, 228338256, 762479457, 811667359, 27368307, 59469516, 766196482, 86609485, 340941507, 625544428, 808412394, 585237443, 361786913, 595143852, 199888908, 579691190, 459188207, 439729802, 899297830, 888050723, 736550105, 85990869, 56305184, 168891766, 927880474, 934814019, 567277637, 44930135, 47401357, 281863274, 692636218, 14235602, 400295761, 421835306, 661224977, 168203998, 544064410, 583967898, 751231582, 623534362, 243276029, 60050552, 395891998, 159554990, 970055531, 487998999, 82104855, 513365505, 1559745, 261384175, 323214113, 444090941, 80729842, 664989237, 195888993, 457882808, 212938473, 827544702, 677711203, 66404266, 195290384, 349551356, 83489485, 52167191, 547665832, 350016754, 296269301, 23015220, 748566338, 109838563, 105574531, 542432219, 325636655, 630621321, 933245637, 238971581, 557301633, 848952161, 925152039, 724691727, 238087006, 910291942, 492237273, 834860913, 368925948, 740594930, 806047532, 172115341, 116261993, 268838846, 243624360, 481661251, 95182730, 115435332, 136026497, 710729672, 366384665, 270239666, 79874094, 112390913, 103056890, 339464594, 108312174, 479334442, 135498044, 591048681, 353339603, 839849206, 736265527, 217544623, 618898635, 380858207, 898554793, 54062950, 419363534, 160398980, 990918946, 450718279, 648991369, 500780548, 39052406, 460373282, 461415770, 643638805, 668123525, 673464765, 199866123, 841799766, 504962686, 128487469, 299172297, 577786541, 773206631, 204355779, 30977140, 363172638, 472935553, 587220627, 793270300, 522049725, 335416359, 472012302, 332139472, 464599136, 309058615, 580204973, 954840109, 895609896, 836813268, 386027524, 724205533, 715247054, 830567832, 68409439, 189239124, 943653305, 811056092, 825132993, 985084650, 148528617, 882148737, 701445829, 302177126, 241107368, 940567523, 325334066, 115312728, 218129285, 180002392, 917084264, 435327356, 421623838, 59883031, 79716267, 429277690, 316486674, 940338439, 940524812, 833550543, 996164327, 697611981, 274192146, 925347821, 893732627, 358655417, 581830879, 347046911, 125354499, 247662371, 568392357, 209244402, 149586983, 309134554, 263053337, 780072518, 990826116, 155569943, 711553356, 451373308, 462639908, 654611901, 41815820, 675258797, 934281721, 275105629, 325912072, 252551589, 554917439, 754363835, 909210595, 719566487, 424989675, 50590510, 198768464, 99199382, 747407118, 497196934, 726997875, 296229203, 703397904, 732868367, 26031303, 216665960, 352946234, 733333339, 459878625, 16634739, 68718097, 404206040, 97830135, 31131935, 864326453, 167831173, 718498895, 608823837, 385388084, 681756977, 313132653, 442795120, 256141983, 636578562, 553033642, 919273670, 326686486, 141827977, 693305776, 186576440, 565456578, 519397500, 696628828, 370732451, 915265013, 923043932, 777901604, 637939935, 490676632, 462528877, 798895521, 699767918, 811575797, 606870929, 179111720, 508038818, 752550134, 848924691, 34629406, 435904287, 208184231, 206330671, 131772368, 648711554, 523696723, 25058942, 817928963, 724464507, 701453022, 281230685, 596949867, 303076380, 272814771, 48824684, 939889684, 48527183, 484458461, 326159309, 668422905, 965656187, 359960756, 407934361, 456152084, 124804049, 920251227, 483924582, 167087470, 903466878, 368114731, 388728584, 249153339, 322908524, 92255682, 15570863, 744158074, 334326205, 98349682, 769795511, 888146074, 582627184, 919419361, 986708498, 373745190, 256853930, 702823274, 308186532, 180350107, 606241871, 273712630, 682189174, 946867127, 502371023, 825871994, 701506133, 341779962, 815463367, 49187570, 957939114, 933376898, 338121343, 869630152, 632627667, 435887178, 129621197, 827722926, 918513230, 547838438, 852304035, 689189630, 567033437, 212842957, 404149413, 663307737, 206282795, 488906585, 280700600, 534279149, 375297772, 922377372, 219932130, 701050258, 863002719, 217598709, 810551911, 247844667, 812283640, 857241854, 624148346, 564827277, 478982047, 454039189, 48562889, 671734977, 508831870, 897162044, 892168027, 277714559, 624500515, 808691930, 895205045, 628829100, 919717789, 748510389, 574455974, 172096141, 581418893, 15391652, 203191898, 880691101, 986598821, 264688936, 785804644, 423951674, 696623384, 161960209, 541120825, 841656666, 629786193, 269571439, 76770272, 421943723, 751040886, 672850561, 493689107, 100228913, 639655136, 929893103, 814362881, 406024012, 10065330, 983737173, 551060742, 823845496, 946421040, 842203531, 894247956, 885362182, 116667533, 241427979, 281804989, 563796647, 774625892, 256473217, 918860977, 753513874, 304644842, 161362528, 627655552, 799289297, 816701166, 787113234, 701220427, 245795606, 10475577, 759404319, 491466901, 114495791, 586445753, 718623833, 66547751, 351472920, 494166907, 172114298, 364380585, 965683742, 117483873, 237120480, 193781724, 107141741, 793307102, 236455213, 14162538, 778983779, 49389215, 859637374, 713258160, 923333660, 83868974, 217298111, 176966527, 105359006, 10430738, 315103615, 708233401, 946149627, 281329488, 31503476, 965785236, 916291845, 899946391, 512634493, 121000338, 492741665, 165393390, 917041303, 741626808, 536841269, 377329025, 859917803, 373451745, 823495900, 73146164, 847549272, 646654592, 205951465, 780882948, 598245292, 698611116}; +inline int mod_fact(int x) { + int id = x / DELTA, y = id * DELTA, ret = fct[id]; + for( ; (++y) <= x; ret = (LL)ret * y % mod); + return ret; +} +inline int mod_inv(int x) { + return x <= 1 ? x : mod - (int)(mod / x * (LL)mod_inv(mod % x) % mod); +} +inline int mod_binom(int n, int m) { + return n < m ? 0 : (LL)mod_fact(n) * mod_inv((LL)mod_fact(m) * mod_fact(n - m) % mod) % mod; +} +int t, n; +char buf[maxs]; +int main() { + scanf("%d", &t); + while(t--) { + scanf("%d%s", &n, buf); + for(int i = 0; buf[i]; ++i, --n); + printf("%d\n", n & 1 ? 0 : (int)((LL)mod_binom(n, n >> 1) * mod_inv((n >> 1) + 1) % mod * (n + 1) % mod)); + } + return 0; +} diff --git a/self_training/2017-08-13/1001.cpp b/self_training/2017-08-13/1001.cpp new file mode 100644 index 00000000..098bdd50 --- /dev/null +++ b/self_training/2017-08-13/1001.cpp @@ -0,0 +1,26 @@ +#include +typedef long long LL; +const int maxn = 1001, mod = (int)1e9 + 7; +int fact[maxn], iact[maxn]; +int main() { + iact[1] = 1; + for(int i = 2; i < maxn; ++i) + iact[i] = mod - (int)(mod / i * (LL)iact[mod % i] % mod); + fact[0] = iact[0] = 1; + for(int i = 1; i < maxn; ++i) { + fact[i] = (LL)fact[i - 1] * i % mod; + iact[i] = (LL)iact[i - 1] * iact[i] % mod; + } + int t, n, m; + scanf("%d", &t); + while(t--) { + scanf("%d%d", &n, &m); + if(n < m) { + n = n + m; + m = n - m; + n = n - m; + } + printf("%d\n", (int)((LL)fact[n] * iact[m] % mod * iact[n - m] % mod)); + } + return 0; +} diff --git a/self_training/2017-08-13/1003.cpp b/self_training/2017-08-13/1003.cpp new file mode 100644 index 00000000..6ab4cd20 --- /dev/null +++ b/self_training/2017-08-13/1003.cpp @@ -0,0 +1,45 @@ +#include +typedef long long LL; +const int maxn = 1001, maxm = 4001, mod = 998244353; +int fact[maxm], iact[maxm], a[4], f[3][maxm]; +void conv(int alen, int a[], int blen, int b[], int f[]) { + for(int i = 0; i < alen + blen - 1; ++i) + f[i] = 0; + for(int i = 0; i < alen; ++i) + for(int j = 0; j < blen; ++j) + f[i + j] = (f[i + j] + (LL)a[i] * b[j]) % mod; +} +int main() { + iact[1] = 1; + for(int i = 2; i < maxm; ++i) + iact[i] = mod - (int)(mod / i * (LL)iact[mod % i] % mod); + fact[0] = iact[0] = 1; + for(int i = 1; i < maxm; ++i) { + fact[i] = (LL)fact[i - 1] * i % mod; + iact[i] = (LL)iact[i - 1] * iact[i] % mod; + } + while(scanf("%d%d%d%d", a + 0, a + 1, a + 2, a + 3) == 4) { + int cur = 0, pre = 1, sz = 1; + f[cur][0] = 1; + for(int i = 0; i < 4; ++i) { + if(!a[i]) + continue; + pre ^= 1; + cur ^= 1; + for(int j = 1; j <= a[i]; ++j) + f[2][j] = (LL)fact[a[i] - 1] * iact[j - 1] % mod * iact[a[i] - j] % mod * iact[j] % mod; + conv(sz, f[pre], a[i] + 1, f[2], f[cur]); + sz += a[i]; + } + int ans = 0; + for(int i = 0; i < sz; ++i) { + f[cur][i] = (LL)f[cur][i] * fact[i] % mod; + if((sz - i) & 1) + (ans += f[cur][i]) >= mod && (ans -= mod); + else + (ans -= f[cur][i]) < 0 && (ans += mod); + } + printf("%d\n", ans); + } + return 0; +} diff --git a/self_training/2017-08-13/1004.cpp b/self_training/2017-08-13/1004.cpp new file mode 100644 index 00000000..2f8a2471 --- /dev/null +++ b/self_training/2017-08-13/1004.cpp @@ -0,0 +1,58 @@ +#include +#include +typedef long long LL; +const int maxn = 500001, mod = (int)1e9 + 7; +int fact[maxn], pw[maxn]; +int n, a[maxn], b[maxn], cnt, ans; +LL m; +struct Node { + int idx; + bool operator < (Node const &t) const { + return a[idx] > a[t.idx]; + } +}; +std::priority_queue Q; +int main() { + fact[0] = pw[0] = 1; + for(int i = 1; i < maxn; ++i) { + fact[i] = (LL)fact[i - 1] * i % mod; + pw[i] = 2LL * pw[i - 1] % mod; + } + while(scanf("%d%I64d", &n, &m) == 2) { + for(int i = 0; i < n; ++i) { + scanf("%d", a + i); + b[i] = pw[i - (i > 0)]; + } + ans = cnt = 0; + m -= a[0]; + for(int i = 1; i < n; ++i) + if(m < 0) { + m -= a[i]; + while(!Q.empty()) { + int id = Q.top().idx; + if(m + a[id] >= 0) + break; + Q.pop(); + (cnt -= b[id]) < 0 && (cnt += mod); + } + ans = (ans + (LL)cnt * fact[n - i]) % mod; + } else { + m -= a[i]; + if(m < 0) { // only once + for( ; !Q.empty(); Q.pop()); + for(int j = 0; j <= i; ++j) + if(m + a[j] >= 0) { + (cnt += b[j]) >= mod && (cnt -= mod); + Q.push((Node){j}); + } + ans = (ans + (LL)cnt * fact[n - i]) % mod; + } else { + ans = (ans + (LL)pw[i] * fact[n - i]) % mod; + } + } + if(m >= 0) + (ans += pw[n - 1]) >= mod && (ans -= mod); + printf("%d\n", ans); + } + return 0; +} diff --git a/self_training/2017-08-13/1005.cpp b/self_training/2017-08-13/1005.cpp new file mode 100644 index 00000000..d2e376c5 --- /dev/null +++ b/self_training/2017-08-13/1005.cpp @@ -0,0 +1,105 @@ +#include +#include +#include +#include +typedef long long LL; +const int maxn = 501, N = 2001, M = 500001; +const LL INF = 0x3f3f3f3f3f3f3f3fLL; +namespace MCMF { + int n, m, s, t, lnk[N], p[N]; + LL flow, cost, d[N], a[N]; + bool inq[N]; + struct Edge { + int nxt, v; + LL w, c; + } e[M << 1 | 1]; + void AddEdge(int u, int v, LL w, LL c) { + e[m] = (Edge){lnk[u], v, w, c}; + lnk[u] = m++; + e[m] = (Edge){lnk[v], u, 0LL, -c}; + lnk[v] = m++; + } + bool BellmanFord() { + for(int i = 0; i < n; ++i) + d[i] = -INF; + memset(inq, 0, n * sizeof(bool)); + d[s] = 0; + inq[s] = 1; + p[s] = -1; + a[s] = INF; + std::queue Q; + Q.push(s); + while(!Q.empty()) { + int u = Q.front(); + Q.pop(); + inq[u] = 0; + for(int it = lnk[u]; it != -1; it = e[it].nxt) { + int v = e[it].v; + if(e[it].w > 0 && d[v] < d[u] + e[it].c) { + d[v] = d[u] + e[it].c; + p[v] = it; + a[v] = std::min(a[u], e[it].w); + if(!inq[v]) { + Q.push(v); + inq[v] = 1; + } + } + } + } + if(d[t] <= 0) + return 0; + flow += a[t]; + cost += d[t] * a[t]; + for(int u = t; u != s; u = e[p[u] ^ 1].v) { + e[p[u]].w -= a[t]; + e[p[u] ^ 1].w += a[t]; + } + return 1; + } + LL solve(int N, LL cL[], LL cR[], LL dis[][maxn]) { + n = (N << 1) + 2; + m = 0; + memset(lnk, -1, n * sizeof(int)); + s = N << 1; + t = N << 1 | 1; + for(int i = 0; i < N; ++i) { + AddEdge(s, i, cL[i], 0); + AddEdge(N + i, t, cR[i], 0); + } + for(int i = 0; i < N; ++i) + for(int j = 0; j < N; ++j) + if(dis[i][j] > 0) + AddEdge(i, N + j, std::min(cL[i], cR[j]), dis[i][j]); + flow = cost = 0; + while(BellmanFord()); + return cost; + } +} +int n, m; +LL a[maxn], b[maxn], c[maxn], d[maxn], dis[maxn][maxn]; +int main() { + while(scanf("%d%d", &n, &m) == 2) { + memset(dis, 0x3f, sizeof dis); + for(int i = 0; i < n; ++i) { + scanf("%I64d%I64d%I64d%I64d", a + i, b + i, c + i, d + i); + dis[i][i] = 0; + } + while(m--) { + int u, v; + LL w; + scanf("%d%d%I64d", &u, &v, &w); + --u, --v; + dis[u][v] = std::min(dis[u][v], w); + dis[v][u] = std::min(dis[v][u], w); + } + for(int k = 0; k < n; ++k) + for(int i = 0; i < n; ++i) + for(int j = 0; j < n; ++j) + dis[i][j] = std::min(dis[i][j], dis[i][k] + dis[k][j]); + for(int i = 0; i < n; ++i) + for(int j = 0; j < n; ++j) + dis[i][j] = c[j] - a[i] - dis[i][j]; + printf("%I64d\n", MCMF::solve(n, b, d, dis)); + } + return 0; +} diff --git a/self_training/2017-08-13/1006.cpp b/self_training/2017-08-13/1006.cpp new file mode 100644 index 00000000..4310d54f --- /dev/null +++ b/self_training/2017-08-13/1006.cpp @@ -0,0 +1,43 @@ +#include +#include +using namespace std; +typedef long long LL; +const int maxn = 100001; +int n, m; +LL tmp, sum, ans; +pair a[maxn]; +bool cmp(pair const &x, pair const &y) { + if(x.first == y.first) + return x.second > y.second; + return x.first < y.first; +} +int main() { + while(scanf("%d%d", &n, &m) == 2) { + for(int i = 0; i < n; ++i) + scanf("%d%d", &a[i].first, &a[i].second); + sort(a, a + n, cmp); + int sz = 0; + for(int i = 0; i < n; ++i) + if(!sz || a[i - 1].first != a[i].first) { + if(sz && a[sz - 1].second >= a[i].first) + a[sz - 1].second = max(a[sz - 1].second, a[i].second); + else + a[sz++] = a[i]; + } + n = sz; + ans = tmp = sum = 0; + for(int i = 0, j = 0; i < n; ++i) { + sum += a[i].second - a[i].first + 1; + if(j < i) + tmp += a[i].first - a[i - 1].second - 1; + for( ; j < i && tmp > m; ++j) { + sum -= a[j].second - a[j].first + 1; + tmp -= a[j + 1].first - a[j].second - 1; + } + if(tmp <= m) + ans = max(ans, sum + m); + } + printf("%I64d\n", ans); + } + return 0; +} diff --git a/self_training/2017-08-27/2A.cpp b/self_training/2017-08-27/2A.cpp new file mode 100644 index 00000000..d6d33820 --- /dev/null +++ b/self_training/2017-08-27/2A.cpp @@ -0,0 +1,74 @@ +#include +typedef long long LL; +const int maxn = 501, s1 = 29, s2 = 425149, m1 = (int)1e9 + 7, m2 = (int)1e9 + 9, maxs = 1 << 18; +int n, m, tot, st, tim[maxs | 1], lnk[maxs | 1]; +struct Pair { + int v1, v2; + Pair operator - (Pair const &t) const { + int r1 = v1 - t.v1, r2 = v2 - t.v2; + return (Pair){r1 < 0 ? r1 + m1 : r1, r2 < 0 ? r2 + m2 : r2}; + } + Pair operator + (Pair const &t) const { + int r1 = v1 + t.v1, r2 = v2 + t.v2; + return (Pair){r1 < m1 ? r1 : r1 - m1, r2 < m2 ? r2 : r2 - m2}; + } + Pair operator + (int const &t) const { + int r1 = v1 + t, r2 = v2 + t; + return (Pair){r1 < m1 ? r1 : r1 - m1, r2 < m2 ? r2 : r2 - m2}; + } + Pair operator * (Pair const &t) const { + return (Pair){(int)((LL)v1 * t.v1 % m1), (int)((LL)v2 * t.v2 % m2)}; + } + Pair operator * (int const &t) const { + return (Pair){(int)((LL)v1 * t % m1), (int)((LL)v2 * t % m2)}; + } +} pw1[maxn], pw2[maxn], h[maxn][maxn]; +struct Edge { + int nxt; + LL val; +} e[maxn * maxn]; +char buf[maxn][maxn + 2]; +bool check(LL val) { + int pos = val & (maxs - 1); + if(tim[pos] != st) { + tim[pos] = st; + lnk[pos] = -1; + } + for(int it = lnk[pos]; it != -1; it = e[it].nxt) + if(e[it].val == val) + return 1; + e[tot] = (Edge){lnk[pos], val}; + lnk[pos] = tot++; + return 0; +} +int main() { + pw1[0] = pw2[0] = (Pair){1, 1}; + for(int i = 1; i < maxn; ++i) { + pw1[i] = pw1[i - 1] * s1; + pw2[i] = pw2[i - 1] * s2; + } + scanf("%d%d", &n, &m); + for(int i = 1; i <= n; ++i) { + scanf("%s", buf[i] + 1); + for(int j = 1; j <= m; ++j) + h[i][j] = h[i][j - 1] * s1 + (buf[i][j] - 'a'); + } + for(int i = 1; i <= n; ++i) + for(int j = 1; j <= m; ++j) + h[i][j] = h[i - 1][j] * s2 + h[i][j]; + for(int len = n < m ? n : m; len > 0; --len) { + tot = 0; + ++st; + Pair sx = pw1[len], sy = pw2[len], sz = sx * sy; + for(int i = len; i <= n; ++i) + for(int j = len; j <= m; ++j) { + Pair val = h[i][j] - h[i][j - len] * sx - h[i - len][j] * sy + h[i - len][j - len] * sz; + if(check((LL)val.v1 << 32 | val.v2)) { + printf("%d\n", len); + return 0; + } + } + } + puts("0"); + return 0; +} diff --git a/self_training/2017-08-27/2B.cpp b/self_training/2017-08-27/2B.cpp new file mode 100644 index 00000000..752d7c32 --- /dev/null +++ b/self_training/2017-08-27/2B.cpp @@ -0,0 +1,22 @@ +#include +#include +#include +typedef long long LL; +const int maxn = 301, mod = (int)1e9 + 7; +int n, m, fact[maxn], iact[maxn], ans; +int main() { + scanf("%d%d", &n, &m); + iact[1] = 1; + for(int i = 2; i <= n || i <= m; ++i) + iact[i] = mod - (int)(mod / i * (LL)iact[mod % i] % mod); + fact[0] = iact[0] = 1; + for(int i = 1; i <= n || i <= m; ++i) { + fact[i] = (LL)fact[i - 1] * i % mod; + iact[i] = (LL)iact[i - 1] * iact[i] % mod; + } + for(int i = 1; i <= n && i <= m; ++i) + ans = (ans + (LL)iact[i - 1] * iact[n - i] % mod * iact[m - i]) % mod; + ans = (LL)ans * fact[n - 1] % mod * fact[m] % mod; + printf("%d\n", ans); + return 0; +} diff --git a/self_training/2017-08-27/2C.cpp b/self_training/2017-08-27/2C.cpp new file mode 100644 index 00000000..863c128b --- /dev/null +++ b/self_training/2017-08-27/2C.cpp @@ -0,0 +1,84 @@ +#include +#include +#include +const int maxn = 100001, maxd = 17; +int len, mod, dt, a[maxn], sa[maxn], rk[maxn], ht[maxn], st[maxn][maxd], Log[maxn]; +int m, f[maxn], g[maxn], c[maxn], ans[maxn]; +std::vector idx[maxn]; +int lcp(int L, int R) { + if(L == R) return maxn; + int k = Log[R - (L++)]; + return std::min(st[L][k], st[R - (1 << k) + 1][k]); +} +void suffix_array() { + int *x = f, *y = g; m = mod; + for(int i = 0; i < m; ++i) c[i] = 0; + for(int i = 0; i < len; ++i) ++c[x[i] = a[i]]; + for(int i = 1; i < m; ++i) c[i] += c[i - 1]; + for(int i = len - 1; i >= 0; --i) sa[--c[x[i]]] = i; + for(int k = 1, p = 0; k <= len; k <<= 1, m = p, p = 0) { + for(int i = len - k; i < len; ++i) y[p++] = i; + for(int i = 0; i < len; ++i) + if(sa[i] >= k) y[p++] = sa[i] - k; + for(int i = 0; i < m; ++i) c[i] = 0; + for(int i = 0; i < len; ++i) ++c[x[y[i]]]; + for(int i = 0; i < m; ++i) c[i] += c[i - 1]; + for(int i = len - 1; i >= 0; --i) + sa[--c[x[y[i]]]] = y[i]; + std::swap(x, y), p = 1, x[sa[0]] = 0; + for(int i = 1; i < len; ++i) + x[sa[i]] = y[sa[i - 1]] == y[sa[i]] + && y[sa[i - 1] + k] == y[sa[i] + k] ? p - 1 : p++; + if(p >= len) break; + } + for(int i = 0; i < len; ++i) rk[sa[i]] = i; + for(int i = 0, j, k = 0; i < len; ++i) { + if(k) --k; + if(!rk[i]) continue; + for(j = sa[rk[i] - 1]; a[i + k] == a[j + k]; ++k); + ht[rk[i]] = k; + } + for(int i = 2; i <= len; ++i) Log[i] = Log[i >> 1] + 1; + for(int i = 0; i < len; ++i) st[i][0] = ht[i]; + for(int d = 1; 1 << d <= len; ++d) + for(int i = 0; i + (1 << (d - 1)) < len; ++i) + st[i][d] = std::min(st[i][d - 1], st[i + (1 << (d - 1))][d - 1]); +} +bool cmp(int u, int v, int add) { + int com = rk[u] < rk[v] ? lcp(rk[u], rk[v]) : lcp(rk[v], rk[u]); + return com < len && (a[(u + com) % len] + add) % mod < (a[(v + com) % len] + add) % mod; +} +int main() { + scanf("%d%d%d", &len, &mod, &dt); + for(int i = 0; i < len; ++i) { + scanf("%d", a + i); + idx[a[i]].push_back(i); + } + for(int i = 0; i < len; ++i) + a[len + i] = a[i]; + len <<= 1; + suffix_array(); + len >>= 1; + for(int i = 0; i < mod; ++i) + ans[i] = -1; + for(int i = 0; i < mod; ++i) { + if(!idx[i].size()) + continue; + int add = i ? mod - i : 0, low = idx[i][0]; + for(int j = 1, sz = (int)idx[i].size(); j < sz; ++j) + if(cmp(idx[i][j], low, add)) + low = idx[i][j]; + ans[add] = a[(low + dt - 1) % len]; + } + for(int i = 1; i < mod; ++i) + if(ans[i] == -1) + ans[i] = ans[i - 1]; + if(ans[0] == -1) + ans[0] = ans[mod - 1]; + for(int i = 1; i < mod; ++i) + if(ans[i] == -1) + ans[i] = ans[i - 1]; + for(int i = 0; i < mod; ++i) + printf("%d\n", (ans[i] + i) % mod); + return 0; +} diff --git a/self_training/2017-09-15/A.cpp b/self_training/2017-09-15/A.cpp new file mode 100644 index 00000000..4f01c218 --- /dev/null +++ b/self_training/2017-09-15/A.cpp @@ -0,0 +1,18 @@ +#include +using namespace std; +int t, Case, n; +map f; +inline int F(int x) { + if(f.count(x)) + return f[x]; + return f[x] = max(x & 1 ? F(x * 3 + 1) : F(x >> 1), x); +} +int main() { + f[1] = 1; + scanf("%d", &t); + while(t--) { + scanf("%d%d", &Case, &n); + printf("%d %d\n", Case, F(n)); + } + return 0; +} diff --git a/self_training/2017-09-15/B.cpp b/self_training/2017-09-15/B.cpp new file mode 100644 index 00000000..3cdd44f9 --- /dev/null +++ b/self_training/2017-09-15/B.cpp @@ -0,0 +1,17 @@ +#include +using namespace std; +const int maxn = 10000001; +int t, Case, mod, ans; +char buf[maxn]; +int main() { + scanf("%d", &t); + while(t--) { + scanf("%d%d%s", &Case, &mod, buf); + --mod; + ans = 0; + for(int i = 0; buf[i]; ++i) + (ans += buf[i] - '0') >= mod && (ans -= mod); + printf("%d %d\n", Case, ans); + } + return 0; +} diff --git a/self_training/2017-09-15/C.cpp b/self_training/2017-09-15/C.cpp new file mode 100644 index 00000000..560ecc75 --- /dev/null +++ b/self_training/2017-09-15/C.cpp @@ -0,0 +1,11 @@ +#include +using namespace std; +int t, Case, n; +int main() { + scanf("%d", &t); + while(t--) { + scanf("%d%d", &Case, &n); + printf("%d %d\n", Case, (int)floor((n * (n - 3) + 20) / 24.0 + ((n << 1) - 3) * (n & 1 ? 1 : -1) / 16.0)); + } + return 0; +} diff --git a/self_training/2017-09-15/D.cpp b/self_training/2017-09-15/D.cpp new file mode 100644 index 00000000..58b8bf05 --- /dev/null +++ b/self_training/2017-09-15/D.cpp @@ -0,0 +1,32 @@ +#include +using namespace std; +const int maxn = 1001; +int t, Case, n, cur, nxt = 1; +double pL, pM, pR, f[2][maxn][maxn << 1 | 1]; +int main() { + scanf("%d", &t); + while(t--) { + scanf("%d%d%lf%lf", &Case, &n, &pL, &pR); + pM = 1 - pL - pR; + f[cur][0][0] = 1; + for(int i = 0; i < n; ++i, cur ^= 1, nxt ^= 1) { + for(int j = 0; j <= i + 1; ++j) + memset(f[nxt][j], 0, (i - j + 2) * sizeof(double)); + for(int j = 0; j <= i; ++j) + for(int k = 0; j + k <= i; ++k) { + double tmp = f[cur][j][k]; + f[nxt][j][k + 1] += tmp * pL; + f[nxt][j][k] += tmp * pM; + (k ? f[nxt][j][k - 1] : f[nxt][j + 1][k]) += tmp * pR; + } + } + double ans = 0, sum = 0; + for(int i = n; i >= 1; --i) { + for(int j = 0; i + j <= n; ++j) + sum += f[cur][i][j]; + ans += sum; + } + printf("%d %.4f\n", Case, ans); + } + return 0; +} diff --git a/self_training/2017-09-15/E.py b/self_training/2017-09-15/E.py new file mode 100644 index 00000000..541a93bc --- /dev/null +++ b/self_training/2017-09-15/E.py @@ -0,0 +1,13 @@ +from fractions import * +f = [] +f.append([Fraction(0, 1), Fraction(1, 1)]) +for i in range(1, 401) : + g = [Fraction(0, 1), Fraction(1, 1)] + for j in range(2, i + 2) : + g.append(f[i - 1][j - 1] * Fraction(i, j)) + g[1] -= g[j] + f.append(g) +t = int(input()) +for test in range(t) : + Case, n, m = map(int, input().split()) + print("%d %s" % (Case, f[n][m])) diff --git a/self_training/2017-09-15/F.cpp b/self_training/2017-09-15/F.cpp new file mode 100644 index 00000000..2c3a454a --- /dev/null +++ b/self_training/2017-09-15/F.cpp @@ -0,0 +1,24 @@ +#include +using namespace std; +typedef long long LL; +const int maxn = 21; +int t, Case, n; +LL c[maxn], f[maxn]; +int main() { + c[0] = c[1] = 1; + f[0] = f[1] = 2; + for(int i = 2; i < maxn; ++i) { + for(int j = i - 1; j >= 0; --j) { + f[i] += c[j] * f[j] * f[i - 1 - j] >> 2; + c[j + 1] += c[j]; + } + c[0] = 1; + } + f[1] = 1; + scanf("%d", &t); + while(t--) { + scanf("%d%d", &Case, &n); + printf("%d %lld\n", Case, f[n]); + } + return 0; +} diff --git a/self_training/2017-09-15/G.cpp b/self_training/2017-09-15/G.cpp new file mode 100644 index 00000000..d0229e39 --- /dev/null +++ b/self_training/2017-09-15/G.cpp @@ -0,0 +1,105 @@ +#include +using namespace std; +const int maxn = 41, maxm = 68921; +int tt, Case, n, tr[3][3], s[3], t[3], dis[maxm], que[maxm], L, R; +char pre[maxm], buf[maxm]; +inline int idx(int x, int y, int z) { + return (x * maxn + y) * maxn + z; +} +inline void rdx(int o, int &x, int &y, int &z) { + z = o % maxn; + o /= maxn; + y = o % maxn; + x = o / maxn; +} +int main() { + scanf("%d", &tt); + while(tt--) { + scanf("%d%d", &Case, &n); + for(int i = 0; i < 3; ++i) + scanf("%d%d%d", tr[i] + 0, tr[i] + 1, tr[i] + 2); + printf("%d %d\n", Case, n); + while(n--) { + scanf("%d%d%d%d%d%d%d", &Case, s + 0, s + 1, s + 2, t + 0, t + 1, t + 2); + memset(dis, -1, sizeof dis); + int S = idx(s[0], s[1], s[2]), T = idx(t[0], t[1], t[2]); + L = R = 0; + dis[S] = 0; + que[R++] = S; + while(L < R && dis[T] == -1) { + int udx = que[L++], u[3]; + rdx(udx, u[0], u[1], u[2]); + for(int i = 0; i < 3; ++i) { + if(u[i]) { // forward + int v[3] = {u[0] + tr[i][0], u[1] + tr[i][1], u[2] + tr[i][2]}; + --v[i]; + if(v[0] < maxn && v[1] < maxn && v[2] < maxn) { + int vdx = idx(v[0], v[1], v[2]); + if(dis[vdx] == -1) { + que[R++] = vdx; + pre[vdx] = 'A' + i; + dis[vdx] = dis[udx] + 1; + } + } + } + if(u[0] >= tr[i][0] && u[1] >= tr[i][1] && u[2] >= tr[i][2]) { // backward + int v[3] = {u[0] - tr[i][0], u[1] - tr[i][1], u[2] - tr[i][2]}; + ++v[i]; + if(v[0] < maxn && v[1] < maxn && v[2] < maxn) { + int vdx = idx(v[0], v[1], v[2]); + if(dis[vdx] == -1) { + que[R++] = vdx; + pre[vdx] = 'a' + i; + dis[vdx] = dis[udx] + 1; + } + } + } + } + } + if(dis[T] == -1) + printf("%d NO SOLUTION\n", Case); + else { + for(int i = dis[T], cur = T; i--; ) { + int seq[3]; + rdx(cur, seq[0], seq[1], seq[2]); + buf[i] = pre[cur]; + if(pre[cur] >= 'A' && pre[cur] <= 'C') { + int op = pre[cur] - 'A'; + for(int j = 0; j < 3; ++j) + seq[j] -= tr[op][j]; + ++seq[op]; + } else { + int op = pre[cur] - 'a'; + for(int j = 0; j < 3; ++j) + seq[j] += tr[op][j]; + --seq[op]; + } + cur = idx(seq[0], seq[1], seq[2]); + } + buf[dis[T]] = '\0'; + printf("%d %d %s\n", Case, dis[T], buf); + /* + for(int i = 0; i < dis[T]; ++i) { + if(buf[i] >= 'A' && buf[i] <= 'C') { + int op = buf[i] - 'A'; + assert(s[op] > 0); + --s[op]; + for(int j = 0; j < 3; ++j) + s[j] += tr[op][j]; + } else { + int op = buf[i] - 'a'; + for(int j = 0; j < 3; ++j) { + s[j] -= tr[op][j]; + assert(s[j] >= 0); + } + ++s[op]; + } + } + for(int i = 0; i < 3; ++i) + assert(s[i] == t[i]); + */ + } + } + } + return 0; +} diff --git a/self_training/2017-09-15/H.cpp b/self_training/2017-09-15/H.cpp new file mode 100644 index 00000000..e31af2a0 --- /dev/null +++ b/self_training/2017-09-15/H.cpp @@ -0,0 +1,53 @@ +#include +using namespace std; +const int maxn = 21; +const double pi = acos(-1.0), eps = 1e-9; +int t, Case, n, m, f[maxn]; +double x[maxn], y[maxn], cur; +inline int sgn(double x) { + return (x > eps) - (x < -eps); +} +int main() { + scanf("%d", &t); + while(t--) { + scanf("%d%d%d%d%lf", &Case, &n, &m, f + 0, &cur); + cur = cur / 180 * pi; + while(sgn(cur) <= 0) + cur += pi; + for(int i = 1; i <= n; ++i) { + int idx; + scanf("%d", &idx); + scanf("%lf%lf", x + idx, y + idx); + } + for(int i = 0; i < m; ++i) { // f[i] -> f[i + 1] + int p1, p2; + double ang1 = pi * 2, ang2 = pi * 2; + for(int j = 1; j <= n; ++j) { + if(f[i] == j) + continue; + double tmp = atan2(y[j] - y[f[i]], x[j] - x[f[i]]); + while(sgn(tmp) <= 0) // (-pi, pi] -> (0, pi] + tmp += pi; + if(sgn(tmp - cur) > 0 && sgn(tmp - ang1) < 0) { + ang1 = tmp; + p1 = j; + } + if(sgn(tmp - ang2) < 0) { + ang2 = tmp; + p2 = j; + } + } + if(sgn(ang1 - pi) <= 0) { + cur = ang1; + f[i + 1] = p1; + } else { + cur = ang2; + f[i + 1] = p2; + } + } + printf("%d ", Case); + for(int i = 1; i <= m; ++i) + printf("%d%c", f[i], " \n"[i == m]); + } + return 0; +} diff --git a/self_training/2017-09-15/J.cpp b/self_training/2017-09-15/J.cpp new file mode 100644 index 00000000..891c8acb --- /dev/null +++ b/self_training/2017-09-15/J.cpp @@ -0,0 +1,23 @@ +#include +using namespace std; +const int maxn = 101, maxs = 601; +int t, Case, len, n; +char buf[maxn], str[maxs]; +int main() { + scanf("%d", &t); + while(t--) { + scanf("%d", &Case); + while(getchar() != '\n'); + gets(buf); + len = strlen(buf); + scanf("%d", &n); + for(int i = 0, j = 0, dt; i < n; ++i) { + scanf("%d", &dt); + j = (j + dt + len) % len; + str[i] = buf[j]; + } + str[n] = '\0'; + printf("%d %s\n", Case, str); + } + return 0; +} diff --git a/self_training/2018-01-17/7491A.cpp b/self_training/2018-01-17/7491A.cpp new file mode 100644 index 00000000..34d8a2bc --- /dev/null +++ b/self_training/2018-01-17/7491A.cpp @@ -0,0 +1,23 @@ +#include +using namespace std; +typedef unsigned UL; +int main() { + UL mask = 0; + for(int i = 0; i < 10; ++i) { + int x; + scanf("%d", &x); + mask |= 1UL << (x - 1); + } + int n; + scanf("%d", &n); + while(n--) { + int cnt = 0; + for(int i = 0; i < 6; ++i) { + int x; + scanf("%d", &x); + cnt += (mask >> (x - 1)) & 1; + } + puts(cnt >= 3 ? "Lucky" : "Unlucky"); + } + return 0; +} diff --git a/self_training/2018-01-17/7491B.cpp b/self_training/2018-01-17/7491B.cpp new file mode 100644 index 00000000..f24a7739 --- /dev/null +++ b/self_training/2018-01-17/7491B.cpp @@ -0,0 +1,26 @@ +#include +using namespace std; +const int maxn = 101; +int n, seq[maxn << 1 | 1][maxn], idx[maxn * maxn], mat[maxn][maxn]; +int main() { + scanf("%d", &n); + for(int i = 1; i <= n << 1; ++i) { + for(int j = 1; j <= n; ++j) + scanf("%d", seq[i] + j); + if(idx[seq[i][1]]) { + for(int j = 1; j <= n; ++j) + mat[1][j] = seq[i][j]; + } else { + idx[seq[i][1]] = i; + } + } + for(int i = 1; i <= n; ++i) { + int j = idx[mat[1][i]]; + for(int k = 2; k <= n; ++k) + mat[k][i] = seq[j][k]; + } + for(int i = 1; i <= n; ++i) + for(int j = 1; j <= n; ++j) + printf("%d%c", mat[i][j], " \n"[i == n && j == n]); + return 0; +} diff --git a/self_training/2018-01-17/7491C.cpp b/self_training/2018-01-17/7491C.cpp new file mode 100644 index 00000000..64a84330 --- /dev/null +++ b/self_training/2018-01-17/7491C.cpp @@ -0,0 +1,12 @@ +#include +using namespace std; +typedef long long LL; +const int mod = (int)1e9 + 7; +int main() { + int n, k, ans = 1; + scanf("%d", &n); + for(k = (n - 1) * (n - 1); k > 0; k >>= 1, n = (LL)n * n % mod) + (k & 1) && (ans = (LL)ans * n % mod); + printf("%d\n", ans); + return 0; +} diff --git a/self_training/2018-01-17/7491D.cpp b/self_training/2018-01-17/7491D.cpp new file mode 100644 index 00000000..f3fdadbd --- /dev/null +++ b/self_training/2018-01-17/7491D.cpp @@ -0,0 +1,34 @@ +#include +using namespace std; +typedef long long LL; +typedef pair Node; +const int maxn = (int)3e5 + 1, maxm = 101; +const LL INF = (LL)2e18; +int n, q, tot; +LL a[maxn]; +Node b[maxm]; +int main() { + scanf("%d%d", &n, &q); + for(int i = 1; i <= n; ++i) + scanf("%lld", a + i); + while(q--) { + int L, R; + scanf("%d%d", &L, &R); + if(R - L + 1 < 3) { + puts("-1"); + continue; + } + tot = 0; + for(int i = L; i <= R && tot < maxm; ++i) + b[tot++] = (Node){a[i], i}; + sort(b, b + tot); + for( ; tot >= 3; --tot) + if(b[tot - 1].first < b[tot - 2].first + b[tot - 3].first) { + printf("%d %d %d\n", b[tot - 1].second, b[tot - 2].second, b[tot - 3].second); + break; + } + if(tot < 3) + puts("-1"); + } + return 0; +} diff --git a/self_training/2018-01-17/7491E.cpp b/self_training/2018-01-17/7491E.cpp new file mode 100644 index 00000000..18a91279 --- /dev/null +++ b/self_training/2018-01-17/7491E.cpp @@ -0,0 +1,53 @@ +#include +using namespace std; +typedef long long LL; +const int maxn = (int)1e5 + 1; +int n, m, seg[maxn << 1 | 1]; +LL a[maxn], b[maxn]; +LL read() { + LL ret = 0; + static char buf[11]; + scanf("%s", buf); + for(char *ptr = buf; *ptr; ++ptr) + ret = ret << 5 | (*ptr - 'a' + 1); + return ret; +} +inline int seg_idx(int L, int R) { + return (L + R) | (L < R); +} +void seg_upd(int L, int R, int x, int v) { + if(L == R) { + seg[seg_idx(L, R)] += v; + return; + } + int M = (L + R) >> 1; + if(x <= M) + seg_upd(L, M, x, v); + else + seg_upd(M + 1, R, x, v); + seg[seg_idx(L, R)] = min(seg[seg_idx(L, M)], seg[seg_idx(M + 1, R)]); +} +int main() { + scanf("%d%d", &n, &m); + for(int i = 1; i <= n; ++i) + a[i] = read(); + for(int i = 1; i <= m; ++i) + b[i] = read(); + sort(b + 1, b + m + 1); + m = unique(b + 1, b + m + 1) - b - 1; + LL ans = 0; + for(int i = 1, j = 1; i <= n; ++i) { + for( ; j <= n && seg[seg_idx(1, m)] < 1; ++j) { + int k = lower_bound(b + 1, b + m + 1, a[j]) - b; + a[j] = b[k] == a[j] ? k : 0; + if(a[j]) + seg_upd(1, m, a[j], 1); + } + if(seg[seg_idx(1, m)] >= 1) + ans += n + 2 - j; + if(a[i]) + seg_upd(1, m, a[i], -1); + } + printf("%lld\n", ans); + return 0; +} diff --git a/self_training/2018-01-17/7491F.cpp b/self_training/2018-01-17/7491F.cpp new file mode 100644 index 00000000..580a28ab --- /dev/null +++ b/self_training/2018-01-17/7491F.cpp @@ -0,0 +1,109 @@ +#include +using namespace std; +const int maxn = (int)2e5 + 3, maxd = 19; +int n, mx, seq[maxn], in[maxn], out[maxn], dep[maxn], fa[maxd][maxn]; +vector e[maxn]; +int lca(int u, int v) { + for(int i = 0, j = dep[u] - dep[v]; j > 0; ++i, j >>= 1) + (j & 1) && (u = fa[i][u]); + for(int i = 0, j = dep[v] - dep[u]; j > 0; ++i, j >>= 1) + (j & 1) && (v = fa[i][v]); + if(u == v) + return u; + for(int i = mx - 1; i >= 0; --i) + if(fa[i][u] != fa[i][v]) { + u = fa[i][u]; + v = fa[i][v]; + } + return fa[0][u]; +} +struct Diameter { + int u, v, d; + Diameter() {} + Diameter(int u) : u(u), v(u), d(0) {} + Diameter(int u, int v) : u(u), v(v) { + d = dep[u] + dep[v] - (dep[lca(u, v)] << 1); + } + bool operator < (Diameter const &t) const { + return d < t.d; + } + Diameter operator + (int const &t) const { + return max(*this, max(Diameter(u, t), Diameter(v, t))); + } + Diameter operator + (Diameter const &t) const { + return max(*this, max(t, max(Diameter(u, t.u), max(Diameter(u, t.v), max(Diameter(v, t.u), Diameter(v, t.v)))))); + } +} pre[maxn], suf[maxn], out1, out2; +void pfs(int u) { + seq[++mx] = u; + in[u] = mx; + for(vector::iterator it = e[u].begin(); it != e[u].end(); ++it) { + int v = *it; + if(v == fa[0][u]) + continue; + dep[v] = dep[u] + 1; + fa[0][v] = u; + pfs(v); + } + out[u] = mx; +} +int ans; +Diameter dfs(int u) { + Diameter ret = u, tmp; + for(vector::iterator it = e[u].begin(); it != e[u].end(); ++it) { + int v = *it; + if(v == fa[0][u]) + continue; + ret = ret + dfs(v); + } + if(in[u] > 1) { + tmp = pre[in[u] - 1]; + if(out[u] < n) + tmp = tmp + suf[out[u] + 1]; + int dis = (max(ret.d, tmp.d) + 1) >> 1; + if(dis < ans) { + ans = dis; + out1 = ret; + out2 = tmp; + } + } else if(out[u] < n) { + tmp = suf[out[u] + 1]; + int dis = (max(ret.d, tmp.d) + 1) >> 1; + if(dis < ans) { + ans = dis; + out1 = ret; + out2 = tmp; + } + } + return ret; +} +int getMid(Diameter dia) { + int u = dep[dia.u] < dep[dia.v] ? dia.v : dia.u; + for(int i = 0, j = dia.d >> 1; j > 0; ++i, j >>= 1) + (j & 1) && (u = fa[i][u]); + return u; +} +int main() { + scanf("%d", &n); + for(int i = 1; i < n; ++i) { + int u, v; + scanf("%d%d", &u, &v); + e[u].push_back(v); + e[v].push_back(u); + } + pfs(1); + for(mx = 0; 1 << mx <= n; ++mx); + for(int i = 1; i < mx; ++i) + for(int j = 1; j <= n; ++j) + fa[i][j] = fa[i - 1][fa[i - 1][j]]; + pre[1] = seq[1]; + for(int i = 2; i <= n; ++i) + pre[i] = pre[i - 1] + seq[i]; + suf[n] = seq[n]; + for(int i = n - 1; i >= 1; --i) + suf[i] = suf[i + 1] + seq[i]; + ans = n; + dfs(1); + printf("%d %d\n", getMid(out1), getMid(out2)); + return 0; +} diff --git a/self_training/2018-02-24/agc21A.cpp b/self_training/2018-02-24/agc21A.cpp new file mode 100644 index 00000000..7d12f5a3 --- /dev/null +++ b/self_training/2018-02-24/agc21A.cpp @@ -0,0 +1,17 @@ +#include +using namespace std; +typedef long long LL; +int main() { + LL n; + scanf("%lld", &n); + char buf[21]; + int len = sprintf(buf, "%lld", n + 1), tp = 0, ans = 0; + for(int i = 0; i < len; ++i) { + buf[i] -= '0'; + if(buf[i]) + ans = max(ans, tp + buf[i] - 1 + (len - 1 - i) * 9); + tp += buf[i]; + } + printf("%d\n", ans); + return 0; +} diff --git a/self_training/2018-02-24/agc21B.cpp b/self_training/2018-02-24/agc21B.cpp new file mode 100644 index 00000000..3bfa10b7 --- /dev/null +++ b/self_training/2018-02-24/agc21B.cpp @@ -0,0 +1,61 @@ +#include +using namespace std; +typedef long long LL; +const int maxn = 101; +const double pi = acos(-1.0); +int n, m, ord[maxn], stk[maxn]; +struct Point { + int x, y; + void read() { + scanf("%d%d", &x, &y); + } + bool operator < (Point const &t) const { + return x < t.x || (x == t.x && y < t.y); + } + Point operator - (Point const &t) const { + return (Point){x - t.x, y - t.y}; + } + LL det(Point const &t) const { + return (LL)x * t.y - (LL)y * t.x; + } + LL dot(Point const &t) const { + return (LL)x * t.x + (LL)y * t.y; + } + double len() const { + return sqrt(dot(*this)); + } +} p[maxn]; +double ans[maxn]; +int main() { + scanf("%d", &n); + for(int i = 0; i < n; ++i) { + p[i].read(); + ord[i] = i; + } + sort(ord, ord + n, [&](int const &u, int const &v) { + return p[u] < p[v]; + }); + for(int i = 0; i < n; ++i) { + int o = ord[i]; + for( ; m > 1 && (p[stk[m - 1]] - p[stk[m - 2]]).det(p[o] - p[stk[m - 2]]) <= 0; --m); + stk[m++] = o; + } + for(int i = n - 2, tp = m; i >= 0; --i) { + int o = ord[i]; + for( ; m > tp && (p[stk[m - 1]] - p[stk[m - 2]]).det(p[o] - p[stk[m - 2]]) <= 0; --m); + stk[m++] = o; + } + --m; + if(m == 2) { + ans[stk[0]] = ans[stk[1]] = 0.5; + } else { + for(int i = 0; i < m; ++i) { + int lft = stk[(m + i - 1) % m], mid = stk[i], rht = stk[(i + 1) % m]; + Point pre = p[mid] - p[lft], suf = p[rht] - p[mid]; + ans[mid] = acos(pre.dot(suf) / pre.len() / suf.len()) / pi / 2; + } + } + for(int i = 0; i < n; ++i) + printf("%.20f\n", ans[i]); + return 0; +} diff --git a/self_training/2018-02-24/agc21C.cpp b/self_training/2018-02-24/agc21C.cpp new file mode 100644 index 00000000..e3abff73 --- /dev/null +++ b/self_training/2018-02-24/agc21C.cpp @@ -0,0 +1,89 @@ +#include +using namespace std; +const int maxn = 3001; +int n, m, a, b; +char buf[maxn][maxn]; +int main() { + scanf("%d%d%d%d", &n, &m, &a, &b); + int row = n & 1 ? m >> 1 : 0, col = m & 1 ? n >> 1 : 0, sp = n & 1 && m & 1; + int aa = max(a - row, 0), bb = max(b - col, 0); + int com = (aa >> 1) + (bb >> 1), spa = aa & 1, spb = bb & 1; + int upp = (n >> 1) * (m >> 1); + if(com + spa + spb <= upp) { + for(int i = 0; i < a && i < row; ++i) { + buf[0][i << 1] = '<'; + buf[0][i << 1 | 1] = '>'; + } + for(int i = 0; i < b && i < col; ++i) { + buf[(n & 1) + (i << 1)][0] = '^'; + buf[(n & 1) + (i << 1 | 1)][0] = 'v'; + } + } else if(sp && com + 1 <= upp) { + if(spa) { + for(int i = 0; i < b && i < col; ++i) { + buf[(i << 1)][0] = '^'; + buf[(i << 1 | 1)][0] = 'v'; + } + for(int i = 0; i < a && i < row; ++i) { + buf[0][(m & 1) + (i << 1)] = '<'; + buf[0][(m & 1) + (i << 1 | 1)] = '>'; + } + buf[n - 1][0] = '<'; + buf[n - 1][1] = '>'; + --aa; + if(spb) { + buf[n - 2][2] = '^'; + buf[n - 1][2] = 'v'; + --bb; + } + } else { + for(int i = 0; i < a && i < row; ++i) { + buf[0][i << 1] = '<'; + buf[0][i << 1 | 1] = '>'; + } + for(int i = 0; i < b && i < col; ++i) { + buf[(n & 1) + (i << 1)][0] = '^'; + buf[(n & 1) + (i << 1 | 1)][0] = 'v'; + } + buf[0][m - 1] = '^'; + buf[1][m - 1] = 'v'; + --bb; + } + } else { + puts("NO"); + return 0; + } + for(int i = 0; i < n >> 1 && aa + bb; ++i) + for(int j = 0; j < m >> 1 && aa + bb; ++j) { + if(aa && !buf[(n & 1) + (i << 1)][(m & 1) + (j << 1)] && !buf[(n & 1) + (i << 1)][(m & 1) + (j << 1 | 1)]) { + buf[(n & 1) + (i << 1)][(m & 1) + (j << 1)] = '<'; + buf[(n & 1) + (i << 1)][(m & 1) + (j << 1 | 1)] = '>'; + --aa; + } + if(aa && !buf[(n & 1) + (i << 1 | 1)][(m & 1) + (j << 1)] && !buf[(n & 1) + (i << 1 | 1)][(m & 1) + (j << 1 | 1)]) { + buf[(n & 1) + (i << 1 | 1)][(m & 1) + (j << 1)] = '<'; + buf[(n & 1) + (i << 1 | 1)][(m & 1) + (j << 1 | 1)] = '>'; + --aa; + } + if(bb && !buf[(n & 1) + (i << 1)][(m & 1) + (j << 1)] && !buf[(n & 1) + (i << 1 | 1)][(m & 1) + (j << 1)]) { + buf[(n & 1) + (i << 1)][(m & 1) + (j << 1)] = '^'; + buf[(n & 1) + (i << 1 | 1)][(m & 1) + (j << 1)] = 'v'; + --bb; + } + if(bb && !buf[(n & 1) + (i << 1)][(m & 1) + (j << 1 | 1)] && !buf[(n & 1) + (i << 1 | 1)][(m & 1) + (j << 1 | 1)]) { + buf[(n & 1) + (i << 1)][(m & 1) + (j << 1 | 1)] = '^'; + buf[(n & 1) + (i << 1 | 1)][(m & 1) + (j << 1 | 1)] = 'v'; + --bb; + } + } + assert(!aa && !bb); + puts("YES"); + for(int i = 0; i < n; ++i) { + for(int j = 0; j < m; ++j) + if(!buf[i][j]) + buf[i][j] = '.'; + buf[i][m] = '\0'; + puts(buf[i]); + } + return 0; +} diff --git a/self_training/2018-02-24/agc21D.cpp b/self_training/2018-02-24/agc21D.cpp new file mode 100644 index 00000000..8b16afd4 --- /dev/null +++ b/self_training/2018-02-24/agc21D.cpp @@ -0,0 +1,24 @@ +#include +using namespace std; +const int maxn = 301; +int n, m, f[maxn][maxn][maxn]; +char buf[maxn]; +int F(int L, int R, int c) { + if(L >= R) + return L == R; + if(f[L][R][c] != -1) + return f[L][R][c]; + int ret = max(F(L + 1, R, c), F(L, R - 1, c)); + if(buf[L] == buf[R]) + ret = max(ret, F(L + 1, R - 1, c) + 2); + if(c) + ret = max(ret, F(L + 1, R - 1, c - 1) + 2); + return f[L][R][c] = ret; +} +int main() { + scanf("%s%d", buf, &m); + n = strlen(buf); + memset(f, -1, sizeof f); + printf("%d\n", F(0, n - 1, m)); + return 0; +} diff --git a/self_training/2018-03-13/7736B.cpp b/self_training/2018-03-13/7736B.cpp new file mode 100644 index 00000000..80cf8427 --- /dev/null +++ b/self_training/2018-03-13/7736B.cpp @@ -0,0 +1,35 @@ +#include +using namespace std; +typedef long long LL; +const int maxn = (int)1e5 + 1, BASE = (int)1e9; +int n, m, a[maxn], b[maxn]; +int main() { + scanf("%d%d", &n, &m); + int pos1 = 1, pos2 = 1; + for(int i = 1; i <= n; ++i) { + scanf("%d", a + i); + if(a[pos1] < a[i]) + pos1 = pos2 = i; + else if(a[pos1] == a[i]) + pos2 = i; + } + int pos3 = 1; + for(int i = 1; i <= m; ++i) { + scanf("%d", b + i); + if(b[pos3] < b[i]) + pos3 = i; + } + LL ans = 0; + for(int i = 1; i <= pos1; ++i) + ans += (LL)a[i] * BASE + b[1]; + for(int i = 2; i <= pos3; ++i) + ans += (LL)a[pos1] * BASE + b[i]; + for(int i = pos1 + 1; i <= pos2; ++i) + ans += (LL)a[i] * BASE + b[pos3]; + for(int i = pos3 + 1; i <= m; ++i) + ans += (LL)a[pos2] * BASE + b[i]; + for(int i = pos2 + 1; i <= n; ++i) + ans += (LL)a[i] * BASE + b[m]; + printf("%lld\n", ans); + return 0; +} diff --git a/self_training/2018-03-13/7736C.cpp b/self_training/2018-03-13/7736C.cpp new file mode 100644 index 00000000..d62f6815 --- /dev/null +++ b/self_training/2018-03-13/7736C.cpp @@ -0,0 +1,24 @@ +#include +using namespace std; +const int maxn = 101; +int n, m, ds, cs, da, ca, h[maxn]; +int main() { + scanf("%d%d%d%d%d%d", &n, &m, &ds, &cs, &da, &ca); + for(int i = 0; i < n; ++i) + scanf("%d", h + i); + sort(h, h + n); + int ans = 0; + for(int i = 0; i * ca <= m; ++i) { + int dmg = i * da, rem = m - i * ca, ctr = 0; + for(int j = 0; j < n; ++j) { + int tmp = (dmg < h[j] ? ((h[j] - dmg - 1) / ds + 1) * cs : 0); + if(tmp <= rem) { + ++ctr; + rem -= tmp; + } + } + ans = max(ans, ctr); + } + printf("%d\n", ans); + return 0; +} diff --git a/self_training/2018-03-13/7736D.cpp b/self_training/2018-03-13/7736D.cpp new file mode 100644 index 00000000..93ed232c --- /dev/null +++ b/self_training/2018-03-13/7736D.cpp @@ -0,0 +1,33 @@ +#include +using namespace std; +typedef long long LL; +const int maxn = 1001, mod = 998244353; +int n, m, f[maxn], g[maxn], ans; +int mod_pow(int x, int k) { + int ret = 1; + for( ; k > 0; k >>= 1, x = (LL)x * x % mod) + (k & 1) && (ret = (LL)ret * x % mod); + return ret; +} +int main() { + scanf("%d%d", &n, &m); + f[1] = 1; + for(int i = 2; i <= n; ++i) + f[i] = mod - (int)(mod / i * (LL)f[mod % i] % mod); + f[0] = 1; + for(int i = 1; i <= n; ++i) + f[i] = (LL)f[i - 1] * f[i] % mod; + for(int i = 1; i <= n; ++i) + f[i] = mod_pow(f[i], m); + g[0] = 1; + for(int i = 0; i <= n; ++i) { + for(int j = 0; j < i; ++j) + g[i] = (g[i] + ((i - j) & 1 ? 1LL : -1LL) * g[j] * f[i - j]) % mod; + g[i] < 0 && (g[i] += mod); + } + int ans = g[n]; + for(int i = 1; i <= n; ++i) + ans = (LL)ans * i % mod; + printf("%d\n", ans); + return 0; +} diff --git a/self_training/2018-03-30/82A.cpp b/self_training/2018-03-30/82A.cpp new file mode 100644 index 00000000..19990244 --- /dev/null +++ b/self_training/2018-03-30/82A.cpp @@ -0,0 +1,30 @@ +#include +using namespace std; +typedef long long LL; +const int maxp = 101; +int t, tot, pr[maxp], d[maxp]; +LL lim; +int dfs(int dep, int upp, LL val, int sig) { + int ret = sig; + LL plim = lim / pr[dep]; + for(int i = 1; i <= upp && val <= plim; ++i) + ret = max(ret, dfs(dep + 1, i, val *= pr[dep], sig * (i + 1))); + return ret; +} +int main() { + for(int i = 2; i < maxp; ++i) { + if(!d[i]) + pr[tot++] = d[i] = i; + for(int j = 0, k; (k = i * pr[j]) < maxp; ++j) { + d[k] = pr[j]; + if(d[i] == pr[j]) + break; + } + } + scanf("%d", &t); + while(t--) { + scanf("%lld", &lim); + printf("%d\n", dfs(0, maxp, 1, 1)); + } + return 0; +} diff --git a/self_training/2018-03-30/82B.cpp b/self_training/2018-03-30/82B.cpp new file mode 100644 index 00000000..0e485f90 --- /dev/null +++ b/self_training/2018-03-30/82B.cpp @@ -0,0 +1,35 @@ +#include +using namespace std; +const int maxn = (int)1e6 + 1, maxd = 21; +int n, m, lim, a[maxn], mx, fa[maxd][maxn]; +int main() { + scanf("%d%d%d", &n, &m, &lim); + for(int i = 1, las = 0, sum = 0; i <= n; ++i) { + scanf("%d", a + i); + for(sum += a[i]; sum > lim; sum -= a[++las]); + fa[0][i] = las; + } + for(mx = 0; 1 << mx <= n; ++mx); + for(int i = 1; i < mx; ++i) + for(int j = 1; j <= n; ++j) + fa[i][j] = fa[i - 1][fa[i - 1][j]]; + while(m--) { + int L, R, ans = 0; + scanf("%d%d", &L, &R); + for(int i = mx - 1; i >= 0; --i) + if(fa[i][R] >= L) { + R = fa[i][R]; + ans += 1 << i; + } + if(L <= R) { + R = fa[0][R]; + ++ans; + } + if(L <= R) { + puts("Chtholly"); + } else { + printf("%d\n", ans); + } + } + return 0; +} diff --git a/self_training/2018-03-30/82C.cpp b/self_training/2018-03-30/82C.cpp new file mode 100644 index 00000000..7dfa24e7 --- /dev/null +++ b/self_training/2018-03-30/82C.cpp @@ -0,0 +1,64 @@ +#include +using namespace std; +typedef long long LL; +const int maxn = (int)1e6 + 3, maxd = 11, mod = 10; +int n, m, a[maxn], pos[maxn], bit[maxd][maxn]; +struct Query { + int idx, L, R; + bool operator < (Query const &t) const { + return R < t.R; + } +} que[maxn]; +char ans[maxn][maxd]; +void bit_upd(int bit[], int x, int v) { + for( ; x <= n; x += x & -x) + bit[x] += v; +} +int bit_que(int bit[], int x) { + int ret = 0; + for( ; x > 0; x -= x & -x) + ret += bit[x]; + return ret; +} +int main() { + scanf("%d%d", &n, &m); + for(int i = 1; i <= n; ++i) + scanf("%d", a + i); + for(int i = 1; i <= m; ++i) { + que[i].idx = i; + scanf("%d%d", &que[i].L, &que[i].R); + } + sort(que + 1, que + m + 1); + for(int i = 1, j = 1; i <= n; ++i) { + int las = pos[a[i]], lft = 0, rht = 0, pre, cur = i; + int ctr = 0; + while(cur > las && (lft < maxd || rht < maxd)) { + pre = cur; + cur = las; + if(lft < maxd) + cur = max(cur, pos[max(a[i] - lft - 1, 0)]); + if(rht < maxd) + cur = max(cur, pos[min(a[i] + rht + 1, maxn - 1)]); + if(lft && lft < maxd) { + bit_upd(bit[lft], cur + 1, -1); + bit_upd(bit[lft], pre + 1, 1); + } + if(rht && rht < maxd) { + bit_upd(bit[rht], cur + 1, -1); + bit_upd(bit[rht], pre + 1, 1); + } + if(lft + rht + 1 < maxd) { + bit_upd(bit[lft + rht + 1], cur + 1, 1); + bit_upd(bit[lft + rht + 1], pre + 1, -1); + } + for( ; lft < maxd && a[i] - lft - 1 > 0 && pos[a[i] - lft - 1] >= cur; ++lft); + for( ; rht < maxd && a[i] + rht + 1 < maxn && pos[a[i] + rht + 1] >= cur; ++rht); + } + for(pos[a[i]] = i; j <= m && que[j].R == i; ++j) + for(int k = 1; k < maxd; ++k) + ans[que[j].idx][k - 1] = '0' + bit_que(bit[k], que[j].L) % mod; + } + for(int i = 1; i <= m; ++i) + puts(ans[i]); + return 0; +} diff --git a/self_training/2018-03-30/82D.cpp b/self_training/2018-03-30/82D.cpp new file mode 100644 index 00000000..41284c2d --- /dev/null +++ b/self_training/2018-03-30/82D.cpp @@ -0,0 +1,19 @@ +#include +using namespace std; +const int maxn = 101; +int n, a[maxn]; +int main() { + scanf("%d", &n); + for(int i = 0; i < n; ++i) + scanf("%d", a + i); + if(a[n - 1] == 0) { + puts("UP"); + } else if(a[n - 1] == 15) { + puts("DOWN"); + } else if(n > 1) { + puts(a[n - 2] < a[n - 1] ? "UP" : "DOWN"); + } else { + puts("-1"); + } + return 0; +} diff --git a/self_training/2018-03-30/82E.cpp b/self_training/2018-03-30/82E.cpp new file mode 100644 index 00000000..8e004307 --- /dev/null +++ b/self_training/2018-03-30/82E.cpp @@ -0,0 +1,72 @@ +#include +using namespace std; +const int maxn = 1001, maxd = 1 << 16 | 1; +int lbt[maxd]; +int lowBit(bitset const &msk, size_t const &low, size_t const &upp) { + typedef unsigned long long _WordT; + _WordT *seq = (_WordT *)&msk; + size_t pL = low >> 6, pR = upp >> 6; + size_t qL = low & 63, qR = upp & 63; + for(size_t i = pL; i <= pR; ++i) { + _WordT val = seq[i]; + if(i == pR && qR < 63) + val &= (static_cast<_WordT>(1) << (qR + 1)) - 1; + if(i == pL) + val = (val >> qL) << qL; + if(val != static_cast<_WordT>(0)) { + size_t ret = i << 6; + if((val & ((static_cast<_WordT>(1) << 32) - 1)) == static_cast<_WordT>(0)) { + val >>= 32; + ret |= 32; + } + if((val & ((static_cast<_WordT>(1) << 16) - 1)) == static_cast<_WordT>(0)) { + val >>= 16; + ret |= 16; + } + return ret + lbt[static_cast(val & ((static_cast<_WordT>(1) << 16) - 1))]; + } + } + return -1; +} +int n, m, q; +bitset e[maxn], dis[maxn][maxn], cur, nxt, tmp; +int main() { + lbt[0] = -1; + for(int i = 1; i < maxd; ++i) + lbt[i] = i & 1 ? 0 : lbt[i >> 1] + 1; + scanf("%d%d%d", &n, &m, &q); + while(m--) { + int u, v; + scanf("%d%d", &u, &v); + --u, --v; + e[u].set(v); + e[v].set(u); + } + for(int i = 0; i < n; ++i) { + dis[i][0].set(i); + cur = dis[i][0]; + nxt = cur | e[i]; + for(int j = 1; j < n; ++j) { + tmp = nxt ^ cur; + cur = dis[i][j] = nxt; + for(int u = lowBit(tmp, 0, n - 1); u != -1; u = lowBit(tmp, u + 1, n - 1)) + nxt |= e[u]; + if(cur == nxt) { + for(int k = j + 1; k < n; ++k) + dis[i][k] = cur; + break; + } + } + } + while(q--) { + int c, u, v; + cur.reset(); + scanf("%d", &c); + while(c--) { + scanf("%d%d", &u, &v); + cur |= dis[--u][min(v, n - 1)]; + } + printf("%d\n", (int)cur.count()); + } + return 0; +} diff --git a/self_training/2018-04-08-2/7989A.cpp b/self_training/2018-04-08-2/7989A.cpp new file mode 100644 index 00000000..30a58b3b --- /dev/null +++ b/self_training/2018-04-08-2/7989A.cpp @@ -0,0 +1,20 @@ +#include +using namespace std; +map Hash; +int F(int n) { + if(Hash.count(n)) + return Hash[n]; + int ret = 0; + for(int i = 1; i <= n; i <<= 1) { + int lft = n / (i << 1), rht = n % (i << 1); + ret = max(ret, F(lft) + (rht > i)); + } + return Hash[n] = ret; +} +int main() { + int n; + scanf("%d", &n); + Hash[0] = 0; + printf("%d\n", F(n)); + return 0; +} diff --git a/self_training/2018-04-08-2/7989B.cpp b/self_training/2018-04-08-2/7989B.cpp new file mode 100644 index 00000000..2187b62b --- /dev/null +++ b/self_training/2018-04-08-2/7989B.cpp @@ -0,0 +1,27 @@ +#include +using namespace std; +typedef long long LL; +const int maxn = (int)1e5 + 1, maxv = (int)1e7 + 1; +int n, a[maxn]; +LL ans; +int main() { + scanf("%d", &n); + for(int i = 0; i < n; ++i) { + scanf("%d", a + i); + if(i && !a[i] && a[i - 1]) { + puts("-1"); + return 0; + } + } + for(int i = 1; i < n; ++i) + while(a[i - 1] > a[i]) + if(a[i] * 2 < maxv) { + ++ans; + a[i] *= 2; + } else { + a[i - 1] = (a[i - 1] + 1) >> 1; + ans += n - i; + } + printf("%lld\n", ans); + return 0; +} diff --git a/self_training/2018-04-08-2/7989C.cpp b/self_training/2018-04-08-2/7989C.cpp new file mode 100644 index 00000000..e1ebb339 --- /dev/null +++ b/self_training/2018-04-08-2/7989C.cpp @@ -0,0 +1,28 @@ +#include +using namespace std; +typedef long long LL; +const int maxn = (int)2e5 + 1, INF = 0x3f3f3f3f; +int n, h, c[2]; +pair p[2][maxn]; +LL ans; +int main() { + scanf("%d%d", &n, &h); + for(int i = 0; i < n; ++i) { + int x, y; + scanf("%d%d", &y, &x); + int o = (y - x) & 1; + p[o][c[o]++] = make_pair((LL)y - x, (LL)x); + } + for(int o = 0; o < 2; ++o) { + sort(p[o], p[o] + c[o]); + for(int i = 0; i < c[o]; ++i) { + LL base = p[o][i].first, dt = p[o][i].second; + ans += (h - 1) * 2; + int pL = lower_bound(p[o], p[o] + c[o], make_pair(base - (h - 1 - dt) * 2, (LL)-INF)) - p[o]; + int pR = lower_bound(p[o], p[o] + c[o], make_pair(base + dt * 2 + 1, (LL)-INF)) - p[o] - 1; + ans -= pR - pL; + } + } + printf("%lld\n", ans); + return 0; +} diff --git a/self_training/2018-04-08-2/7989D.cpp b/self_training/2018-04-08-2/7989D.cpp new file mode 100644 index 00000000..7bb80f4a --- /dev/null +++ b/self_training/2018-04-08-2/7989D.cpp @@ -0,0 +1,57 @@ +#include +using namespace std; +typedef double DB; +const int maxn = (int)1e5 + 1, INF = 0x3f3f3f3f; +const DB eps = 1e-12; +int n, pL[maxn], pR[maxn]; +vector e[maxn]; +DB lim, qL[maxn], qR[maxn]; +inline int dcmp(DB x) { + return (x > eps) - (x < -eps); +} +bool dfs(int u, int fa) { + qL[u] = pL[u]; + qR[u] = pR[u]; + for(auto &v : e[u]) { + if(v == fa) + continue; + if(!dfs(v, u)) + return 0; + qL[u] = max(qL[u], qL[v] - lim); + qR[u] = min(qR[u], qR[v] + lim); + } + return dcmp(qL[u] - qR[u]) <= 0; +} +int main() { + scanf("%d", &n); + for(int i = 0; i < n; ++i) { + char buf[11]; + scanf("%s", buf); + if(buf[0] == '*') { + pL[i] = -INF; + pR[i] = INF; + } else { + sscanf(buf, "%d", pL + i); + pR[i] = pL[i]; + } + } + for(int i = 1; i < n; ++i) { + int u, v; + scanf("%d%d", &u, &v); + --u; + --v; + e[u].push_back(v); + e[v].push_back(u); + } + DB L = 0, R = 1e7; + for(int i = 0; i < 100; ++i) { + lim = L + (R - L) * 0.5; + if(dfs(1, -1)) { + R = lim; + } else { + L = lim; + } + } + printf("%.20f\n", L); + return 0; +} diff --git a/self_training/2018-04-08/A.cpp b/self_training/2018-04-08/A.cpp new file mode 100644 index 00000000..f748cecc --- /dev/null +++ b/self_training/2018-04-08/A.cpp @@ -0,0 +1,43 @@ +#include +using namespace std; +const int maxn = 31; +int t, n, m, dam[maxn]; +char buf[maxn]; + +int main() { + scanf("%d", &t); + for(int Case = 1; Case <= t; ++Case) { + scanf("%d%s", &m, buf); + n = strlen(buf); + for(int i = 0, v = 1; i < n; ++i) + if(buf[i] == 'C') { + dam[i] = 0; + v <<= 1; + } else { + dam[i] = v; + m -= v; + } + int ans = 0; + while(m < 0) { + bool chk = 0; + for(int i = n - 1; i > 0; --i) + if(buf[i - 1] == 'C' && buf[i] == 'S') { + swap(buf[i - 1], buf[i]); + swap(dam[i - 1], dam[i]); + dam[i - 1] >>= 1; + m += dam[i - 1]; + chk = 1; + break; + } + if(!chk) + break; + ++ans; + } + if(m < 0) { + printf("Case #%d: IMPOSSIBLE\n", Case); + } else { + printf("Case #%d: %d\n", Case, ans); + } + } + return 0; +} diff --git a/self_training/2018-04-08/B.cpp b/self_training/2018-04-08/B.cpp new file mode 100644 index 00000000..a8558ba2 --- /dev/null +++ b/self_training/2018-04-08/B.cpp @@ -0,0 +1,34 @@ +#include +using namespace std; +const int maxn = (int)1e5 + 1; +int t, n, a[maxn], b[maxn]; +int main() { + scanf("%d", &t); + for(int Case = 1; Case <= t; ++Case) { + scanf("%d", &n); + for(int i = 0; i < n; ++i) + scanf("%d", a + i); + for(int i = 0; i < n; i += 2) + b[i >> 1] = a[i]; + sort(b, b + ((n + 1) >> 1)); + for(int i = 0; i < n; i += 2) + a[i] = b[i >> 1]; + for(int i = 1; i < n; i += 2) + b[i >> 1] = a[i]; + sort(b, b + (n >> 1)); + for(int i = 1; i < n; i += 2) + a[i] = b[i >> 1]; + int pos = -1; + for(int i = 1; i < n; ++i) + if(a[i - 1] > a[i]) { + pos = i - 1; + break; + } + if(pos >= 0) { + printf("Case #%d: %d\n", Case, pos); + } else { + printf("Case #%d: OK\n", Case); + } + } + return 0; +} diff --git a/self_training/2018-04-08/C.cpp b/self_training/2018-04-08/C.cpp new file mode 100644 index 00000000..0d38d64a --- /dev/null +++ b/self_training/2018-04-08/C.cpp @@ -0,0 +1,59 @@ +#include +using namespace std; +const int maxn = 201; +int t, n, r, c, wan[maxn][maxn]; +bool vis[maxn][maxn]; +set > pos[11]; +int main() { + scanf("%d", &t); + while(t--) { + scanf("%d", &n); + memset(wan, 0, sizeof wan); + memset(vis, 0, sizeof vis); + for(int i = 0; i <= 9; ++i) + set >().swap(pos[i]); + n = max(n, 9); + r = 3; + c = (n - 1) / r + 1; + for(int i = 4, j; (j = (n - 1) / i + 1) >= 3; ++i) + if(i * j < r * c) { + r = i; + c = j; + } + for(int i = 1; i <= r; ++i) + for(int j = 1; j <= c; ++j) + for(int dx = -1; dx <= 1; ++dx) + for(int dy = -1; dy <= 1; ++dy) { + int x = i + dx, y = j + dy; + if(x > 1 && x < r && y > 1 && y < c) + ++wan[x][y]; + } + for(int i = 2; i < r; ++i) + for(int j = 2; j < c; ++j) + pos[wan[i][j]].insert(make_pair(i, j)); + int mx = 9; + while(1) { + for( ; !pos[mx].size(); --mx); + assert(mx > 0); + pair state = *pos[mx].begin(); + int x = state.first, y = state.second; + printf("%d %d\n", x, y); + fflush(stdout); + scanf("%d%d", &x, &y); + if(x <= 0 || y <= 0) + break; + if(vis[x][y]) + continue; + vis[x][y] = 1; + for(int dx = -1; dx <= 1; ++dx) + for(int dy = -1; dy <= 1; ++dy) { + int xx = x + dx, yy = y + dy; + if(xx > 1 && xx < r && yy > 1 && yy < c) { + pos[wan[xx][yy]].erase(make_pair(xx, yy)); + pos[--wan[xx][yy]].insert(make_pair(xx, yy)); + } + } + } + } + return 0; +} diff --git a/self_training/2018-04-08/D.cpp b/self_training/2018-04-08/D.cpp new file mode 100644 index 00000000..245d3be2 --- /dev/null +++ b/self_training/2018-04-08/D.cpp @@ -0,0 +1,37 @@ +#include +using namespace std; +typedef long double DB; +const DB sqrt2 = sqrtl(2.0), sqrt3 = sqrtl(3.0), base = atan(1 / sqrt2); +int t; +DB p[3][3], Ratio, theta, sin_theta, cos_theta; +inline void readDB(DB &x) { + double _x; + scanf("%lf", &_x); + x = _x; +} +inline void writeDB(DB x, char endc) { + printf("%.20f%c", (double)x, endc); +} +int main() { + scanf("%d", &t); + for(int Case = 1; Case <= t; ++Case) { + readDB(Ratio); + theta = asin(Ratio / sqrt3) - base; + sin_theta = sin(theta); + cos_theta = cos(theta); + p[0][0] = -0.5 * sin_theta; + p[0][1] = 0.5 * cos_theta; + p[0][2] = 0; + p[1][0] = 0.25 * sqrt2 * cos_theta; + p[1][1] = 0.25 * sqrt2 * sin_theta; + p[1][2] = 0.25 * sqrt2; + p[2][0] = -0.25 * sqrt2 * cos_theta; + p[2][1] = -0.25 * sqrt2 * sin_theta; + p[2][2] = 0.25 * sqrt2; + printf("Case #%d:\n", Case); + for(int i = 0; i < 3; ++i) + for(int j = 0; j < 3; ++j) + writeDB(p[i][j], " \n"[j == 2]); + } + return 0; +} diff --git a/self_training/2018-07-06/131A.cpp b/self_training/2018-07-06/131A.cpp new file mode 100644 index 00000000..c2648451 --- /dev/null +++ b/self_training/2018-07-06/131A.cpp @@ -0,0 +1,27 @@ +#include +using namespace std; +const int maxn = (int)1e5 + 1; +int n, m, L, R, pos[maxn], ord[maxn]; +int main() { + scanf("%d", &n); + for(int i = 1; i <= n; ++i) { + int v; + scanf("%d", &v); + pos[v] = ord[i] = i; + } + L = 1; + R = n; + scanf("%d", &m); + while(m--) { + char op[5]; + int val; + scanf("%s%d", op, &val); + pos[val] = op[0] == 'F' ? --L : ++R; + } + sort(ord + 1, ord + n + 1, [&](int const &u, int const &v) { + return pos[u] < pos[v]; + }); + for(int i = 1; i <= n; ++i) + printf("%d%c", ord[i], " \n"[i == n]); + return 0; +} diff --git a/self_training/2018-07-06/131B.cpp b/self_training/2018-07-06/131B.cpp new file mode 100644 index 00000000..8890f8c3 --- /dev/null +++ b/self_training/2018-07-06/131B.cpp @@ -0,0 +1,35 @@ +#include +using namespace std; +typedef long long LL; +const int maxn = 501; +int n, m, xx, yy, zz, mat[maxn][maxn], c[maxn], cc[maxn]; +LL s[maxn], ss[maxn], ans; +int main() { + scanf("%d%d%d%d%d", &n, &m, &xx, &yy, &zz); + for(int i = 1; i <= n; ++i) + for(int j = 1; j <= m; ++j) + scanf("%d", mat[i] + j); + for(int xL = 1; xL <= n; ++xL) { + memset(c + 1, 0, m * sizeof(int)); + memset(s + 1, 0, m * sizeof(LL)); + for(int xR = xL; xR <= n && xR - xL + 1 <= xx; ++xR) { + int L = 0, R = 0; + static int que[maxn]; + que[R++] = 0; + for(int y = 1; y <= m; ++y) { + c[y] += !mat[xR][y]; + cc[y] = cc[y - 1] + c[y]; + s[y] += mat[xR][y]; + ss[y] = ss[y - 1] + s[y]; + for( ; L < R && !(y - que[L] <= yy && cc[y] - cc[que[L]] <= zz); ++L); + if(L < R) + ans = max(ans, ss[y] - ss[que[L]]); + for( ; L < R && ss[y] <= ss[que[R - 1]]; --R); + que[R++] = y; + } + } + } + printf("%lld\n", ans); + return 0; +} + diff --git a/self_training/2018-07-06/131C.cpp b/self_training/2018-07-06/131C.cpp new file mode 100644 index 00000000..87fe34b2 --- /dev/null +++ b/self_training/2018-07-06/131C.cpp @@ -0,0 +1,117 @@ +#include +using namespace std; +typedef long long LL; +const int maxn = (int)5e4 + 1, maxd = 11, maxs = 1 << 10 | 1, mod = (int)1e9 + 7; +int n, m, tot, lnk[maxn], ord[maxn], fa[maxn], sz[maxn], col[maxn], msk[maxn]; +struct Edge { + int nxt, v; +} e[maxn << 1 | 1]; +bool ban[maxn]; +int bits[maxs], cnt[maxs], sum[maxs], tmp[maxs], res[maxd]; +void bfs(int rt) { + tot = 0; + ord[tot++] = rt; + for(int i = 0; i < tot; ++i) { + int u = ord[i]; + for(int it = lnk[u]; it != -1; it = e[it].nxt) { + int v = e[it].v; + if(v == fa[u] || ban[v]) + continue; + fa[v] = u; + ord[tot++] = v; + } + } +} +int getRoot(int rt) { + bfs(rt); + int pos = -1, cnt = maxn; + for(int i = tot - 1; i >= 0; --i) { + int u = ord[i], mx = 0; + sz[u] = 1; + for(int it = lnk[u]; it != -1; it = e[it].nxt) { + int v = e[it].v; + if(v == fa[u] || ban[v]) + continue; + sz[u] += sz[v]; + mx = max(mx, sz[v]); + } + mx = max(mx, tot - sz[u]); + if(mx < cnt) { + pos = u; + cnt = mx; + } + } + return pos; +} +void solve(int rt) { + fa[rt] = -1; + rt = getRoot(rt); + ban[rt] = 1; + memset(sum, 0, m * sizeof(int)); + msk[rt] = col[rt]; + ++sum[msk[rt]]; + ++res[bits[msk[rt]]]; + for(int it = lnk[rt]; it != -1; it = e[it].nxt) { + int v = e[it].v; + if(ban[v]) + continue; + memset(cnt, 0, m * sizeof(int)); + fa[v] = rt; + bfs(v); + for(int i = 0; i < tot; ++i) { + int u = ord[i]; + msk[u] = msk[fa[u]] | col[u]; + ++cnt[msk[u]]; + } + for(int i = 1; i < m; i <<= 1) + for(int j = 0; j < m; ++j) + if(i & j) { + (cnt[j] += cnt[j - i]) >= mod && (cnt[j] -= mod); + (sum[j] += sum[j - i]) >= mod && (sum[j] -= mod); + } + for(int i = 0; i < m; ++i) + tmp[i] = (LL)cnt[i] * sum[i] % mod; + for(int i = 1; i < m; i <<= 1) + for(int j = 0; j < m; ++j) + if(i & j) { + (cnt[j] -= cnt[j - i]) < 0 && (cnt[j] += mod); + (sum[j] -= sum[j - i]) < 0 && (sum[j] += mod); + (tmp[j] -= tmp[j - i]) < 0 && (tmp[j] += mod); + } + for(int i = 0; i < m; ++i) { + sum[i] += cnt[i]; + (res[bits[i]] += tmp[i]) >= mod && (res[bits[i]] -= mod); + } + } + for(int it = lnk[rt]; it != -1; it = e[it].nxt) { + int v = e[it].v; + if(!ban[v]) + solve(v); + } +} +int main() { + int K; + scanf("%d%d", &n, &K); + m = 1 << K; + memset(lnk + 1, -1, n * sizeof(int)); + for(int i = 1; i <= n; ++i) { + scanf("%d", col + i); + col[i] = 1 << (col[i] - 1); + } + for(int i = 1; i < n; ++i) { + int u, v; + scanf("%d%d", &u, &v); + e[tot] = (Edge){lnk[u], v}; + lnk[u] = tot++; + e[tot] = (Edge){lnk[v], u}; + lnk[v] = tot++; + } + for(int i = 1; i < m; ++i) + bits[i] = bits[i >> 1] + (i & 1); + solve(1); + int ans = 0; + for(int i = K; i >= 0; --i) + ans = (131LL * ans + res[i]) % mod; + printf("%d\n", ans); + return 0; +} diff --git a/self_training/2018-07-06/131D.cpp b/self_training/2018-07-06/131D.cpp new file mode 100644 index 00000000..77ce34fe --- /dev/null +++ b/self_training/2018-07-06/131D.cpp @@ -0,0 +1,47 @@ +#include +using namespace std; +typedef long long LL; +const int maxn = (int)1e5 + 3, maxc = 26; +int len, a[maxc], b[maxc], h[maxn << 1 | 1]; +LL f[maxn], g[maxn], pre[maxn], suf[maxn]; +char buf[maxn], str[maxn << 1 | 1]; +int main() { + scanf("%s", buf + 1); + len = strlen(buf + 1); + for(int i = 0; i < maxc; ++i) + scanf("%d%d", a + i, b + i); + str[0] = '$'; + for(int i = 1; i <= len; ++i) { + int o = buf[i] - 'a'; + f[i] = f[i - 1] + a[o]; + g[i] = g[i - 1] + b[o]; + str[i * 2 - 1] = buf[i]; + str[i * 2] = '#'; + } + LL mx = 0; + for(int i = 1; i <= len; ++i) { + mx = min(mx, f[i] - g[i]); + pre[i] = g[i] + mx; + } + mx = g[len] - f[len]; + for(int i = len; i >= 1; --i) { + mx = min(mx, g[i - 1] - f[i - 1]); + suf[i] = f[len] - g[i - 1] + mx; + } + LL ans = ~0ull >> 1; + for(int i = 1, mx = 0, id; i < len * 2; ++i) { + h[i] = i < mx ? min(h[id * 2 - i], mx - i) : 1; + for( ; str[i - h[i]] == str[i + h[i]]; ++h[i]); + if(mx < i + h[i]) { + mx = i + h[i]; + id = i; + } + int L = (i - h[i] + 1) / 2 + 1, R = (i + h[i]) / 2; + if(L <= R) { + ans = min(ans, f[L - 1] + suf[R + 1]); + ans = min(ans, f[len] - f[R] + pre[L - 1]); + } + } + printf("%lld\n", ans); + return 0; +} diff --git a/self_training/2018-07-06/131E.cpp b/self_training/2018-07-06/131E.cpp new file mode 100644 index 00000000..66a91e1e --- /dev/null +++ b/self_training/2018-07-06/131E.cpp @@ -0,0 +1,131 @@ +#include +using namespace std; +const int maxn = 105, maxm = 3001, maxl = 17, maxd = 1 << 16 | 1, INF = 0x3f3f3f3f; +int N, M, S, T, flow, cost, lnk[maxn], d[maxn], p[maxn], a[maxn]; +bool inq[maxn]; +struct Edge { + int nxt, v, w, c; +} e[maxm << 1 | 1]; +void addEdge(int u, int v, int w, int c) { + e[M] = (Edge){lnk[u], v, w, c}; + lnk[u] = M++; + e[M] = (Edge){lnk[v], u, 0, -c}; + lnk[v] = M++; +} +bool BellmanFord() { + memset(d, 0x3f, N * sizeof(int)); + memset(inq, 0, N * sizeof(bool)); + d[S] = 0; + inq[S] = 1; + p[S] = 0; + a[S] = INF; + queue Q; + Q.push(S); + while(!Q.empty()) { + int u = Q.front(); + Q.pop(); + inq[u] = 0; + for(int it = lnk[u]; it != -1;it = e[it].nxt) { + int v = e[it].v; + if(e[it].w > 0 && d[v] > d[u] + e[it].c) { + d[v] = d[u] + e[it].c; + p[v] = it; + a[v] = min(a[u], e[it].w); + if(!inq[v]) { + Q.push(v); + inq[v] = 1; + } + } + } + } + if(d[T] == INF) + return 0; + flow += a[T]; + cost += d[T] * a[T]; + for(int u = T; u != S; u = e[p[u] ^ 1].v) { + e[p[u]].w -= a[T]; + e[p[u] ^ 1].w += a[T]; + } + return 1; +} +int bits[maxd], rev[maxl][maxd], cnt[maxl], idx[maxd]; +int n, m, len[maxn], msk[maxn], dc[maxn], mc[maxn], dis[maxn][maxd]; +void scanBinary(int &len, int &msk) { + static char buf[maxl]; + scanf("%s", buf); + len = strlen(buf); + msk = 0; + for(int i = 0; i < len; ++i) + msk = msk << 1 | (buf[i] - '0'); +} +int main() { + for(int i = 1; i < maxd; ++i) + bits[i] = bits[i >> 1] + (i & 1); + for(int i = 0; i < maxd; ++i) + idx[i] = cnt[bits[i]]++; + for(int i = 1; i < maxl; ++i) + for(int j = 1; j < 1 << i; ++j) + rev[i][j] = rev[i - 1][j >> 1] | ((j & 1) << (i - 1)); + scanf("%d", &n); + for(int i = 0; i < n; ++i) + scanBinary(len[i], msk[i]); + scanf("%d", &m); + for(int i = n; i < n + m; ++i) + scanBinary(len[i], msk[i]); + for(int i = 0; i < n + m; ++i) + scanf("%d", dc + i); + for(int i = 0; i < n + m; ++i) + scanf("%d", mc + i); + for(int i = 0; i < n + m; ++i) { + int LEN = len[i], *DIS = dis[i], qL = 0, qR = 0; + static int que[maxd]; + memset(DIS, -1, cnt[bits[msk[i]]] * sizeof(int)); + que[qR++] = msk[i]; + DIS[idx[msk[i]]] = 0; + while(qL < qR) { + int u = que[qL++]; + for(int len = 2; len <= LEN; ++len) { + int all = (1 << len) - 1; + for(int L = 0, R = L + len - 1; R < LEN; ++L, ++R) { + int msk = (u >> L) & all; + int v = u ^ ((msk ^ rev[len][msk]) << L); + if(DIS[idx[v]] == -1) { + DIS[idx[v]] = DIS[idx[u]] + 1; + que[qR++] = v; + } + } + } + } + } + N = n + m + 4; + M = 0; + int PS = N - 4; + int PT = N - 3; + S = N - 2; + T = N - 1; + memset(lnk, -1, N * sizeof(int)); + for(int i = 0; i < n; ++i) { + addEdge(S, i, 1, 0); + addEdge(PS, T, 1, 0); + addEdge(i, PT, 1, dc[i]); + } + for(int i = n; i < n + m; ++i) { + addEdge(S, PT, 1, 0); + addEdge(i, T, 1, 0); + addEdge(PS, i, 1, dc[i]); + } + for(int i = 0; i < n; ++i) + for(int j = n; j < n + m; ++j) { + if(len[i] != len[j] || bits[msk[i]] != bits[msk[j]]) + continue; + int low = INF; + for(int k = 0; k < cnt[bits[msk[i]]]; ++k) + if(dis[i][k] != -1 && dis[j][k] != -1) + low = min(low, mc[i] * dis[i][k] + mc[j] * dis[j][k]); + addEdge(i, j, 1, low); + } + addEdge(PT, PS, INF, 0); + while(BellmanFord()); + printf("%d\n", cost); + return 0; +} diff --git a/self_training/2018-07-06/131F.cpp b/self_training/2018-07-06/131F.cpp new file mode 100644 index 00000000..9cd13f96 --- /dev/null +++ b/self_training/2018-07-06/131F.cpp @@ -0,0 +1,73 @@ +#include +using namespace std; +typedef long long LL; +const int maxn = (int)3e4 + 1; +int n, m, q, base, ord[maxn], idx[maxn], cnt[maxn], lpos[maxn]; +LL res[maxn]; +char buf[maxn]; +struct Mask { + LL data[3]; + int get(int pos) const { + return (data[pos / 9] >> (pos % 9 * 6)) & 63; + } + void add(int pos) { + int pre = get(pos), cur = (pre + 1) % base; + data[pos / 9] ^= (LL)(pre ^ cur) << (pos % 9 * 6); + } + bool operator < (Mask const &t) const { + for(int i = 0; i < 3; ++i) + if(data[i] != t.data[i]) + return data[i] < t.data[i]; + return 0; + } +} msk[maxn]; +struct Query { + int idx, L, R; + void read(int id) { + idx = id; + scanf("%d%d", &L, &R); + --L; + } + bool operator < (Query const &t) const { + if(lpos[L] != lpos[t.L]) + return lpos[L] < lpos[t.L]; + return R < t.R; + } +} que[maxn]; +int main() { + scanf("%d%s%d", &base, buf + 1, &q); + for(int i = 1; i <= q; ++i) + que[i].read(i); + n = strlen(buf + 1); + for(int i = 1; i <= n; ++i) { + msk[i] = msk[i - 1]; + msk[i].add(buf[i] - 'a'); + ord[i] = i; + } + sort(ord, ord + n + 1, [&](int const &u, int const &v) { + return msk[u] < msk[v]; + }); + m = 0; + for(int i = 0; i <= n; ++i) + idx[ord[i]] = !i || msk[ord[i - 1]] < msk[ord[i]] ? ++m : m; + int sq = (int)ceil(sqrt(q)); + for(int i = 1; i <= n; ++i) + lpos[i] = i < sq ? 0 : lpos[i - sq] + 1; + sort(que + 1, que + q + 1); + int qL = 0, qR = -1; + LL ans = 0; + for(int i = 1; i <= q; ++i) { + for( ; qL > que[i].L; --qL) + ans += cnt[idx[qL - 1]]++; + for( ; qR < que[i].R; ++qR) + ans += cnt[idx[qR + 1]]++; + for( ; qL < que[i].L; ++qL) + ans -= --cnt[idx[qL]]; + for( ; qR > que[i].R; --qR) + ans -= --cnt[idx[qR]]; + res[que[i].idx] = ans; + } + for(int i = 1; i <= q; ++i) + printf("%lld\n", res[i]); + return 0; +} diff --git a/self_training/2018-07-19/A.cpp b/self_training/2018-07-19/A.cpp new file mode 100644 index 00000000..2a00153e --- /dev/null +++ b/self_training/2018-07-19/A.cpp @@ -0,0 +1,20 @@ +#include +using namespace std; +typedef long long LL; +const int maxn = (int)2e3 + 9, mod = (int)1e9 + 7; +int n, m, fact[maxn], iact[maxn]; +int main() { + iact[1] = 1; + for(int i = 2; i < maxn; ++i) + iact[i] = mod - (int)(mod / i * (LL)iact[mod % i] % mod); + fact[0] = iact[0] = 1; + for(int i = 1; i < maxn; ++i) { + fact[i] = (LL)fact[i - 1] * i % mod; + iact[i] = (LL)iact[i - 1] * iact[i] % mod; + } + while(scanf("%d%d", &n, &m) == 2) { + int ways = (LL)fact[n + m + 1] * fact[n + m] % mod * iact[n + 1] % mod * iact[n] % mod * iact[m + 1] % mod * iact[m] % mod; + printf("%d\n", ways); + } + return 0; +} diff --git a/self_training/2018-07-19/B.cpp b/self_training/2018-07-19/B.cpp new file mode 100644 index 00000000..b4a15a53 --- /dev/null +++ b/self_training/2018-07-19/B.cpp @@ -0,0 +1,20 @@ +#include +using namespace std; +typedef long long LL; +const int maxn = 11; +int n, mod, f[maxn]; +inline int sum2(int x) { + return x * (x - 1LL) / 2 % mod; +} +int main() { + while(scanf("%d%d", &n, &mod) == 2) { + f[1] = 0; + f[0] = f[2] = 1 % mod; + for(int i = 3; i <= n; ++i) { + f[i] = ((i - 1LL) * (f[i - 1] + f[i - 2]) - (LL)sum2(i - 1) * f[i - 3]) % mod; + f[i] < 0 && (f[i] += mod); + } + printf("%d\n", f[n]); + } + return 0; +} diff --git a/self_training/2018-07-19/C.cpp b/self_training/2018-07-19/C.cpp new file mode 100644 index 00000000..6344480f --- /dev/null +++ b/self_training/2018-07-19/C.cpp @@ -0,0 +1,51 @@ +#include +using namespace std; +typedef long long LL; +const int maxn = (int)51, maxm = (int)1e3 + 1, mod = (int)1e9 + 7; +int n, m, tot, pw[maxn]; +LL vec[maxm]; +pair seq[maxm]; +int main() { + pw[0] = 1; + for(int i = 1; i < maxn; ++i) + (pw[i] = pw[i - 1] << 1) >= mod && (pw[i] -= mod); + while(scanf("%d%d", &n, &m) == 2) { + memset(vec, 0, m * sizeof(LL)); + for(int i = 0; i < n; ++i) { + static char buf[maxm]; + scanf("%s", buf); + for(int j = 0; j < m; ++j) + vec[j] = vec[j] << 1 | (buf[j] == '1'); + } + sort(vec, vec + m); + tot = 0; + for(int i = 0; i < m; ++i) + if(!tot || seq[tot - 1].first < vec[i]) + seq[tot++] = make_pair(vec[i], 1); + else + ++seq[tot - 1].second; + int zero = seq[0].first ? 0 : seq[0].second; + int way0 = zero * zero * zero; + int way1 = 0; + for(int i = zero > 0; i < tot; ++i) { + int tmp = seq[i].second + zero; + way1 += tmp * tmp * tmp - way0; + } + int way2 = 0; + for(int i = zero > 0; i < tot; ++i) + for(int j = i + 1; j < tot; ++j) { + way2 += 3 * seq[i].second * seq[j].second * ((zero << 1) + seq[i].second + seq[j].second); + LL msk = seq[i].first ^ seq[j].first; + int k = lower_bound(seq, seq + tot, make_pair(msk, 0)) - seq; + if(j < k && seq[k].first == msk) + way2 += 6 * seq[i].second * seq[j].second * seq[k].second; + } + int way3 = m * m * m - way0 - way1 - way2; + int ans = (LL)way0 * pw[n] % mod; + n > 0 && (ans = (ans + (LL)way1 * pw[n - 1]) % mod); + n > 1 && (ans = (ans + (LL)way2 * pw[n - 2]) % mod); + n > 2 && (ans = (ans + (LL)way3 * pw[n - 3]) % mod); + printf("%d\n", ans); + } + return 0; +} diff --git a/self_training/2018-07-19/F.cpp b/self_training/2018-07-19/F.cpp new file mode 100644 index 00000000..35b78e8d --- /dev/null +++ b/self_training/2018-07-19/F.cpp @@ -0,0 +1,78 @@ +#include +using namespace std; +typedef long long LL; +const int maxn = (int)1e3 + 9, mod = (int)1e9 + 7; +int tot, pr[maxn], d[maxn], iact[maxn], deg, val[maxn]; +int n, a[maxn], ans; +int mod_pow(int x, int k) { + int ret = 1; + for( ; k > 0; k >>= 1, x = (LL)x * x % mod) + (k & 1) && (ret = (LL)ret * x % mod); + return ret; +} +int value(int x) { + if(x <= deg) + return val[x]; + static int pre[maxn], suf[maxn]; + pre[0] = x % mod; + for(int i = 1; i <= deg; ++i) + pre[i] = (LL)(x - i) * pre[i - 1] % mod; + suf[deg] = (x - deg) % mod; + for(int i = deg - 1; i >= 0; --i) + suf[i] = (LL)(x - i) * suf[i + 1] % mod; + int ret = 0; + for(int i = 0; i <= deg; ++i) { + int tmp = (LL)val[i] * iact[i] % mod * iact[deg - i] % mod; + i > 0 && (tmp = (LL)tmp * pre[i - 1] % mod); + i < deg && (tmp = (LL)tmp * suf[i + 1] % mod); + ((deg - i) & 1) && tmp && (tmp = mod - tmp); + (ret += tmp) >= mod && (ret -= mod); + } + return ret; +} +int main() { + iact[1] = 1; + for(int i = 2; i < maxn; ++i) { + iact[i] = mod - (int)(mod / i * (LL)iact[mod % i] % mod); + if(!d[i]) + pr[tot++] = d[i] = i; + for(int j = 0, k; (k = i * pr[j]) < maxn; ++j) { + d[k] = pr[j]; + if(d[i] == pr[j]) + break; + } + } + iact[0] = 1; + for(int i = 1; i < maxn; ++i) + iact[i] = (LL)iact[i - 1] * iact[i] % mod; + while(scanf("%d", &n) == 1) { + a[0] = ans = 1; + for(int i = 1; i <= n; ++i) { + scanf("%d", a + i); + ans = (LL)ans * a[i] % mod; + } + sort(a + 1, a + n + 1); + ans = (LL)ans * a[n] % mod; + for(int i = 1, prd = 1; i <= n; prd = (LL)prd * a[i++] % mod) { + if(a[i - 1] == a[i]) + continue; + int k = n - i + 1; + deg = k + 1; + memset(val, 0, (deg + 1) * sizeof(int)); + val[1] = 1; + for(int j = 2; j <= deg; ++j) + if(d[j] == j) + val[j] = mod_pow(j, k); + else + val[j] = (LL)val[d[j]] * val[j / d[j]] % mod; + for(int j = 1; j <= deg; ++j) + (val[j] += val[j - 1]) >= mod && (val[j] -= mod); + int coeff = value(a[i - 1] - 1) - value(a[i] - 1); + coeff < 0 && (coeff += mod); + ans = (ans + (LL)coeff * prd) % mod; + } + printf("%d\n", ans); + } + + return 0; +} diff --git a/self_training/2018-07-19/G.cpp b/self_training/2018-07-19/G.cpp new file mode 100644 index 00000000..a4ecc4d4 --- /dev/null +++ b/self_training/2018-07-19/G.cpp @@ -0,0 +1,115 @@ +#include +using namespace std; +typedef long long LL; +const int maxn = 51, maxd = 12, maxs = 1 << 12 | 1, mod = (int)1e9 + 7; +int n, m, q, inv[maxn]; +vector e[maxn]; +struct Info { + int val, ways; + void upd(Info const &t) { + val > t.val && (val = t.val, ways = 0); + val == t.val && (ways += t.ways) >= mod && (ways -= mod); + } + Info merge(Info const &t) const { + int fir = val + t.val; + if(fir >= n) + return (Info){n, 0}; + int sec = (LL)ways * t.ways % mod; + return (Info){fir, sec}; + } +} f[maxs][maxn], g[maxs][maxn]; +int tot, vis[maxn], dis[maxn], ord[maxn]; +vector adt[maxn]; +int main() { + inv[1] = 1; + for(int i = 2; i < maxn; ++i) + inv[i] = mod - (int)(mod / i * (LL)inv[mod % i] % mod); + while(scanf("%d%d%d", &n, &q, &m) == 3) { + for(int i = 0; i < n; ++i) + vector().swap(e[i]); + while(q--) { + int u, v; + scanf("%d%d", &u, &v); + --u; + --v; + e[u].push_back(v); + e[v].push_back(u); + } + for(int i = 0; i < 1 << m; ++i) + for(int j = 0; j < n; ++j) + f[i][j] = g[i][j] = (Info){n, 0}; + for(int i = 0; i < n; ++i) { + int cur = i < m ? 1 << i : 0; + f[cur][i] = g[cur][i] = (Info){0, 1}; + } + memset(vis, -1, n * sizeof(int)); + for(int msk = 0; msk < 1 << m; ++msk) { + for(int i = 0; i < n; ++i) { + int cur = i < m ? 1 << i : 0; + if(cur && !(msk & cur)) + continue; + int rem = msk ^ cur, lbt = rem & -rem; + if(rem) + for(int msk2 = (rem - 1) & rem; msk2; msk2 = (msk2 - 1) & rem) + if(msk2 & lbt) { + int u = msk2 | cur, v = (rem ^ msk2) | cur; + g[msk][i].upd(f[u][i].merge(g[v][i])); + } + if(g[msk][i].val < n) + adt[g[msk][i].val].push_back(i); + } + tot = 0; + for(int i = 0, j = 0; i < n; ++i) { + for(auto u : adt[i]) { + if(vis[u] != msk) { + vis[u] = msk; + dis[u] = n; + } + if(dis[u] > i) { + dis[u] = i; + ord[tot++] = u; + } + } + vector().swap(adt[i]); + for( ; j < tot; ++j) { + int u = ord[j]; + if(dis[u] > i) + break; + for(auto v : e[u]) { + int cur = v < m ? 1 << v : 0; + if(cur && !(msk & cur)) + continue; + if(vis[v] != msk) { + vis[v] = msk; + dis[v] = n; + } + if(dis[v] > dis[u] + 1) { + dis[v] = dis[u] + 1; + ord[tot++] = v; + } + } + } + } + for(int i = 0; i < tot; ++i) { + int u = ord[i]; + for(auto v : e[u]) { + int msk2 = msk | (v < m ? 1 << v : 0); + Info tmp = g[msk][u]; + ++tmp.val; + f[msk2][v].upd(tmp); + g[msk2][v].upd(tmp); + } + } + } + Info ans = (Info){n, 0}; + for(int i = 0; i < n; ++i) + ans.upd(g[(1 << m) - 1][i]); + if(ans.val < n) { + ans.ways = (LL)ans.ways * inv[ans.val + 1] % mod; + printf("%d\n", ans.ways); + } else { + puts("0"); + } + } + return 0; +} diff --git a/self_training/2018-07-19/I.cpp b/self_training/2018-07-19/I.cpp new file mode 100644 index 00000000..3e0e2a79 --- /dev/null +++ b/self_training/2018-07-19/I.cpp @@ -0,0 +1,85 @@ +#include +using namespace std; +typedef long long LL; +const int maxn = (int)3e5 + 9; +int m, len, rem[maxn], sa[maxn], rk[maxn], ht[maxn], f[maxn], g[maxn], c[maxn]; +char pat[maxn], perm[3], buf[maxn]; +LL ans; +void suffix_array() { + int i, j, k, *x = f, *y = g; + m = 256; + memset(c, 0, m * sizeof(int)); + for(i = 0; i < len; ++i) + ++c[x[i] = buf[i]]; + for(i = 1; i < m; ++i) + c[i] += c[i - 1]; + for(i = len - 1; i >= 0; --i) + sa[--c[x[i]]] = i; + for(k = 1; k <= len; k <<= 1, m = j) { + j = 0; + for(i = len - k; i < len; ++i) + y[j++] = i; + for(i = 0; i < len; ++i) + if(sa[i] >= k) + y[j++] = sa[i] - k; + memset(c, 0, m * sizeof(int)); + for(i = 0; i < len; ++i) + ++c[x[y[i]]]; + for(i = 0; i < m; ++i) + c[i] += c[i - 1]; + for(i = len - 1; i >= 0; --i) + sa[--c[x[y[i]]]] = y[i]; + swap(x, y); + j = 1; + x[sa[0]] = 0; + for(i = 1; i < len; ++i) + x[sa[i]] = y[sa[i - 1]] == y[sa[i]] && y[sa[i - 1] + k] == y[sa[i] + k] ? j - 1 : j++; + x[len] = j; + if(j >= len) + break; + } + for(i = 0; i < len; ++i) + rk[sa[i]] = i; + for(i = k = 0; i < len; ++i) { + if(k) --k; + if(!rk[i]) { + ht[rk[i]] = 0; + continue; + } + for(j = sa[rk[i] - 1]; buf[i + k] == buf[j + k]; ++k); + ht[rk[i]] = k; + } +// for(int i = 0; i < len; ++i) +// printf("#%d %d (%d, %d): %s\n", i + 1, sa[i], ht[i], rem[sa[i]], buf + sa[i]); +} +int main() { + while(scanf("%d", &m) == 1) { + len = ans = 0; + scanf("%s", pat); + for(int i = 0, j = 0; i < m; i = j) { + for( ; j < m && pat[i] == pat[j]; ++j); + ans = max(ans, (LL)(j - i)); + } + ans *= 3; + for(int i = 0; i < 3; ++i) + perm[i] = 'a' + i; + do { + for(int i = 0; i < m; ++i) { + rem[len] = m - i; + buf[len++] = perm[pat[i] - 'a']; + } + rem[len] = 0; + buf[len++] = '$'; + } while(next_permutation(perm, perm + 3)); + buf[--len] = '\0'; + suffix_array(); + for(int i = 0; i < len; ++i) { + int low = ht[i], upp = rem[sa[i]]; + ans += max(upp - low, 0); + } +// assert(ans % 6 == 0); + ans /= 6; + printf("%lld\n", ans); + } + return 0; +} diff --git a/self_training/2018-07-19/J.cpp b/self_training/2018-07-19/J.cpp new file mode 100644 index 00000000..7acbbcdb --- /dev/null +++ b/self_training/2018-07-19/J.cpp @@ -0,0 +1,55 @@ +#include +using namespace std; +const int maxn = (int)1e5 + 1; +int n, m, a[maxn], fir[maxn], bit[maxn], ans[maxn]; +struct Query { + int idx, L, R; + bool operator < (Query const &t) const { + return R > t.R; + } +} que[maxn]; +void bit_add(int x, int v) { + for( ; x <= n; x += x & -x) + bit[x] += v; +} +int bit_sum(int x) { + int ret = 0; + for( ; x > 0; x -= x & -x) + ret += bit[x]; + return ret; +} +int main() { + while(scanf("%d%d", &n, &m) == 2) { + memset(bit + 1, 0, n * sizeof(int)); + memset(fir + 1, 0, n * sizeof(int)); + for(int i = 1; i <= n; ++i) { + scanf("%d", a + i); + if(!fir[a[i]]) { + fir[a[i]] = i; + bit_add(i, 1); + } + } + for(int i = 1; i <= m; ++i) { + int L, R; + scanf("%d%d", &L, &R); + if(L >= R) + L = R = 1; + que[i] = (Query){i, L, R}; + } + sort(que + 1, que + m + 1); + for(int i = 1, qR = n + 1; i <= m; ++i) { + for( ; qR > que[i].R; --qR) { + int &cur = fir[a[qR - 1]]; + if(cur > 1) { + bit_add(cur, -1); + cur = 1; + bit_add(cur, 1); + } + } + ans[que[i].idx] = bit_sum(que[i].L); + } + for(int i = 1; i <= m; ++i) + printf("%d\n", ans[i]); + } + return 0; +} diff --git a/self_training/2018-07-21/B.cpp b/self_training/2018-07-21/B.cpp new file mode 100644 index 00000000..afdae480 --- /dev/null +++ b/self_training/2018-07-21/B.cpp @@ -0,0 +1,60 @@ +#include +using namespace std; +typedef long long LL; +const int maxn = (int)1e5 + 1; +const LL INF = 0x3f3f3f3f3f3f3f3fLL; +int n, tot, a[maxn], b[maxn], fa[maxn], idx[maxn]; +LL f[maxn][2]; +vector e[maxn], p[maxn]; +bool sp[maxn]; +void dfs(int u) { + f[u][0] = 0; + f[u][1] = sp[u] ? 0 : INF; + for(int v : e[u]) { + dfs(v); + LL tmp = min(f[v][1], f[v][0] + a[v] - b[v]); + f[u][1] = min(f[u][1] + tmp, min(f[u][0], f[u][1]) + min(f[v][0], f[v][1]) + a[v]); + f[u][0] += tmp; + } +} +int main() { + scanf("%d", &n); + for(int i = 1; i <= n; ++i) + scanf("%d", a + i); + for(int i = 1; i <= n; ++i) + scanf("%d", b + i); + for(int i = 1; i <= n; ++i) + scanf("%d", fa + i); + for(int i = 1; i <= n; ++i) { + if(idx[i]) + continue; + int j = i; + ++tot; + for( ; !idx[j]; idx[j] = tot, j = fa[j]); + if(idx[j] && idx[j] != tot) { + --tot; + for(int k = i; idx[k] != idx[j]; idx[k] = idx[j], k = fa[k]); + } + } + for(int i = 1; i <= n; ++i) { +// printf("%d%c", idx[i], " \n"[i == n]); + p[idx[i]].push_back(i); + } + LL ans = 0; + for(int i = 1; i <= tot; ++i) { + int u = *p[i].begin(); + for( ; idx[u] == i; idx[u] = -1, u = fa[u]); + for(int v : p[i]) + if(u != v) + e[fa[v]].push_back(v); + dfs(u); + LL tmp = min(f[u][1], f[u][0] + a[u] - b[u]); + sp[u] = 1; + sp[fa[u]] = 1; + dfs(u); + tmp = min(tmp, min(f[u][0], f[u][1]) + a[u]); + ans += tmp; + } + printf("%lld\n", ans); + return 0; +} diff --git a/self_training/2018-07-21/E.cpp b/self_training/2018-07-21/E.cpp new file mode 100644 index 00000000..8ec7aae5 --- /dev/null +++ b/self_training/2018-07-21/E.cpp @@ -0,0 +1,116 @@ +#include +using namespace std; +typedef long long LL; +const int maxn = (int)1e5 + 1, maxd = 11, mod = (int)1e9 + 7; +int n, m, fa[maxn], f[maxn][maxd]; +vector e[maxn]; +void merge(int Fu[], int Fv[]) { // (FF - 1) = (F - 1) G + assert(Fu[0] == 1); + assert(Fv[0] == 1); + for(int i = maxd - 1; i > 0; --i) { + LL tmp = Fu[i]; + for(int j = 1; j < i; ++j) + tmp += (LL)Fv[j] * Fu[i - j]; + // tmp <= (maxd - 2) (mod - 1)^2 + (mod - 1) < 2^63 + Fu[i] = tmp % mod; + } +} +void split(int Fu[], int Fv[]) { // (F - 1) = (FF - 1) G + assert(Fu[0] == 1); + assert(Fv[0] == 1); + for(int i = 1; i < maxd; ++i) { + LL tmp = Fu[i]; + for(int j = 1; j < i; ++j) + tmp -= (LL)Fv[j] * Fu[i - j]; + // tmp >= -(maxd - 2) (mod - 1)^2 >= -2^63 + (Fu[i] = tmp % mod) < 0 && (Fu[i] += mod); + } +} +void dfs(int u) { + for(int v : e[u]) { + dfs(v); + merge(f[u], f[v]); + } +} +int mod_inv(int x) { + int y = mod, u = 1, v = 0, r; + while(y) { + r = x / y; + swap(x, y); + y -= r * x; + swap(u, v); + v -= r * u; + } + assert(x == 1); + return u < 0 ? u + mod : u; +} +int main() { + scanf("%d%d", &n, &m); + for(int i = 1; i <= n; ++i) { + f[i][0] = 1; + scanf("%d", f[i] + 1); + } + for(int i = 2; i <= n; ++i) { + scanf("%d", fa + i); + e[fa[i]].push_back(i); + } + dfs(1); + while(m--) { + int x, y, z; + scanf("%d%d%d", &x, &y, &z); + static int pre[maxd], cur[maxd], tmp[maxd], arr[maxd]; + if(x == 0) { + memcpy(pre, f[y], sizeof pre); + int prd = (LL)z * mod_inv(f[y][1]) % mod; + for(int i = 1; i < maxd; ++i) + f[y][i] = (LL)f[y][i] * prd % mod; + memcpy(cur, f[y], sizeof cur); + for(int i = 2, u = fa[y]; i < maxd && u; ++i, u = fa[u]) { + memcpy(tmp, f[u], sizeof tmp); + split(f[u], pre); + merge(f[u], cur); + swap(pre, tmp); + memcpy(cur, f[u], sizeof cur); + } + } else if(x == 1) { + memcpy(pre, f[y], sizeof pre); + memset(cur, 0, sizeof cur); + cur[0] = 1; + for(int i = 2, u = fa[y]; i < maxd && u; ++i, u = fa[u]) { + memcpy(tmp, f[u], sizeof tmp); + split(f[u], pre); + merge(f[u], cur); + swap(pre, tmp); + memcpy(cur, f[u], sizeof cur); + } + fa[y] = z; + memset(pre, 0, sizeof pre); + pre[0] = 1; + memcpy(cur, f[y], sizeof cur); + for(int i = 2, u = fa[y]; i < maxd && u; ++i, u = fa[u]) { + memcpy(tmp, f[u], sizeof tmp); + split(f[u], pre); + merge(f[u], cur); + swap(pre, tmp); + memcpy(cur, f[u], sizeof cur); + } + } else { + int ans = f[y][z]; + memcpy(pre, f[y], sizeof pre); + memset(cur, 0, sizeof cur); + cur[0] = 1; + for(int i = 2, u = fa[y]; i < maxd && u; ++i, u = fa[u]) { + memcpy(tmp, f[u], sizeof tmp); + memcpy(arr, f[u], sizeof arr); + (ans += arr[z]) >= mod && (ans -= mod); + split(arr, pre); + merge(arr, cur); + (ans -= arr[z]) < 0 && (ans += mod); + swap(pre, tmp); + swap(cur, arr); + } + printf("%d\n", ans); + } + } + return 0; +} diff --git a/self_training/2018-07-21/G.cpp b/self_training/2018-07-21/G.cpp new file mode 100644 index 00000000..57105518 --- /dev/null +++ b/self_training/2018-07-21/G.cpp @@ -0,0 +1,83 @@ +#include +using namespace std; +typedef long long LL; +const int maxn = (int)5e5 + 1, INF = 0x3f3f3f3f; +int n; +LL m, x[maxn], c[maxn], s[maxn], ans; +int main() { + scanf("%d%lld", &n, &m); + m >>= 1; + for(int i = 1; i <= n; ++i) + scanf("%lld", x + i); + for(int i = 1; i <= n; ++i) { + scanf("%lld", c + i); + s[i] = s[i - 1] + c[i]; + } + int L = 1, R = 1; + LL cnt = c[1], cost = 0; + for(int i = 1; i <= n; ++i) { + for( ; L < i && cost > m; ++L) { + cnt -= c[L]; + cost -= (x[i] - x[L]) * c[L]; + } + for( ; R + 1 <= n; ++R) { + LL adt = (x[R + 1] - x[i]) * c[R + 1]; + if(cost + adt > m) + break; + cnt += c[R + 1]; + cost += adt; + } + for( ; L < i && R + 1 <= n; ) { + LL lft = x[i] - x[L], rht = x[R + 1] - x[i]; + if(lft < rht) + break; + cnt -= c[L]; + cost -= lft * c[L]; + ++L; + for( ; R + 1 <= n && lft >= rht; ) { + LL adt = rht * c[R + 1]; + if(cost + adt > m) + break; + cnt += c[R + 1]; + cost += adt; + ++R; + rht = x[R + 1] - x[i]; + } + } +// fprintf(stderr, "%d: [%d, %d]: %lld %lld\n", i, L, R, cnt, cost); + LL lft = L > 1 ? x[i] - x[L - 1] : INF, lc = L > 1 ? c[L - 1] : -1; + LL rht = R + 1 <= n ? x[R + 1] - x[i] : INF, rc = R + 1 <= n ? c[R + 1] : -1; + LL ctr = cnt, rem = m - cost; + if(lft <= rht && lft < INF) { + LL tmp = min(lc, rem / lft); + ctr += tmp; + lc -= tmp; + rem -= lft * tmp; + } + if(lft >= rht && rht < INF) { + LL tmp = min(rc, rem / rht); + ctr += tmp; + rc -= tmp; + rem -= rht * tmp; + } + if(lft >= rht && lft < INF) { + LL tmp = min(lc, rem / lft); + ctr += tmp; + lc -= tmp; + rem -= lft * tmp; + } + if(lft <= rht && rht < INF) { + LL tmp = min(rc, rem / rht); + ctr += tmp; + rc -= tmp; + rem -= rht * tmp; + } + ans = max(ans, ctr); + if(i + 1 <= n) { + cost += (s[i] - s[L - 1]) * (x[i + 1] - x[i]); + cost -= (s[R] - s[i]) * (x[i + 1] - x[i]); + } + } + printf("%lld\n", ans); + return 0; +} diff --git a/self_training/2018-07-21/I.cpp b/self_training/2018-07-21/I.cpp new file mode 100644 index 00000000..22782474 --- /dev/null +++ b/self_training/2018-07-21/I.cpp @@ -0,0 +1,20 @@ +#include +using namespace std; +const int maxn = (int)1e5 + 1; +int n, m, ans; +bool row[maxn], col[maxn]; +int main() { + scanf("%d%d", &n, &m); + while(m--) { + int x, y; + scanf("%d%d", &x, &y); + row[x] = col[y] = 1; + } + for(int i = 1; i <= n; ++i) { + !row[i] && (++ans); + !col[i] && (++ans); + } + (n & 1) && !row[(n + 1) >> 1] && !col[(n + 1) >> 1] && (--ans); + printf("%d\n", ans); + return 0; +} diff --git a/self_training/2018-07-21/K.cpp b/self_training/2018-07-21/K.cpp new file mode 100644 index 00000000..2d5e8eb2 --- /dev/null +++ b/self_training/2018-07-21/K.cpp @@ -0,0 +1,69 @@ +#include +using namespace std; +typedef long long LL; +const int maxs = (int)1e6 + 1, mod1 = (int)1e9 + 7, mod2 = (int)1e9 + 9; +int n, m, p, q, nxt[maxs], seq1[maxs], seq2[maxs]; +char buf[maxs]; +int main() { + scanf("%d%d", &n, &m); + for(int i = 0, idx = 0; i < n; ++i) { + int h1 = 0, h2 = 0; + scanf("%s", buf + idx); + for(int j = 0; j < m; ++j, ++idx) { + int o = buf[idx] - 'a'; + h1 = (29LL * h1 + o) % mod1; + h2 = (29LL * h2 + o) % mod2; + } + seq1[i] = h1; + seq2[i] = h2; + } + for(int i = 1, j = 0; i < n; ++i) { + for( ; j && !(seq1[j] == seq1[i] && seq2[j] == seq2[i]); j = nxt[j]); + nxt[i + 1] = seq1[j] == seq1[i] && seq2[j] == seq2[i] ? ++j : 0; + } + p = n - nxt[n]; + memset(seq1, 0, m * sizeof(int)); + memset(seq2, 0, m * sizeof(int)); + for(int i = 0, idx = 0; i < n; ++i) + for(int j = 0; j < m; ++j, ++idx) { + int o = buf[idx] - 'a'; + seq1[j] = (29LL * seq1[j] + o) % mod1; + seq2[j] = (29LL * seq2[j] + o) % mod2; + } + for(int i = 1, j = 0; i < m; ++i) { + for( ; j && !(seq1[j] == seq1[i] && seq2[j] == seq2[i]); j = nxt[j]); + nxt[i + 1] = seq1[j] == seq1[i] && seq2[j] == seq2[i] ? ++j : 0; + } + q = m - nxt[m]; + for(int i = 0, idx = 0; i < n; ++i) { + int *seq = seq1, *res = seq2; + int *que = nxt, L = 0, R = 0; + for(int j = 0; j < m; ++j, ++idx) { + scanf("%d", seq + j); + for( ; L < R && j - que[L] >= q; ++L); + for( ; L < R && seq[j] >= seq[que[R - 1]]; --R); + que[R++] = j; + res[idx] = seq[que[L]]; +// printf("%d%c", res[idx], " \n"[j == m - 1]); + } + } + int ans = ~0u >> 1; + for(int j = q - 1; j < m; ++j) { + int *seq = seq1, *res = seq2; + int *que = nxt, L = 0, R = 0; + for(int i = 0, idx = j; i < n; ++i, idx += m) { + seq[i] = res[idx]; +// printf("%d%c", seq[i], " \n"[i == n - 1]); + } + for(int i = 0; i < n; ++i) { + for( ; L < R && i - que[L] >= p; ++L); + for( ; L < R && seq[i] >= seq[que[R - 1]]; --R); + que[R++] = i; + i >= p - 1 && (ans = min(ans, seq[que[L]])); +// printf("%d%c", seq[que[L]], " \n"[i == n - 1]); + } + } +// printf("%d %d %d\n", ans, p, q); + printf("%lld\n", ans * (p + 1LL) * (q + 1)); + return 0; +} diff --git a/self_training/2018-07-22/A.cpp b/self_training/2018-07-22/A.cpp new file mode 100644 index 00000000..50240af1 --- /dev/null +++ b/self_training/2018-07-22/A.cpp @@ -0,0 +1,31 @@ +#include +using namespace std; +const int maxn = (int)1e3 + 1, mod = (int)1e9 + 7; +int t, n, f[2][3]; +char buf[3][maxn]; +int main() { + scanf("%d", &t); + for(int Case = 1; Case <= t; ++Case) { + scanf("%d", &n); + for(int i = 0; i < 3; ++i) + scanf("%s", buf[i]); + int cur = 0, pre = 1; + f[cur][0] = 1; + f[cur][1] = f[cur][2] = 0; + for(int i = 0; i < n; ++i) { + pre ^= 1; + cur ^= 1; + f[cur][0] = f[cur][1] = f[cur][2] = 0; + if(buf[0][i] == '.' && buf[1][i] == '.') { + (f[cur][0] += f[pre][1]) >= mod && (f[cur][0] -= mod); + (f[cur][1] += f[pre][0]) >= mod && (f[cur][1] -= mod); + } + if(buf[1][i] == '.' && buf[2][i] == '.') { + (f[cur][1] += f[pre][2]) >= mod && (f[cur][1] -= mod); + (f[cur][2] += f[pre][1]) >= mod && (f[cur][2] -= mod); + } + } + printf("Case #%d: %d\n", Case, f[cur][2]); + } + return 0; +} diff --git a/self_training/2018-07-22/B.cpp b/self_training/2018-07-22/B.cpp new file mode 100644 index 00000000..b3aed07b --- /dev/null +++ b/self_training/2018-07-22/B.cpp @@ -0,0 +1,60 @@ +#include +using namespace std; +const int maxn = (int)2e3 + 1; +int t, n, m, lch[maxn], rch[maxn], seq[2][maxn], dsu[maxn]; +void pfs(int u) { + if(!u) + return; + seq[0][++seq[0][0]] = u; + pfs(lch[u]); + pfs(rch[u]); +} +void dfs(int u) { + if(!u) + return; + dfs(lch[u]); + dfs(rch[u]); + seq[1][++seq[1][0]] = u; +} +int dsu_find(int x) { + return dsu[x] < 0 ? x : (dsu[x] = dsu_find(dsu[x])); +} +bool dsu_merge(int u, int v) { + u = dsu_find(u); + v = dsu_find(v); + if(u == v) + return 0; + if(dsu[u] < dsu[v]) + swap(u, v); + dsu[v] -= dsu[u] == dsu[v]; + dsu[u] = v; + return 1; +} +int main() { + scanf("%d", &t); + for(int Case = 1; Case <= t; ++Case) { + scanf("%d%d", &n, &m); + for(int i = 1; i <= n; ++i) { + scanf("%d%d", lch + i, rch + i); + dsu[i] = -1; + } + seq[0][0] = seq[1][0] = 0; + pfs(1); + dfs(1); + int cnt = n; + for(int i = 1; i <= n; ++i) + cnt -= dsu_merge(seq[0][i], seq[1][i]); + if(cnt < m) { + printf("Case #%d: Impossible\n", Case); + } else { + for(int i = 1; i <= n; ++i) + if(i == dsu_find(i)) + dsu[i] = -((--cnt) % m + 1); + printf("Case #%d:", Case); + for(int i = 1; i <= n; ++i) + printf(" %d", -dsu[dsu_find(i)]); + printf("\n"); + } + } + return 0; +} diff --git a/self_training/2018-07-22/C.cpp b/self_training/2018-07-22/C.cpp new file mode 100644 index 00000000..b7ad9c79 --- /dev/null +++ b/self_training/2018-07-22/C.cpp @@ -0,0 +1,51 @@ +#include +using namespace std; +typedef long long LL; +const int maxn = (int)3e3 + 1, mod = (int)1e9 + 7; +int t, n, m, low[maxn], upp[maxn], ivs[maxn], f[2][maxn]; +pair seq[maxn]; +inline int mod_inv(int x) { + return x <= 1 ? x : mod - (int)(mod / x * (LL)mod_inv(mod % x) % mod); +} +int main() { + scanf("%d", &t); + for(int Case = 1; Case <= t; ++Case) { + scanf("%d%d", &n, &m); + for(int i = 1; i < n; ++i) { + scanf("%d%d", low + i, upp + i); + ivs[i] = mod_inv(upp[i] - low[i] + 1); + } + low[n] = upp[n] = (int)1e6 + 1; + ivs[n] = 1; + for(int i = 1; i <= m; ++i) + scanf("%d%d", &seq[i].first, &seq[i].second); + sort(seq + 1, seq + m + 1); + int tp = 0; + for(int i = 1; i <= m; ++i) + if(!tp || seq[tp].first < seq[i].first) + seq[++tp] = seq[i]; + else + seq[tp].second = seq[i].second; + m = tp; + int cur = 0, pre = 1; + memset(f[cur], 0, (n + 1) * sizeof(int)); + f[cur][0] = 1; + for(int i = 1; i <= m; ++i) { + pre ^= 1; + cur ^= 1; + int pos = seq[i].first, hh = seq[i].second, sum = f[pre][0]; + f[cur][0] = 0; + for(int j = 1; j <= n; ++j) { + int succ = (LL)(max(min(hh, upp[j]), low[j] - 1) - low[j] + 1) * ivs[j] % mod; + int fail = 1 - succ; fail < 0 && (fail += mod); + f[cur][j] = j >= pos ? (LL)(sum + f[pre][j]) * fail % mod : 0; + sum = ((LL)sum * succ + f[pre][j]) % mod; + printf("%d%c", f[cur][j], " \n"[j == n]); + } + } + int ans = 1 - f[cur][n]; ans < 0 && (ans += mod); + printf("Case #%d: %d\n", Case, ans); + } + return 0; +} + diff --git a/self_training/2018-08-04/1001.cpp b/self_training/2018-08-04/1001.cpp new file mode 100644 index 00000000..322fce1c --- /dev/null +++ b/self_training/2018-08-04/1001.cpp @@ -0,0 +1,44 @@ +#include +using namespace std; +const int maxn = (int)1e3 + 1, maxd = 10, maxs = 1 << maxd | 1; +int t, n, m, low, cnt[maxs], ans; +char buf[maxd + 1]; +void dfs(int sel, int msk, int dist) { + if(!sel) { + ans += dist >= low; + return; + } + dfs(sel >> 1, msk, dist); + msk ^= sel; + for(int i = msk; ; i = (i - 1) & msk) { + dist -= cnt[i] * cnt[i | sel]; + cnt[i] += cnt[i | sel]; + if(!i) + break; + } + dfs(sel >> 1, msk, dist); + for(int i = msk; ; i = (i - 1) & msk) { + cnt[i] -= cnt[i | sel]; + if(!i) + break; + } +} +int main() { + scanf("%d", &t); + for(int Case = 1; Case <= t; ++Case) { + scanf("%d%d%d", &n, &m, &low); + int all = 1 << m, dist = n * (n - 1) / 2; + memset(cnt, 0, all * sizeof(int)); + while(n--) { + int msk = 0; + scanf("%s", buf); + for(int i = 0; i < m; ++i) + msk = msk << 1 | (buf[i] == 'A'); + dist -= cnt[msk]++; + } + ans = 0; + dfs(all >> 1, all - 1, dist); + printf("Case #%d: %d\n", Case, ans); + } + return 0; +} diff --git a/self_training/2018-08-04/1002.cpp b/self_training/2018-08-04/1002.cpp new file mode 100644 index 00000000..e3bb8f43 --- /dev/null +++ b/self_training/2018-08-04/1002.cpp @@ -0,0 +1,26 @@ +#include +using namespace std; +const int maxn = (int)1e5 + 1, maxd = 26; +int t, n, q, cnt[maxn][maxd + 1]; +char buf[maxn]; +int main() { + scanf("%d", &t); + for(int Case = 1; Case <= t; ++Case) { + scanf("%d%d%s", &n, &q, buf); + for(int i = 1; i <= n; ++i) { + memcpy(cnt[i], cnt[i - 1], sizeof(cnt[0])); + ++cnt[i][buf[i - 1] - 'A']; + } + printf("Case #%d:\n", Case); + while(q--) { + int L, R; + scanf("%d%d", &L, &R); + for(int i = 0; i < maxd; ++i) + if(cnt[L - 1][i] != cnt[R][i]) { + printf("%d\n", cnt[R][i] - cnt[L - 1][i]); + break; + } + } + } + return 0; +} diff --git a/self_training/2018-08-04/1003.cpp b/self_training/2018-08-04/1003.cpp new file mode 100644 index 00000000..3d3714d8 --- /dev/null +++ b/self_training/2018-08-04/1003.cpp @@ -0,0 +1,69 @@ +#include +using namespace std; +typedef long long LL; +const int maxn = 201; +const LL INF = 0x3f3f3f3f3f3f3f3fLL; +int t, n; +LL w[maxn][maxn], match[maxn], sx[maxn], sy[maxn], slack[maxn], pre[maxn]; +bool vis[maxn]; +void augment(int rt) { + int cur = 0; + match[0] = rt; + memset(vis, 0, (n + 1) * sizeof(bool)); + memset(slack, 0x3f, (n + 1) * sizeof(LL)); + do { + int nxt, u = match[cur]; + LL dt = INF; + vis[cur] = 1; + for(int v = 1; v <= n; ++v) { + if(vis[v]) + continue; + LL tmp = sx[u] + sy[v] - w[u][v]; + if(slack[v] > tmp) { + slack[v] = tmp; + pre[v] = cur; + } + if(slack[v] < dt) { + dt = slack[v]; + nxt = v; + } + } + for(int v = 0; v <= n; ++v) + if(vis[v]) { + sx[match[v]] -= dt; + sy[v] += dt; + } else + slack[v] -= dt; + cur = nxt; + } while(match[cur]); + for(int v = cur; v; v = pre[v]) + match[v] = match[pre[v]]; +} +LL KM() { + for(int i = 1; i <= n; ++i) { + LL dt = 0; + for(int j = 1; j <= n; ++j) + dt = max(dt, w[i][j]); + sx[i] = dt; + sy[i] = match[i] = 0; + } + for(int i = 1; i <= n; ++i) + augment(i); + LL cost = 0; + for(int i = 1; i <= n; ++i) + cost += sx[i] + sy[i]; + return cost; +} +int main() { + scanf("%d", &t); + for(int Case = 1; Case <= t; ++Case) { + scanf("%d", &n); + for(int i = 1; i <= n; ++i) + for(int j = 1; j <= n; ++j) { + scanf("%I64d", w[i] + j); + w[i][j] = -w[i][j]; + } + printf("Case #%d: %I64d\n", Case, -KM()); + } + return 0; +} diff --git a/self_training/2018-08-04/1004-TLE.cpp b/self_training/2018-08-04/1004-TLE.cpp new file mode 100644 index 00000000..0a746b60 --- /dev/null +++ b/self_training/2018-08-04/1004-TLE.cpp @@ -0,0 +1,72 @@ +#pragma GCC optimize(3) +#pragma GCC optimize("Ofast") +#include +using namespace std; +const int maxn = (int)2e3 + 1; +int t, n, ord[maxn << 1 | 1], ans; +char out[maxn]; +struct Point { + int x, y; + void read() { + scanf("%d%d", &x, &y); + } + Point operator - (Point const &t) const { + return (Point){x - t.x, y - t.y}; + } + int det(Point const &t) const { + return x * t.y - y * t.x; + } + int site() const { + return x < 0 || (!x && y < 0); + } + bool operator < (Point const &t) const { + int s1 = site(), s2 = t.site(); + if(s1 != s2) + return s1 < s2; + return det(t) > 0; + } +} a[maxn], b[maxn]; +bool cmp(int const &u, int const &v) { + return b[u] < b[v]; +} +int main() { + scanf("%d", &t); + for(int Case = 1; Case <= t; ++Case) { + scanf("%d", &n); + n <<= 1; + ans = 0; + for(int i = 0; i < n; ++i) { + a[i].read(); + out[i] = 'B'; + } + out[n] = '\0'; + for(int i = 0; i < n; ++i) { + int tot = 0; + for(int j = 0; j < n; ++j) { + if(i == j) + continue; + b[j] = a[j] - a[i]; + ord[tot++] = j; + } + sort(ord, ord + tot, cmp); + for(int j = 0; j < tot; ++j) + ord[tot + j] = ord[j]; + for(int j = 0, k = 0; j < tot; ++j) { + for( ; k < tot + j && b[ord[j]].det(b[ord[k]]) >= 0; ++k); + if(k - j == (n >> 1) && ord[j] < i) { + ans += 2; + static char tmp[maxn]; + for(int x = 0; x < n; ++x) + tmp[x] = 'B'; + for(int x = j; x < k; ++x) + tmp[ord[x]] = 'A'; + tmp[n] = '\0'; + if(strcmp(out, tmp) > 0) + strcpy(out, tmp); + } + } + } + printf("Case #%d: %d %s\n", Case, ans, out); + } + return 0; +} diff --git a/self_training/2018-08-04/1005.cpp b/self_training/2018-08-04/1005.cpp new file mode 100644 index 00000000..0e2344a6 --- /dev/null +++ b/self_training/2018-08-04/1005.cpp @@ -0,0 +1,37 @@ +#include +using namespace std; +const int maxn = (int)1e4 + 1, mod = (int)1e9 + 7; +int t, n, perm[maxn], f[maxn], bit[maxn], ans[maxn]; +int main() { + scanf("%d", &t); + for(int Case = 1; Case <= t; ++Case) { + scanf("%d", &n); + for(int i = 1; i <= n; ++i) { + scanf("%d", perm + i); + f[i] = 1; + } + memset(ans + 1, 0, n * sizeof(int)); + ans[1] = n; + for(int i = 2; i <= n; ++i) { + memset(bit + 1, 0, n * sizeof(int)); + bool chk = 0; + for(int j = 1; j <= n; ++j) { + int o = perm[j], cur = 0; + for(int x = o - 1; x > 0; x -= x & -x) + (cur += bit[x]) >= mod && (cur -= mod); + for(int x = o; x <= n; x += x & -x) + (bit[x] += f[o]) >= mod && (bit[x] -= mod); + f[o] = cur; + chk |= f[o]; + (ans[i] += f[o]) >= mod && (ans[i] -= mod); + } + if(!chk) + break; + } + printf("Case #%d:", Case); + for(int i = 1; i <= n; ++i) + printf(" %d", ans[i]); + putchar('\n'); + } + return 0; +} diff --git a/self_training/2018-08-04/1006.cpp b/self_training/2018-08-04/1006.cpp new file mode 100644 index 00000000..4fb48c7b --- /dev/null +++ b/self_training/2018-08-04/1006.cpp @@ -0,0 +1,67 @@ +#include +using namespace std; +const int maxn = 101, INF = 0x3f3f3f3f; +int t, n, m, dsu[maxn], ans[maxn]; +struct Edge { + int u, v, w, typ; + void read() { + char ch; + scanf("%d%d%d %c", &u, &v, &w, &ch); + typ = ch == 'R' ? 1 : (ch == 'G' ? 2 : 4); + } + bool operator < (Edge const &t) const { + return w < t.w; + } +} e[maxn]; +int dsu_find(int x) { + return dsu[x] < 0 ? x : (dsu[x] = dsu_find(dsu[x])); +} +bool dsu_merge(int u, int v) { + u = dsu_find(u); + v = dsu_find(v); + if(u == v) + return 0; + if(dsu[u] < dsu[v]) + swap(u, v); + dsu[v] -= dsu[u] == dsu[v]; + dsu[u] = v; + return 1; +} +void solve(int msk) { + int cnt = 0, sum = 0; + static bool vis[maxn]; + memset(vis, 0, m * sizeof(bool)); + memset(dsu + 1, -1, n * sizeof(int)); + for(int i = 0; i < m; ++i) + if((e[i].typ & msk) && dsu_merge(e[i].u, e[i].v)) { + ++cnt; + sum += e[i].w; + vis[i] = 1; + } + if(cnt < n - 1) + return; + (ans[cnt] > sum) && (ans[cnt] = sum); + for(int i = 0; i < m; ++i) + if(!vis[i]) { + ++cnt; + sum += e[i].w; + vis[i] = 1; + (ans[cnt] > sum) && (ans[cnt] = sum); + } +} +int main() { + scanf("%d", &t); + for(int Case = 1; Case <= t; ++Case) { + scanf("%d%d", &n, &m); + for(int i = 0; i < m; ++i) + e[i].read(); + sort(e, e + m); + memset(ans + 1, 0x3f, m * sizeof(int)); + solve(3); + solve(6); + printf("Case #%d:\n", Case); + for(int i = 1; i <= m; ++i) + printf("%d\n", ans[i] < INF ? ans[i] : -1); + } + return 0; +} diff --git a/self_training/2018-08-11/1001.cpp b/self_training/2018-08-11/1001.cpp new file mode 100644 index 00000000..06cb72fb --- /dev/null +++ b/self_training/2018-08-11/1001.cpp @@ -0,0 +1,17 @@ +#include +using namespace std; +const int maxn = (int)1e3 + 1; +int n, a[maxn]; +int main() { + while(scanf("%d", &n) == 1) { + for(int i = 0; i < n; ++i) + scanf("%d", a + i); + sort(a, a + n); + int ans = -1; + for(int i = 2; i < n; ++i) + if(a[i] < a[i - 1] + a[i - 2]) + ans = max(ans, a[i] + a[i - 1] + a[i - 2]); + printf("%d\n", ans); + } + return 0; +} diff --git a/self_training/2018-08-11/1002.cpp b/self_training/2018-08-11/1002.cpp new file mode 100644 index 00000000..0089b469 --- /dev/null +++ b/self_training/2018-08-11/1002.cpp @@ -0,0 +1,50 @@ +#include +#include +using namespace std; +const int maxn = (int)2e5 + 1; +int n, m; +list que[maxn], rev[maxn]; +int main() { + while(scanf("%d%d", &n, &m) == 2) { + while(m--) { + int typ, x, y, z; + scanf("%d%d%d", &typ, &x, &y); + if(typ == 1) { + scanf("%d", &z); + if(y == 1) { + que[x].push_back(z); + rev[x].push_front(z); + } else { + que[x].push_front(z); + rev[x].push_back(z); + } + } else if(typ == 2) { + if(que[x].empty()) { + puts("-1"); + } else if(y == 1) { + printf("%d\n", que[x].back()); + que[x].pop_back(); + rev[x].pop_front(); + } else { + printf("%d\n", que[x].front()); + que[x].pop_front(); + rev[x].pop_back(); + } + } else { + scanf("%d", &z); + if(z == 1) { + que[x].splice(que[x].end(), rev[y]); + rev[x].splice(rev[x].begin(), que[y]); + } else { + que[x].splice(que[x].end(), que[y]); + rev[x].splice(rev[x].begin(), rev[y]); + } + } + } + for(int i = 1; i <= n; ++i) { + list().swap(que[i]); + list().swap(rev[i]); + } + } + return 0; +} diff --git a/self_training/2018-08-11/1003.cpp b/self_training/2018-08-11/1003.cpp new file mode 100644 index 00000000..d2ee017f --- /dev/null +++ b/self_training/2018-08-11/1003.cpp @@ -0,0 +1,55 @@ +#include +using namespace std; +const int maxn = (int)1e4 + 1; +int n, m, tot, f[maxn], g[maxn]; +pair seq[maxn]; +char buf[maxn]; +int main() { + while(scanf("%d%d%s", &n, &m, buf) == 3) { + tot = 0; + for(int i = 0; i < n; ++i) + if(!tot || buf[i] != seq[tot - 1].first) + seq[tot++] = make_pair(buf[i], 1); + else + ++seq[tot - 1].second; + memset(f, 0, (m + 1) * sizeof(int)); + memset(g, 0, (m + 1) * sizeof(int)); + { + bool fir = 0; + f[0] = seq[0].first == '1' ? seq[0].second : 0; + if(seq[0].first == '1') + fir = 1; + if(tot > 2) { + sort(seq + 1, seq + tot - 1); + for(int i = tot - 2, j = 0; i > 0 && j + 1 + fir <= m; --i) + if(seq[i].first == '1') { + f[j + 1 + fir] = f[j] + seq[i].second; + j += 1 + fir; + fir = 1; + } + } + for(int i = 1; i <= m; ++i) + f[i] = max(f[i], f[i - 1]); + } + { + bool fir = 0; + g[0] = 0; + if(tot > 2) { + sort(seq + 1, seq + tot - 1); + for(int i = tot - 2, j = 0; i > 0 && j + 1 + fir <= m; --i) + if(seq[i].first == '1') { + g[j + 1 + fir] = g[j] + seq[i].second; + j += 1 + fir; + fir = 1; + } + } + for(int i = 1; i <= m; ++i) + g[i] = max(g[i], g[i - 1]); + } + int ans = max(f[m], g[m]); + if(tot > 1 && seq[tot - 1].first == '1' && m > 0) + ans = max(ans, max(f[m - 1], g[m - 1]) + seq[tot - 1].second); + printf("%d\n", ans); + } + return 0; +} diff --git a/self_training/2018-08-11/1004.cpp b/self_training/2018-08-11/1004.cpp new file mode 100644 index 00000000..0e7581f2 --- /dev/null +++ b/self_training/2018-08-11/1004.cpp @@ -0,0 +1,41 @@ +#include +using namespace std; +typedef long long LL; +const int maxn = (int)2e3 + 1, mod = 998244353; +int fact[maxn], iact[maxn], pw2[maxn], c[maxn][maxn]; +int n, m, f[maxn], g[maxn]; +int main() { + iact[1] = 1; + for(int i = 2; i < maxn; ++i) + iact[i] = mod - (int)(mod / i * (LL)iact[mod % i] % mod); + fact[0] = iact[0] = pw2[0] = c[0][0] = 1; + for(int i = 1; i < maxn; ++i) { + fact[i] = (LL)fact[i - 1] * i % mod; + iact[i] = (LL)iact[i - 1] * iact[i] % mod; + (pw2[i] = pw2[i - 1] << 1) >= mod && (pw2[i] -= mod); + c[i][0] = c[i][i] = 1; + for(int j = 1; j < i; ++j) + (c[i][j] = c[i - 1][j - 1] + c[i - 1][j]) >= mod && (c[i][j] -= mod); + } + while(scanf("%d%d", &n, &m) == 2) { + memset(f, 0, (n + 1) * sizeof(int)); + for(int i = 0; i <= n; ++i) + f[i] = (LL)fact[(n << 1) - i] * iact[i] % mod * fact[n] % mod * iact[n - i] % mod * pw2[i] % mod; + for(int i = n; i >= 0; --i) { + LL tmp = 0; + for(int j = n; j > i; --j) { + tmp += (LL)c[j][i] * g[j]; + if(tmp < 0) + tmp = (unsigned long long)tmp % mod; + } + (g[i] = (f[i] - tmp) % mod) < 0 && (g[i] += mod); + } + int ans = 0, prd = 1; + for(int i = 0; i <= n; ++i, prd = (LL)prd * m % mod) { + ans = (ans + (LL)g[i] * prd) % mod; +// printf("%d%c", g[i], " \n"[i == n]); + } + printf("%d\n", ans); + } + return 0; +} diff --git a/self_training/2018-08-11/1005.cpp b/self_training/2018-08-11/1005.cpp new file mode 100644 index 00000000..c28588fe --- /dev/null +++ b/self_training/2018-08-11/1005.cpp @@ -0,0 +1,76 @@ +#include +using namespace std; +const int maxn = (int)1e5 + 1; +int n, m, a[maxn], s[maxn], b[maxn], dsu[maxn], ans[maxn], cur; +set sp[maxn], lft[maxn], rht[maxn]; +inline int dsu_find(int x) { + return dsu[x] < 0 ? x : (dsu[x] = dsu_find(dsu[x])); +} +void set_merge(set &src, set &dst) { + if(src.size() > dst.size()) + src.swap(dst); + for(set::iterator it = src.begin(); it != src.end(); ++it) + dst.insert(*it); + set().swap(src); +} +void dsu_merge(int u, int v) { + u = dsu_find(u); + v = dsu_find(v); + if(sp[u].size() < sp[v].size()) { + for(set::iterator it = sp[u].begin(); it != sp[u].end(); ++it) { + int val = s[*it - 1] + m; + set::iterator jt = rht[v].lower_bound(val); + if(jt != rht[v].end()) + cur = min(cur, *jt - val); + if(jt != rht[v].begin()) + cur = min(cur, val - *(--jt)); + } + } else { + for(set::iterator it = sp[v].begin(); it != sp[v].end(); ++it) { + int val = s[*it] - m; + set::iterator jt = lft[u].lower_bound(val); + if(jt != lft[u].end()) + cur = min(cur, *jt - val); + if(jt != lft[u].begin()) + cur = min(cur, val - *(--jt)); + } + } + if(dsu[u] < dsu[v]) + swap(u, v); + dsu[v] -= dsu[u] == dsu[v]; + dsu[u] = v; + set_merge(sp[u], sp[v]); + set_merge(lft[u], lft[v]); + set_merge(rht[u], rht[v]); +} +int main() { + while(scanf("%d%d", &n, &m) == 2) { + for(int i = 1; i <= n; ++i) { + scanf("%d", a + i); + s[i] = s[i - 1] + a[i]; + dsu[i] = -1; + } + for(int i = 1; i <= n; ++i) + scanf("%d", b + i); + cur = ~0u >> 1; + for(int i = n; i >= 1; --i) { + int u = b[i]; + sp[u].insert(u); + lft[u].insert(s[u - 1]); + rht[u].insert(s[u]); + cur = min(cur, abs(a[u] - m)); + if(u > 1 && sp[dsu_find(u - 1)].size() > 0) + dsu_merge(u - 1, u); + if(u < n && sp[dsu_find(u + 1)].size() > 0) + dsu_merge(u, u + 1); + ans[i] = cur; + } + for(int i = 1; i <= n; ++i) { + printf("%d\n", ans[i]); + set().swap(sp[i]); + set().swap(lft[i]); + set().swap(rht[i]); + } + } + return 0; +} diff --git a/self_training/2018-08-18/1001.cpp b/self_training/2018-08-18/1001.cpp new file mode 100644 index 00000000..46ab70bd --- /dev/null +++ b/self_training/2018-08-18/1001.cpp @@ -0,0 +1,42 @@ +#include +using namespace std; +typedef long long LL; +const int maxn = (int)1e5 + 1, INF = ~0u >> 1; +int t, n, a[maxn], cur, pre = 1; +LL f[2][2], g[2][2]; +vector e[maxn]; +int main() { + scanf("%d", &t); + while(t--) { + scanf("%d", &n); + for(int i = 2, p; i <= n; ++i) { + scanf("%d", &p); + e[p].push_back(i); + } + for(int i = 1; i <= n; ++i) + scanf("%d", a + i); + f[cur][0] = max(a[1], 0); + f[cur][1] = 0; + g[cur][0] = min(a[1], 0); + g[cur][1] = 0; + for(int i = 1; i <= n; ++i) { + int sz = e[i].size(); + if(!sz) + continue; + swap(cur, pre); + sort(e[i].begin(), e[i].end(), [&](int const &u, int const &v) { + return a[u] < a[v]; + }); + f[cur][0] = f[pre][0] + max(a[e[i][sz - 1]], 0); + f[cur][1] = f[pre][1] + max(a[e[i][sz - 1]], 0); + g[cur][0] = g[pre][0] + min(a[e[i][0]], 0); + g[cur][1] = g[pre][1] + min(a[e[i][0]], 0); + if(sz > 1) { + f[cur][1] = max(f[cur][1], f[cur][0] + max(a[e[i][sz - 2]], 0)); + g[cur][1] = min(g[cur][1], g[cur][0] + min(a[e[i][1]], 0)); + } + vector().swap(e[i]); + } + printf("%I64d %I64d\n", max(f[cur][0], f[cur][1]), min(g[cur][0], g[cur][1])); + } +} diff --git a/self_training/2018-08-18/1002.cpp b/self_training/2018-08-18/1002.cpp new file mode 100644 index 00000000..63e09810 --- /dev/null +++ b/self_training/2018-08-18/1002.cpp @@ -0,0 +1,41 @@ +#include +using namespace std; +typedef long long LL; +const int maxn = 101, mod = (int)1e9 + 7; +int t, n, pL[maxn], pR[maxn]; +inline int mod_inv(int x) { + return x <= 1 ? x : mod - (int)(mod / x * (LL)mod_inv(mod % x) % mod); +} +int solve(int adt, int lim) { + int ret = 1; + for(int i = 0; i < n; ++i) { + int low = pL[i], upp = min(pR[i], lim); + if(low > upp) + return 0; + int tmp = (upp - low + 1LL) * (adt - low + adt - upp) / 2 % mod; + ret = (LL)ret * tmp % mod; + } + return ret; +} +int main() { + scanf("%d", &t); + while(t--) { + scanf("%d", &n); + int low = 0, upp = 0, dom = 1; + for(int i = 0; i < n; ++i) { + scanf("%d%d", pL + i, pR + i); + low = max(low, pL[i]); + upp = max(upp, pR[i]); + dom = dom * (pR[i] - pL[i] + 1LL) % mod; + } + int ans = 0; + for( ; low <= upp; ++low) { + int val = solve(low + 1, low) - solve(low + 1, low - 1); + val < 0 && (val += mod); + (ans += val) >= mod && (ans -= mod); + } + ans = (LL)ans * mod_inv(dom) % mod; + printf("%d\n", ans); + } + return 0; +} diff --git a/self_training/2018-08-18/1003.cpp b/self_training/2018-08-18/1003.cpp new file mode 100644 index 00000000..8ea3ddec --- /dev/null +++ b/self_training/2018-08-18/1003.cpp @@ -0,0 +1,59 @@ +#include +using namespace std; +typedef long long LL; +const int maxn = (int)1e5 + 1, mod = (int)1e9 + 7; +int t, n, m, a[maxn], que[maxn]; +vector e[maxn]; +bool vis[maxn]; +int main() { + scanf("%d", &t); + while(t--) { + scanf("%d%d", &n, &m); + for(int i = 1; i <= n; ++i) + scanf("%d", a + i); + while(m--) { + int u, v; + scanf("%d%d", &u, &v); + e[u].push_back(v); + e[v].push_back(u); + } + int ans = 0; + for(int i = 1; i <= n; ++i) { + if(vis[i]) + continue; + int tot = 0; + vis[i] = 1; + que[tot++] = i; + int mx = 1; + for(int j = 0; j < tot; ++j) { + int u = que[j]; + for( ; mx <= a[u]; mx <<= 1); + for(int v : e[u]) + if(!vis[v]) { + vis[v] = 1; + que[tot++] = v; + } + } + sort(que, que + tot, [&](int const &u, int const &v) { + return a[u] < a[v]; + }); + for(int j = 1; j < mx; j <<= 1) { + int cnt = 0, sum = 0; + for(int k = 0; k < tot; ++k) { + int u = que[k]; + if(a[u] & j) { + sum = (sum + (LL)cnt * a[u]) % mod; + ++cnt; + } + } + ans = (ans + (LL)sum * j) % mod; + } + } + printf("%d\n", ans); + for(int i = 1; i <= n; ++i) { + vector().swap(e[i]); + vis[i] = 0; + } + } + return 0; +} diff --git a/self_training/2018-08-18/1004.cpp b/self_training/2018-08-18/1004.cpp new file mode 100644 index 00000000..b122cc85 --- /dev/null +++ b/self_training/2018-08-18/1004.cpp @@ -0,0 +1,49 @@ +#include +using namespace std; +typedef long long LL; +const int maxn = (int)1e3 + 1, maxm = 5, mod = (int)1e9 + 7; +int t, n, m, path[maxm]; +vector > sp; +vector ways, p[maxn][maxm]; +void dfs(vector *arr, int dep) { + if(dep == m) { + sp.push_back(vector(path, path + m)); + return; + } + for(int x : arr[dep]) { + path[dep] = x; + dfs(arr, dep + 1); + } +} +int main() { + scanf("%d", &t); + while(t--) { + scanf("%d%d", &m, &n); + for(int i = 0; i < m; ++i) + for(int j = 0; j < n; ++j) { + int x; + scanf("%d", &x); + p[--x][i].push_back(j); + } + for(int i = 0; i < n; ++i) + dfs(p[i], 0); + int ans = 0, sz = sp.size(); + vector(sz, 1).swap(ways); + sort(sp.begin(), sp.end()); + for(int i = 0; i < sz; ++i) { + for(int j = 0; j < i; ++j) { + bool chk = 1; + for(int k = 0; chk && k < m; ++k) + chk &= sp[j][k] < sp[i][k]; + chk && (ways[i] += ways[j]) >= mod && (ways[i] -= mod); + } + (ans += ways[i]) >= mod && (ans -= mod); + } + printf("%d\n", ans); + vector >().swap(sp); + for(int i = 0; i < n; ++i) + for(int j = 0; j < m; ++j) + vector().swap(p[i][j]); + } + return 0; +} diff --git a/self_training/2018-08-18/1005.cpp b/self_training/2018-08-18/1005.cpp new file mode 100644 index 00000000..312efeb3 --- /dev/null +++ b/self_training/2018-08-18/1005.cpp @@ -0,0 +1,56 @@ +#include +using namespace std; +const int maxn = 21, maxm = 401, maxd = 1 << 7 | 1, maxs = 400 << 7 | 1, maxt = 400, INF = ~0u >> 1; +int t, n, m, q, mat[maxn][maxn], tot, val[maxm], col[maxm], f[maxs], que[maxs]; +int main() { + srand(time(0)); + scanf("%d", &t); + while(t--) { + scanf("%d%d%d", &n, &m, &q); + tot = 0; + memset(col, -1, sizeof col); + for(int i = 0; i < n; ++i) + for(int j = 0; j < m; ++j) { + scanf("%d", mat[i] + j); + if(mat[i][j] > 0 && col[mat[i][j]] == -1) { + col[mat[i][j]] = 0; + val[tot++] = mat[i][j]; + } + } + int ans = INF; + col[0] = 0; + for(int o = 0; o < maxt; ++o) { + for(int i = 0; i < tot; ++i) + col[val[i]] = 1 << (rand() % q); + memset(f, -1, ((n * m) << q) * sizeof(int)); + int tot = 0; + for(int i = 0; i < n; ++i) + for(int j = 0; j < m; ++j) + if(mat[i][j] != -1) { + int s = ((i * m + j) << q) | col[mat[i][j]]; + f[s] = 0; + que[tot++] = s; + } + for(int i = 0; i < tot; ++i) { + int udx = que[i], ux = (udx >> q) / m, uy = (udx >> q) % m, us = udx & ((1 << q) - 1); + if(us == (1 << q) - 1) + ans = min(ans, f[udx]); + for(int dx = -1; dx <= 1; ++dx) + for(int dy = -1; dy <= 1; ++dy) { + if((dx != 0) == (dy != 0)) + continue; + int vx = ux + dx, vy = uy + dy; + if(vx < 0 || vx >= n || vy < 0 || vy >= m || mat[vx][vy] == -1) + continue; + int vs = us | col[mat[vx][vy]], vdx = ((vx * m + vy) << q) | vs; + if(f[vdx] == -1) { + f[vdx] = f[udx] + 1; + que[tot++] = vdx; + } + } + } + } + printf("%d\n", ans < INF ? ans : -1); + } + return 0; +} diff --git a/self_training/2018-08-18/1006.cpp b/self_training/2018-08-18/1006.cpp new file mode 100644 index 00000000..3dc944ca --- /dev/null +++ b/self_training/2018-08-18/1006.cpp @@ -0,0 +1,76 @@ +#include +using namespace std; +typedef long long LL; +const int maxn = (int)2e3 + 1, mod = 998244353; +int t; +struct Poly { + int deg, coeff[maxn]; + void read() { + scanf("%d", °); + for(int i = 0; i <= deg; ++i) + scanf("%d", coeff + i); + } +} A, B, C, D; +inline int mod_inv(int x) { + return x <= 1 ? x : mod - (int)(mod / x * (LL)mod_inv(mod % x) % mod); +} +void poly_norm(Poly &A) { + int ivs = mod_inv(A.coeff[A.deg]); + for(int i = 0; i <= A.deg; ++i) + A.coeff[i] = (LL)A.coeff[i] * ivs % mod; +} +void poly_dif(Poly A, Poly &B) { // B = A' + if(!A.deg) { + B.deg = -1; + return; + } + B.deg = A.deg - 1; + for(int i = 0; i <= B.deg; ++i) + B.coeff[i] = (i + 1LL) * A.coeff[i + 1] % mod; + poly_norm(B); +} +void poly_div(Poly A, Poly B, Poly &D, Poly &R) { // A = B D + R + R = A; + D.deg = A.deg - B.deg; + int ivs = mod_inv(B.coeff[B.deg]); + for(int i = R.deg; i >= B.deg; --i) { + int prd = D.coeff[i - B.deg] = (LL)R.coeff[i] * ivs % mod; + for(int j = 0; j <= B.deg; ++j) + (R.coeff[i - j] = (R.coeff[i - j] - (LL)prd * B.coeff[B.deg - j]) % mod) < 0 && (R.coeff[i - j] += mod); + } + for( ; ~R.deg && !R.coeff[R.deg]; --R.deg); +} +void poly_gcd(Poly A, Poly B, Poly &R) { // R = gcd(A, B) + R = B; + while(A.deg != -1) { + if(A.deg < R.deg) + swap(A, R); + poly_div(A, R, B, A); + } + poly_norm(R); +} +int main() { + scanf("%d", &t); + while(t--) { + A.read(); + B = A; + while(1) { + poly_dif(B, C); + poly_gcd(B, C, D); + if(D.deg == 0) + break; + poly_div(B, D, B, C); + } + printf("%d\n", B.deg); + for(int i = 1; B.deg != 0; ++i) { + poly_div(A, B, A, D); + poly_gcd(A, B, D); + if(D.deg < B.deg) { + poly_div(B, D, C, B); + printf("%d %d\n", C.coeff[0] ? mod - C.coeff[0] : 0, i); + B = D; + } + } + } + return 0; +} diff --git a/self_training/2018-09-01/B.cpp b/self_training/2018-09-01/B.cpp new file mode 100644 index 00000000..8c7e49b5 --- /dev/null +++ b/self_training/2018-09-01/B.cpp @@ -0,0 +1,44 @@ +#include +using namespace std; +const int maxn = (int)1e5 + 1, maxm = (int)1e2 + 1; +typedef long long LL; +LL t, n, m, q, h[maxm][maxn], top, hh[maxn], no[maxn], sum, ans; +char str[maxm][maxn]; +int main() { + scanf("%lld", &t); + for(LL Case = 1; Case <= t; ++Case) { + scanf("%lld%lld%lld", &n, &m, &q); + for(LL i = 1; i <= m; ++i) + memset(str[i] + 1, 0x3f, n * sizeof(char)); + while(q--) { + LL x, y; + scanf("%lld%lld", &y, &x); + str[x][y] = 0; + } + for(LL i = 1; i <= m; ++i) + for(LL j = 1; j <= n; ++j) + h[i][j] = str[i][j] == str[i - 1][j] ? h[i - 1][j] + 1 : 1; + ans = 0; + for(LL i = 1; i <= m; ++i) + for(LL j = 1; j <= n; ++j) { + if(str[i][j] != str[i][j - 1]) { + top = 0; + no[top] = j - 1; + sum = 0; + } + if(!str[i][j]) + continue; + while(top > 0 && h[i][j] <= hh[top]) { + sum -= hh[top] * (no[top] - no[top - 1]); + --top; + } + hh[++top] = h[i][j]; + no[top] = j; + sum += hh[top] * (no[top] - no[top - 1]); + ans += sum; + } + printf("Case #%lld: %lld\n", Case, ans); + } + return 0; +} + diff --git a/self_training/2018-09-01/C.cpp b/self_training/2018-09-01/C.cpp new file mode 100644 index 00000000..fe1b76cb --- /dev/null +++ b/self_training/2018-09-01/C.cpp @@ -0,0 +1,70 @@ +#include +using namespace std; +const int maxn = 201, maxm = (int)2e4 + 1, maxv = 13; +int t, n, m, a[maxm], b[maxn][maxv]; +inline int getLow(int b[]) { + for(int i = 0; i < maxv; ++i) + if(b[i]) + return i; + return -1; +} +inline int getNext(int b[], int v) { + if(v == maxv - 1) + return -1; + if(b[v + 1]) + return v + 1; + return b[maxv - 1] ? maxv - 1 : -1; +} +int main() { + scanf("%d", &t); + for(int Case = 1; Case <= t; ++Case) { + scanf("%d%d", &n, &m); + for(int i = 0; i < m; ++i) { + scanf("%d", a + i); + (a[i] -= 3) < 0 && (a[i] += 13); + } + reverse(a, a + m); + memset(b, 0, n * sizeof b[0]); + for(int i = 0; i < n; ++i) + for(int j = 0; m && j < 5; ++j) + ++b[i][a[--m]]; + int cur = 0, val = getLow(b[cur]); + --b[cur][val]; + while(1) { + bool chk = 1; + for(int i = 1; i < n; ++i) { + int tmp = getNext(b[(cur + i) % n], val); + if(tmp != -1) { + chk = 0; + cur = (cur + i) % n; + val = tmp; + --b[cur][tmp]; + break; + } + } + if(getLow(b[cur]) == -1) + break; + if(chk) { + for(int i = 0; m > 0 && i < n; ++i) + ++b[(cur + i) % n][a[--m]]; + val = getLow(b[cur]); + --b[cur][val]; + if(getLow(b[cur]) == -1) + break; + } + } + printf("Case #%d:\n", Case); + for(int i = 0; i < n; ++i) { + int sum = 0; + for(int j = 0; j < maxv; ++j) { + int k = j + 3 <= 13 ? j + 3 : j + 3 - 13; + sum += k * b[i][j]; + } + if(sum) + printf("%d\n", sum); + else + puts("Winner"); + } + } + return 0; +} diff --git a/self_training/2018-09-01/G.cpp b/self_training/2018-09-01/G.cpp new file mode 100644 index 00000000..ce4c911b --- /dev/null +++ b/self_training/2018-09-01/G.cpp @@ -0,0 +1,87 @@ +#include +using namespace std; +const int maxn = (int)1e5 + 1, INF = 0x3f3f3f3f; +int n, m, seg[maxn << 1 | 1], tot, f[maxn], g[maxn], cnt, pL[maxn], pR[maxn]; +inline int seg_idx(int L, int R) { + return (L + R) | (L < R); +} +void seg_build(int L, int R) { + if(L == R) { + scanf("%d", seg + seg_idx(L, R)); + } else { + int M = (L + R) >> 1; + seg_build(L, M); + seg_build(M + 1, R); + seg[seg_idx(L, R)] = min(seg[seg_idx(L, M)], seg[seg_idx(M + 1, R)]); + } +} +void seg_get(int L, int R, int l, int r) { + if(l <= L && R <= r) { + pL[cnt] = L; + pR[cnt++] = R; + } else { + int M = (L + R) >> 1; + if(l <= M) + seg_get(L, M, l, r); + if(M < r) + seg_get(M + 1, R, l, r); + } +} +int seg_find(int L, int R, int v) { + while(L < R) { + int M = (L + R) >> 1; + if(seg[seg_idx(L, M)] <= v) { + R = M; + } else { + L = M + 1; + } + } + return L; +} +void seg_upd(int L, int R, int x, int v) { + if(L == R) { + seg[seg_idx(L, R)] = v; + } else { + int M = (L + R) >> 1; + if(x <= M) + seg_upd(L, M, x, v); + else + seg_upd(M + 1, R, x, v); + seg[seg_idx(L, R)] = min(seg[seg_idx(L, M)], seg[seg_idx(M + 1, R)]); + } +} +int main() { + scanf("%d%d", &n, &m); + seg_build(1, n); + while(seg[seg_idx(1, n)] < INF) { + int low = seg[seg_idx(1, n)]; + for( ; tot < maxn && g[tot] < low; ++tot) { + f[tot + 1] = f[tot]; + g[tot + 1] = g[tot] + m; + } + if(tot == maxn) + break; + int cur = 1; + while(seg[seg_idx(1, n)] <= g[tot]) { + cnt = 0; + seg_get(1, n, cur, n); + if(cur > 1) + seg_get(1, n, 1, cur - 1); + for(int i = 0; i < cnt; ++i) + if(seg[seg_idx(pL[i], pR[i])] <= g[tot]) { + int x = seg_find(pL[i], pR[i], g[tot]); + ++f[tot]; + g[tot] -= seg[seg_idx(x, x)]; + seg_upd(1, n, x, INF); + } + } + } + scanf("%d", &m); + while(m--) { + int x; + scanf("%d", &x); + x = min(x, tot); + printf("%d %d\n", f[x], g[x]); + } + return 0; +} diff --git a/self_training/2018-09-01/H.cpp b/self_training/2018-09-01/H.cpp new file mode 100644 index 00000000..07f5c8fd --- /dev/null +++ b/self_training/2018-09-01/H.cpp @@ -0,0 +1,70 @@ +#include +using namespace std; +const int maxn = (int)6e5 + 1, maxd = 30, maxs = maxn * maxd + 1; +int n, m, tot, dsu[maxn], rt[maxn]; +struct Trie { + int sz, ch[2]; +} e[maxs]; +int tr_upd(int rt, int v, int dep) { + int tr = rt; + if(!rt) { + tr = ++tot; + e[tr] = e[rt]; + } + ++e[tr].sz; + if(dep < maxd) { + int o = (v >> dep) & 1; + e[tr].ch[o] = tr_upd(e[tr].ch[o], v, dep + 1); + } + return tr; +} +int tr_mrg(int u, int v) { + if(!u || !v) + return u | v; + e[u].sz += e[v].sz; + e[u].ch[0] = tr_mrg(e[u].ch[0], e[v].ch[0]); + e[u].ch[1] = tr_mrg(e[u].ch[1], e[v].ch[1]); + return u; +} +void tr_inc(int rt) { + for( ; rt; rt = e[rt].ch[0]) + swap(e[rt].ch[0], e[rt].ch[1]); +} +int dsu_find(int u) { + return dsu[u] < 0 ? u : (dsu[u] = dsu_find(dsu[u])); +} +int main() { + scanf("%d%d", &n, &m); + for(int i = 1; i <= n; ++i) { + int x; + scanf("%d", &x); + dsu[i] = -1; + rt[i] = tr_upd(0, x, 0); + } + while(m--) { + int typ, x, y, z; + scanf("%d%d", &typ, &x); + if(typ == 1) { + scanf("%d", &y); + x = dsu_find(x); + y = dsu_find(y); + if(x != y) { + if(dsu[x] < dsu[y]) + swap(x, y); + dsu[y] -= dsu[x] == dsu[y]; + dsu[x] = y; + rt[y] = tr_mrg(rt[x], rt[y]); + } + } else if(typ == 2) { + tr_inc(rt[dsu_find(x)]); + } else { + scanf("%d%d", &y, &z); + x = dsu_find(x); + int u = rt[x]; + for(int i = 0; i < y; ++i) + u = e[u].ch[(z >> i) & 1]; + printf("%d\n", e[u].sz); + } + } + return 0; +} diff --git a/self_training/2018-09-15/C.cpp b/self_training/2018-09-15/C.cpp new file mode 100644 index 00000000..6d011ad3 --- /dev/null +++ b/self_training/2018-09-15/C.cpp @@ -0,0 +1,90 @@ +#include +using namespace std; +const int maxn = (int)1e5 + 1, maxm = maxn << 3 | 1; +int n, m, a[maxn], pL[maxn], pR[maxn], etot, ord[maxm], que[maxm]; +long long ans[maxn]; +struct Event { + int typ, x, y, v; +} eve[maxm]; +bool cmp(int const &u, int const &v) { + if(eve[u].x != eve[v].x) + return eve[u].x < eve[v].x; + return eve[u].typ < eve[v].typ; +} +void addEvent(int typ, int x, int y, int v) { + if(x < 0 || x > n || y < 0 || y > n || !v) + return; + ord[etot] = etot; + eve[etot++] = (Event){typ, x, y, v}; +} +void solve(int L, int R) { + if(L == R) + return; + int M = (L + R) >> 1; + solve(L, M); + solve(M + 1, R); + __int128 cnt[9] = {}; + for(int i = L, j = M + 1, k = 0; i <= M || j <= R; ) { + int u = ord[i], v = ord[j]; + if(j > R || (i <= M && eve[u].y <= eve[v].y)) { + if(!eve[u].typ) { + __int128 v0 = eve[u].v; cnt[0] += v0; + __int128 v1 = v0 * eve[u].x; cnt[1] += v1; + __int128 v2 = v0 * eve[u].y; cnt[2] += v2; + __int128 v3 = v1 * eve[u].x; cnt[3] += v3; + __int128 v4 = v2 * eve[u].y; cnt[4] += v4; + __int128 v5 = v1 * eve[u].y; cnt[5] += v5; + __int128 v6 = v5 * eve[u].x; cnt[6] += v6; + __int128 v7 = v5 * eve[u].y; cnt[7] += v7; + } + que[k++] = ord[i++]; + } else { + if(eve[v].typ) { + __int128 v7 = 1; + __int128 v6 = -1; + __int128 v5 = 2; + __int128 v4 = -eve[v].x - 1; + __int128 v3 = eve[v].y + 1; + __int128 v2 = v4 * (-eve[v].x + 1); + __int128 v1 = v3 * (-eve[v].y - 3); + __int128 v0 = v3 * (eve[v].x + 1) * (eve[v].y - eve[v].x + 2); + ans[eve[v].typ] += eve[v].v * (cnt[0] * v0 + cnt[1] * v1 + cnt[2] * v2 + cnt[3] * v3 + cnt[4] * v4 + cnt[5] * v5 + cnt[6] * v6 + cnt[7] * v7) / 2; + } + que[k++] = ord[j++]; + } + } + memcpy(ord + L, que, (R - L + 1) * sizeof(int)); +} +int main() { + static int stk[maxn]; + scanf("%d%d", &n, &m); + for(int i = 1, sz = 0; i <= n; ++i) { + scanf("%d", a + i); + for( ; sz && a[stk[sz - 1]] < a[i]; --sz); + pL[i] = (sz ? stk[sz - 1] : 0) + 1; + stk[sz++] = i; + } + for(int i = n, sz = 0; i >= 1; --i) { + for( ; sz && a[stk[sz - 1]] <= a[i]; --sz); + pR[i] = (sz ? stk[sz - 1] : n + 1) - 1; + stk[sz++] = i; + addEvent(0, pL[i], i, a[i]); + addEvent(0, pL[i], pR[i] + 1, -a[i]); + addEvent(0, i + 1, i, -a[i]); + addEvent(0, i + 1, pR[i] + 1, a[i]); + } + for(int i = 1; i <= m; ++i) { + int xL, xR, yL, yR; + scanf("%d%d%d%d", &xL, &xR, &yL, &yR); + addEvent(i, xR, yR, 1); + addEvent(i, xL - 1, yR, -1); + addEvent(i, xR, yL - 1, -1); + addEvent(i, xL - 1, yL - 1, 1); + } + sort(ord, ord + etot, cmp); + if(etot) + solve(0, etot - 1); + for(int i = 1; i <= m; ++i) + printf("%llu\n", ans[i]); + return 0; +} diff --git a/self_training/2018-09-15/D.java b/self_training/2018-09-15/D.java new file mode 100644 index 00000000..77a7979c --- /dev/null +++ b/self_training/2018-09-15/D.java @@ -0,0 +1,66 @@ +import java.math.*; +import java.util.*; + +public class Main { + static BigInteger choose(int n, int m) { + assert n >= m && m >= 0; + BigInteger fz = BigInteger.ONE, fm = BigInteger.ONE; + m = Math.min(m, n - m); + for(int i = 0; i < m; ++i) { + fz = fz.multiply(BigInteger.valueOf(n - i)); + fm = fm.multiply(BigInteger.valueOf(i + 1)); + } + return fz.divide(fm); + } + public static void main(String[] args) { + Scanner cin = new Scanner(System.in); + while(cin.hasNext()) { + int m = cin.nextInt(), n = cin.nextInt(); + BigInteger fz = BigInteger.ZERO, fm = choose(n - 1 + m, m); + for(int i = 1; i <= m; ++i) { + fz = fz.add(fm); + for(int j = 0, k = 0; j <= n && k <= m; ++j, k += i) { + BigInteger tp = choose(n, j).multiply(choose(n - 1 + m - k, m - k)); + if((j & 1) == 1) { + fz = fz.add(tp); + } else { + fz = fz.subtract(tp); + } + } + } + String ans = ""; + BigInteger[] tmp = fz.divideAndRemainder(fm); + ans += String.valueOf(tmp[0].intValue()) + "."; + fz = tmp[1]; + for(int i = 0; i < 4; ++i) { + tmp = fz.multiply(BigInteger.TEN).divideAndRemainder(fm); + ans += String.valueOf(tmp[0].intValue()); + fz = tmp[1]; + } + if(fz.compareTo(fm.subtract(fz)) >= 0) { + char[] buf = ans.toCharArray(); + int len = buf.length; + ++buf[len - 1]; + for(int i = len - 1; i > 0; --i) { + if(buf[i] == '.') + continue; + if(buf[i] > '9') { + buf[i] -= 10; + if(buf[i - 1] == '.') + ++buf[i - 2]; + else + ++buf[i - 1]; + } + } + if(buf[0] > '9') { + buf[0] -= 10; + ans = "1" + String.valueOf(buf); + } else { + ans = String.valueOf(buf); + } + } + System.out.println(ans); + } + cin.close(); + } +} diff --git a/self_training/2018-09-15/G.cpp b/self_training/2018-09-15/G.cpp new file mode 100644 index 00000000..b587955b --- /dev/null +++ b/self_training/2018-09-15/G.cpp @@ -0,0 +1,21 @@ +#include +using namespace std; +typedef long long LL; +const int maxn = (int)1e5 + 1, mod = (int)1e9 + 7; +int t; +char buf[maxn]; +int main() { + scanf("%d", &t); + while(t--) { + scanf("%s", buf); + int ex = 0; + for(int i = 0; buf[i]; ++i) + ex = (10LL * ex + buf[i] - '0') % (mod - 1); + (--ex) < 0 && (ex += mod - 1); + int ans = 1, tmp = 2; + for( ; ex > 0; ex >>= 1, tmp = (LL)tmp * tmp % mod) + (ex & 1) && (ans = (LL)ans * tmp % mod); + printf("%d\n", ans); + } + return 0; +} \ No newline at end of file diff --git a/self_training/2018-09-15/H.cpp b/self_training/2018-09-15/H.cpp new file mode 100644 index 00000000..e198258f --- /dev/null +++ b/self_training/2018-09-15/H.cpp @@ -0,0 +1,127 @@ +#include +using namespace std; +const int maxn = (int)1e6 + 1, maxn2 = maxn << 1 | 1; +int t, n, seq[maxn2], sa[maxn], rk[maxn], ht[maxn]; +int typ[maxn2], ctr[maxn], pos[maxn], *cur = ht; +void sais(int n, int m, int seq[], int typ[], int pos[]) { +#define pushS(x) sa[cur[seq[x]]--] = x +#define pushL(x) sa[cur[seq[x]]++] = x +#define inducedSort(v) do { \ + memset(sa, -1, n * sizeof(int)); \ + memset(ctr, 0, m * sizeof(int)); \ + for(i = 0; i < n; ++i) ++ctr[seq[i]]; \ + for(i = 1; i < m; ++i) ctr[i] += ctr[i - 1]; \ + for(i = 0; i < m; ++i) cur[i] = ctr[i] - 1; \ + for(i = n1 - 1; i >= 0; --i) pushS(v[i]); \ + for(i = 1; i < m; ++i) cur[i] = ctr[i - 1]; \ + for(i = 0; i < n; ++i) if(sa[i] > 0 && typ[sa[i] - 1]) pushL(sa[i] - 1); \ + for(i = 0; i < m; ++i) cur[i] = ctr[i] - 1; \ + for(i = n - 1; i >= 0; --i) if(sa[i] > 0 && !typ[sa[i] - 1]) pushS(sa[i] - 1); \ +} while(0) + int n1 = typ[n - 1] = 0, ch = rk[0] = -1, *seq1 = seq + n; + register int i, j, k, x, y; + for(i = n - 2; i >= 0; --i) typ[i] = seq[i] == seq[i + 1] ? typ[i + 1] : (seq[i] > seq[i + 1]); + for(i = 1; i < n; ++i) rk[i] = typ[i - 1] && !typ[i] ? (pos[n1] = i, n1++) : -1; + inducedSort(pos); + for(i = 0; i < n; ++i) { + if((x = rk[sa[i]]) < 0) + continue; + if(ch < 1 || pos[x + 1] - pos[x] != pos[y + 1] - pos[y]) + ++ch; + else + for(j = pos[x], k = pos[y]; j <= pos[x + 1]; ++j, ++k) + if((seq[j] << 1 | typ[j]) != (seq[k] << 1 | typ[k])) { + ++ch; + break; + } + seq1[y = x] = ch; + } + if(ch + 1 < n1) + sais(n1, ch + 1, seq1, typ + n, pos + n1); + else + for(i = 0; i < n1; ++i) sa[seq1[i]] = i; + for(i = 0; i < n1; ++i) seq1[i] = pos[sa[i]]; + inducedSort(seq1); +} +void SuffixArray() { + int m = 101; + register int i, j, k; + memset(ctr, 0, m * sizeof(int)); + for(i = 0; i <= n; ++i) ctr[seq[i]] = 1; + for(i = 1; i < m; ++i) ctr[i] += ctr[i - 1]; + for(i = 0; i <= n; ++i) seq[i] = ctr[seq[i]] - 1; + sais(n + 1, ctr[m - 1], seq, typ, pos); + for(i = 0; i < n; ++i) rk[sa[i] = sa[i + 1]] = i; + for(i = k = ht[0] = 0; i < n; ++i) { + if(k) --k; + if(!rk[i]) continue; + for(j = sa[rk[i] - 1]; seq[i + k] == seq[j + k]; ++k); + ht[rk[i]] = k; + } +} +int vtot, etot, lnk[maxn2], fa[maxn2], sz[maxn2], dis[maxn2], ord[maxn2]; +struct Edge { + int nxt, v; +} e[maxn2]; +int newNode() { + lnk[vtot] = -1; + fa[vtot] = sz[vtot] = dis[vtot] = 0; + return vtot++; +} +void addEdge(int u, int v, int w) { + fa[v] = u; + dis[v] = dis[u] + w; + e[etot] = (Edge){lnk[u], v}; + lnk[u] = etot++; +} +void SuffixTree() { + vtot = etot = 0; + int rt = newNode(), last; + addEdge(rt, last = newNode(), n - sa[0]); + ++sz[last]; + for(int i = 1; i < n; ++i) { + int p = last; + for( ; dis[p] > ht[i]; p = fa[p]); + int dt = ht[i] - dis[p]; + if(dt > 0) { + int q = newNode(), v = e[lnk[p]].v, w = dis[v] - dis[p]; + fa[q] = p; + dis[q] = dis[p] + dt; + e[lnk[p]].v = q; + addEdge(q, v, w - dt); + p = q; + } + addEdge(p, last = newNode(), n - sa[i] - ht[i]); + ++sz[last]; + } + vtot = 0; + ord[vtot++] = rt; + for(int i = 0; i < vtot; ++i) { + int u = ord[i]; + for(int it = lnk[u]; it != -1; it = e[it].nxt) + ord[vtot++] = e[it].v; + } + for(int i = vtot - 1; i >= 0; --i) { + int u = ord[i]; + for(int it = lnk[u]; it != -1; it = e[it].nxt) + sz[u] += sz[e[it].v]; + } +} +int main() { + static int cL, cR; + static char buf[maxn]; + while(scanf("%s%d%d", buf, &cL, &cR) == 3) { + n = strlen(buf); + for(int i = 0; i < n; ++i) + seq[i] = buf[i] - 'A' + 1; + seq[n] = 0; + SuffixArray(); + SuffixTree(); + long long ans = 0; + for(int i = 1; i < vtot; ++i) + if(sz[i] >= cL && sz[i] <= cR) + ans += dis[i] - dis[fa[i]]; + printf("%lld\n", ans); + } + return 0; +} diff --git a/self_training/2018-09-15/K.cpp b/self_training/2018-09-15/K.cpp new file mode 100644 index 00000000..18770bdb --- /dev/null +++ b/self_training/2018-09-15/K.cpp @@ -0,0 +1,62 @@ +#include +using namespace std; +typedef unsigned long long ULL; +const int maxn = 65; +int t, n, m, d; +ULL mat[3][maxn]; +void mat_mul(int n, ULL lft[], ULL rht[], ULL res[]) { + memset(res, 0, n * sizeof(ULL)); + for(int i = 0; i < n; ++i) + for(int j = 0; j < n; ++j) + if((lft[i] >> j) & 1) + res[i] ^= rht[j]; +} +int main() { + scanf("%d", &t); + while(t--) { + scanf("%d%d%d", &n, &m, &d); + ULL vec1 = 0, vec2 = 0; + for(int i = 0, idx = 0; i < n; ++i) + for(int j = 0; j < n; ++j, ++idx) { + int x; + scanf("%d", &x); + vec1 ^= (ULL)(x & 1) << idx; + } + for(int i = 0, idx = 0; i < m; ++i) + for(int j = 0; j < m; ++j, ++idx) { + int x; + scanf("%d", &x); + vec2 ^= (ULL)(x & 1) << idx; + } + memset(mat, 0, sizeof mat); + int cur = 0, pre = 1, res = 2; + for(int i = 0; i < n * n; ++i) { + int x1 = i / n, y1 = i % n; + for(int j = 0; j < m * m; ++j) { + int x2 = x1 + j / m - (m >> 1), y2 = y1 + j % m - (m >> 1); + if(x2 >= 0 && x2 < n && y2 >= 0 && y2 < n && ((vec2 >> j) & 1)) + mat[cur][x2 * n + y2] ^= 1ULL << i; + } + } + for(int i = 0; i < n * n; ++i) + mat[res][i] ^= 1ULL << i; + while(d > 0) { + if(d & 1) { + mat_mul(n * n, mat[res], mat[cur], mat[pre]); + swap(res, pre); + } + d >>= 1; + mat_mul(n * n, mat[cur], mat[cur], mat[pre]); + swap(cur, pre); + } + vec2 = 0; + for(int j = 0; j < n * n; ++j) + if((vec1 >> j) & 1) + vec2 ^= mat[res][j]; + int ans = 0; + for(int j = 0; j < n * n; ++j) + ans += (vec2 >> j) & 1; + printf("%d\n", ans); + } + return 0; +} diff --git a/self_training/2018-09-22/C.cpp b/self_training/2018-09-22/C.cpp new file mode 100644 index 00000000..2c416e58 --- /dev/null +++ b/self_training/2018-09-22/C.cpp @@ -0,0 +1,161 @@ +#include +using namespace std; +const int maxn = 4, maxs = 13, perm[maxs] = {9, 1, 2, 3, 4, 5, 6, 7, 8, 0, 10, 12, 11}; +const char *pat[maxs] = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"}; +bool read(int cnt[]) { + for(int i = 0; i < maxs; ++i) { + static char buf[3]; + if(scanf("%s", buf) != 1) + return 0; + for(int j = 0; j < maxs; ++j) + if(!strcmp(buf, pat[j])) + ++cnt[j]; + } + return 1; +} +int getNum(int cnt[]) { + int ret = 0; + for(int i = 0; i < maxs; ++i) + ret += cnt[i]; + return ret; +} +int cnt[maxn + 1][maxs]; +void getStatement(int id, int rnk, int &val, int &ctr, int &adt) { + adt = -1; + switch(id) { + case 0: { + if(cnt[id][rnk]) { + val = rnk; + ctr = 1; + } else { + for(int i = 0; i < maxs; ++i) + if(cnt[id][perm[i]]) { + val = perm[i]; + ctr = 1; + break; + } + } + break; + } + case 1 : { + if(cnt[id][rnk]) { + val = rnk; + ctr = cnt[id][rnk]; + } else { + for(int i = 0; i < maxs; ++i) + if(cnt[id][perm[i]]) { + val = perm[i]; + ctr = 1; + break; + } + } + break; + } + case 2 : { + if(cnt[id][rnk]) { + val = rnk; + ctr = cnt[id][rnk]; + } else { + ctr = maxn + 1; + for(int i = 0; i < maxs; ++i) + if(cnt[id][perm[i]] && cnt[id][perm[i]] < ctr) { + val = perm[i]; + ctr = cnt[id][perm[i]]; + } + } + break; + } + case 3 : { + if(cnt[id][rnk] == 3 || cnt[id][rnk] == 4) { + val = rnk; + ctr = cnt[id][rnk]; + } else { + val = rnk; + ctr = cnt[id][rnk]; + for(int i = 0; i < maxs; ++i) + if(cnt[id][perm[i]] && perm[i] != val) { + adt = perm[i]; + break; + } + } + break; + } + } +} +bool getChallenge(int id, int pid, int rnk, int ctr) { + switch(id) { + case 0 : { + if((pid + 1) % maxn == id && !cnt[id][(rnk + 1) % maxs]) + return 1; + if(cnt[id][rnk] + ctr > maxn) + return 1; + break; + } + case 1 : { + if((pid + 1) % maxn == id && !cnt[id][(rnk + 1) % maxs]) + return 1; + break; + } + case 2 : { + if(cnt[id][rnk] == maxn) + return 1; + break; + } + case 3 : { + if(!getNum(cnt[pid])) + return 1; + break; + } + } + return 0; +} +int main() { + while(read(cnt[0]) && read(cnt[1]) && read(cnt[2]) && read(cnt[3])) { + for(int i = 0, j = 0; ; i = (i + 1) % maxn, j = (j + 1) % maxs) { + int val, ctr, adt; + getStatement(i, j, val, ctr, adt); + cnt[i][val] -= ctr; + cnt[maxn][val] += ctr; + if(adt != -1) { + --cnt[i][adt]; + ++cnt[maxn][adt]; + } + for(int k = 1; k < maxn; ++k) { + int x = (i + k) % maxn; + if(getChallenge(x, i, j, ctr + (adt != -1))) { + if(val == j && adt == -1) { + for(int y = 0; y < maxs; ++y) { + cnt[x][y] += cnt[maxn][y]; + cnt[maxn][y] = 0; + } + } else { + for(int y = 0; y < maxs; ++y) { + cnt[i][y] += cnt[maxn][y]; + cnt[maxn][y] = 0; + } + } + break; + } + } + if(!getNum(cnt[i])) + break; + } + for(int i = 0; i < maxn; ++i) + if(!getNum(cnt[i])) { + puts("WINNER"); + } else { + for(int j = 0, k = 0; j < maxs; ++j) + while(cnt[i][j]--) { + if(k) { + putchar(' '); + } else { + k = 1; + } + printf("%s", pat[j]); + } + putchar('\n'); + } + memset(cnt, 0, sizeof cnt); + } + return 0; +} diff --git a/self_training/2018-09-22/E.py b/self_training/2018-09-22/E.py new file mode 100644 index 00000000..01090f60 --- /dev/null +++ b/self_training/2018-09-22/E.py @@ -0,0 +1,12 @@ +import math +a = [] +for n in range(2001) : + if n < 5 : + val = ((n - 1) * (n - 2) // 2) ** 2 + else : + val = (n - 1) * (2 * (n ** 3 - 2 * n ** 2 + n + 1) * a[n - 1] + (n - 2) * ((n ** 2 - 2 * n + 2) * (n + 1) * a[n - 2] + (2 * n ** 2 - 6 * n + 1) * n * a[n - 3] + (n - 3) * (a[n - 4] * (n ** 3 - 5 * n ** 2 + 3) - (n - 4) * (n - 1) * (n + 1) * a[n - 5]))) // (2 * n * (n - 2)) + a += [val] +for case in range(int(raw_input())) : + n, p = map(int, raw_input().split(" ")) + print(a[n] % p * (math.factorial(n) % p) % p) + diff --git a/self_training/2018-10-12/CHEFPRMS.cpp b/self_training/2018-10-12/CHEFPRMS.cpp new file mode 100644 index 00000000..2f8d5c7e --- /dev/null +++ b/self_training/2018-10-12/CHEFPRMS.cpp @@ -0,0 +1,27 @@ +#include +using namespace std; +const int maxn = 201; +int t, n; +bool f[3][maxn]; +int main() { + for(int i = 2; i < maxn; ++i) { + if(!f[0][i]) + for(int j = i + i; j < maxn; j += i) + f[0][j] = 1; + f[0][i] = !f[0][i]; + } + for(int i = 1; i < maxn; ++i) + if(f[0][i]) + for(int j = i, k = 1; j < maxn; j += i, ++k) + f[1][j] |= i != k && f[0][k]; + for(int i = 1; i < maxn; ++i) + if(f[1][i]) + for(int j = i, k = 0; j < maxn; ++j, ++k) + f[2][j] |= f[1][k]; + scanf("%d", &t); + while(t--) { + scanf("%d", &n); + puts(f[2][n] ? "YES" : "NO"); + } + return 0; +} diff --git a/self_training/2018-10-12/QABC.cpp b/self_training/2018-10-12/QABC.cpp new file mode 100644 index 00000000..05bc3bb5 --- /dev/null +++ b/self_training/2018-10-12/QABC.cpp @@ -0,0 +1,26 @@ +#include +using namespace std; +const int maxn = (int)1e5 + 1; +int t, n, a[maxn], b[maxn]; +int main() { + scanf("%d", &t); + while(t--) { + scanf("%d", &n); + for(int i = 0; i < n; ++i) + scanf("%d", a + i); + for(int i = 0; i < n; ++i) + scanf("%d", b + i); + bool chk = 1; + for(int i = 0; chk && i < n; ++i) { + if(i + 2 < n && a[i] < b[i]) { + int dt = b[i] - a[i]; + a[i] += dt; + a[i + 1] += dt + dt; + a[i + 2] += dt + dt + dt; + } + chk &= a[i] == b[i]; + } + puts(chk ? "TAK" : "NIE"); + } + return 0; +} diff --git a/self_training/2018-10-12/QUALPREL.cpp b/self_training/2018-10-12/QUALPREL.cpp new file mode 100644 index 00000000..83dd0096 --- /dev/null +++ b/self_training/2018-10-12/QUALPREL.cpp @@ -0,0 +1,16 @@ +#include +using namespace std; +const int maxn = (int)1e5 + 1; +int t, n, m, a[maxn]; +int main() { + scanf("%d", &t); + while(t--) { + scanf("%d%d", &n, &m); + for(int i = 0; i < n; ++i) + scanf("%d", a + i); + sort(a, a + n, greater()); + for( ; m < n && a[m - 1] == a[m]; ++m); + printf("%d\n", m); + } + return 0; +} diff --git a/self_training/2018-10-12/SPREAD2.cpp b/self_training/2018-10-12/SPREAD2.cpp new file mode 100644 index 00000000..e2210d35 --- /dev/null +++ b/self_training/2018-10-12/SPREAD2.cpp @@ -0,0 +1,17 @@ +#include +using namespace std; +const int maxn = (int)1e5 + 1; +int t, n, a[maxn]; +int main() { + scanf("%d", &t); + while(t--) { + scanf("%d", &n); + for(int i = 0; i < n; ++i) + scanf("%d", a + i); + int days = 0, pos = 1, cnt = a[0]; + for( ; pos < n; ++days) + for(int lim = min(pos + cnt, n); pos < lim; cnt = min(cnt + a[pos++], n)); + printf("%d\n", days); + } + return 0; +} diff --git a/self_training/2018-10-12/TEAMMATE.cpp b/self_training/2018-10-12/TEAMMATE.cpp new file mode 100644 index 00000000..2ed897ef --- /dev/null +++ b/self_training/2018-10-12/TEAMMATE.cpp @@ -0,0 +1,37 @@ +#include +using namespace std; +typedef long long LL; +const int maxn = (int)1e5 + 1, mod = (int)1e9 + 7; +int t, n, m, a[maxn], c[maxn], inv[maxn], ans; +int main() { + inv[1] = 1; + for(int i = 2; i < maxn; ++i) + inv[i] = mod - (int)(mod / i * (LL)inv[mod % i] % mod); + scanf("%d", &t); + while(t--) { + scanf("%d", &n); + for(int i = 0; i < n; ++i) + scanf("%d", a + i); + sort(a, a + n); + m = 0; + for(int i = 0; i < n; ++i) + if(!m || a[m - 1] < a[i]) { + a[m++] = a[i]; + c[m - 1] = 1; + } else { + ++c[m - 1]; + } + ans = 1; + for(int i = 0; i < m; ++i) { + for(int j = 1; c[i] >= 2; ++j, c[i] -= 2) + ans = ans * (c[i] * (c[i] - 1LL) / 2) % mod * inv[j] % mod; + if(c[i]) { + ans = (LL)ans * c[i + 1] % mod; + --c[i]; + --c[i + 1]; + } + } + printf("%d\n", ans); + } + return 0; +} diff --git a/self_training/2018-11-03/ANGLE.cpp b/self_training/2018-11-03/ANGLE.cpp new file mode 100644 index 00000000..94eeef88 --- /dev/null +++ b/self_training/2018-11-03/ANGLE.cpp @@ -0,0 +1,67 @@ +#include +using namespace std; +typedef long long LL; +const int maxn = (int)1e3 + 1; +inline int getFloor(LL x, int y) { // y > 0 + int z = x / y; + for( ; (LL)y * z <= x; ++z); + for( ; (LL)y * z > x; --z); + return z; +} +int t, n, p, q, a[maxn], ord[maxn]; +int main() { + scanf("%d", &t); + while(t--) { + scanf("%d%d%d", &n, &p, &q); + for(int i = 1; i <= n; ++i) { + scanf("%d", a + i); + ord[i] = i; + } + sort(ord + 1, ord + n + 1, [&](int const &u, int const &v) { + return a[u] < a[v]; + }); + int u = -1, v = -1, w = -1, mxFz = ~0u >> 1, mxFm = 1; + for(int i = 1; i <= n; ++i) { + int Az = a[ord[i]], L = i, R = i; + for(int j = 1; j < i; ++j) { + int Ay = a[ord[j]]; + for( ; L >= 1 && a[ord[L]] > Az - Ay; --L); + for( ; R <= n && a[ord[R]] < Az + Ay; ++R); + if(L + 1 > R - 1) + continue; + int Ax2upp = Ay * Ay + Az * Az + getFloor(-2LL * Ay * Az * p, q); + int pL = L + 1, pR = R - 1; + if(a[ord[pL]] * a[ord[pL]] > Ax2upp) + continue; + while(pL < pR) { + int pM = (pL + pR + 1) >> 1, Ax = a[ord[pM]]; + if(Ax * Ax <= Ax2upp) { + pL = pM; + } else { + pR = pM - 1; + } + } + if(pL == i || pL == j) + --pL; + if(pL == i || pL == j) + --pL; + if(pL <= L) + continue; + int Ax = a[ord[pL]], fz = (Ay * Ay + Az * Az - Ax * Ax), fm = 2 * Ay * Az; + if((LL)fz * mxFm < (LL)mxFz * fm) { + u = ord[pL]; + v = ord[j]; + w = ord[i]; + mxFz = fz; + mxFm = fm; + } + } + } + if(u == -1) { + puts("-1"); + } else { + printf("%d %d %d\n", u, v, w); + } + } + return 0; +} diff --git a/self_training/2018-11-03/BUDDYNIM.cpp b/self_training/2018-11-03/BUDDYNIM.cpp new file mode 100644 index 00000000..5653bb1b --- /dev/null +++ b/self_training/2018-11-03/BUDDYNIM.cpp @@ -0,0 +1,27 @@ +#include +using namespace std; +const int maxn = 101; +int t, n, m, a[maxn], b[maxn]; +int main() { + scanf("%d", &t); + while(t--) { + scanf("%d%d", &n, &m); + for(int i = 0; i < n; ++i) + scanf("%d", a + i); + for(int i = 0; i < m; ++i) + scanf("%d", b + i); + sort(a, a + n, greater()); + for( ; n && !a[n - 1]; --n); + sort(b, b + m, greater()); + for( ; m && !b[m - 1]; --m); + if(n == m) { + bool chk = 1; + for(int i = 0; i < n; ++i) + chk &= a[i] == b[i]; + puts(chk ? "Bob" : "Alice"); + } else { + puts("Alice"); + } + } + return 0; +} \ No newline at end of file diff --git a/self_training/2018-11-03/PAINTREE.cpp b/self_training/2018-11-03/PAINTREE.cpp new file mode 100644 index 00000000..e01ffd36 --- /dev/null +++ b/self_training/2018-11-03/PAINTREE.cpp @@ -0,0 +1,79 @@ +#include +using namespace std; +typedef long long LL; +typedef unsigned long long ULL; +const int maxn = (int)2e3 + 1, mod = 998244353; +int n, sz[maxn], f[maxn][2][maxn]; +vector e[maxn]; +inline void mod_inc(LL &x, LL y) { +// if((x += y) < 0) +// x = (ULL)x % mod; + x = (x + y) % mod; +} +inline void mod_add(int &x, int y) { + x >= mod && (x %= mod); + y >= mod && (y %= mod); + (x += y) >= mod && (x -= mod); +} +void dfs(int u, int fa) { + for(int v : e[u]) { + if(v == fa) + continue; + dfs(v, u); + } + static LL g[6][maxn]; + sz[u] = 0; + g[0][0] = 1; + g[1][0] = g[2][0] = 0; + for(int v : e[u]) { + if(v == fa) + continue; + memset(g[3], 0, (sz[u] + sz[v] + 1) * sizeof(LL)); + memset(g[4], 0, (sz[u] + sz[v] + 1) * sizeof(LL)); + memset(g[5], 0, (sz[u] + sz[v] + 1) * sizeof(LL)); + for(int i = 0; i <= sz[u]; ++i) + for(int j = 0; j <= sz[v]; ++j) { + int sum = f[v][0][j] + f[v][1][j]; + mod_inc(g[3][i + j], (LL)g[0][i] * sum); + mod_inc(g[4][i + j], (LL)g[0][i] * f[v][1][j] + (LL)g[1][i] * sum); + mod_inc(g[5][i + j], (LL)g[1][i] * f[v][1][j] + (LL)g[2][i] * sum); + } + sz[u] += sz[v]; + memcpy(g[0], g[3], (sz[u] + 1) * sizeof(LL)); + memcpy(g[1], g[4], (sz[u] + 1) * sizeof(LL)); + memcpy(g[2], g[5], (sz[u] + 1) * sizeof(LL)); + } + ++sz[u]; + memset(f[u][0], 0, (sz[u] + 1) * sizeof(int)); + memset(f[u][1], 0, (sz[u] + 1) * sizeof(int)); + for(int i = 0; i < sz[u]; ++i) { + g[0][i] >= mod && (g[0][i] %= mod); + g[1][i] >= mod && (g[1][i] %= mod); + g[2][i] >= mod && (g[2][i] %= mod); + mod_add(f[u][0][i], (int)g[0][i]); + if(i) + mod_add(f[u][0][i - 1], (int)g[2][i]); + mod_add(f[u][1][i + 1], (int)g[0][i]); + mod_add(f[u][1][i], (int)g[1][i]); + } +} +inline int mod_inv(int x) { + return x <= 1 ? x : mod - (int)(mod / x * (LL)mod_inv(mod % x) % mod); +} +int main() { + scanf("%d", &n); + for(int i = 1; i < n; ++i) { + int u, v; + scanf("%d%d", &u, &v); + e[u].push_back(v); + e[v].push_back(u); + } + dfs(1, -1); + int prd = 1, ivs = mod_inv(n * (n + 1) / 2), ans = 0; + for(int i = 1; i <= n; ++i) { + prd = (LL)prd * i % mod * ivs % mod; + ans = (ans + (LL)prd * (f[1][0][i] + f[1][1][i])) % mod; + } + printf("%d\n", ans); + return 0; +} \ No newline at end of file diff --git a/self_training/2018-11-03/STRTF.cpp b/self_training/2018-11-03/STRTF.cpp new file mode 100644 index 00000000..89b528e9 --- /dev/null +++ b/self_training/2018-11-03/STRTF.cpp @@ -0,0 +1,47 @@ +#include +using namespace std; +const int maxn = (int)2e5 + 1, maxd = 1 << 9 | 1; +int n, m, mx, low, a[maxn], ans[maxn]; +vector e[maxd], sub[maxd]; +struct Query { + int k, x; +} b[maxn]; +void dfs(int dep, int msk) { + if(dep == mx) { + for(int idx : e[msk]) { + int val = 0, pos = b[idx].x; + for(int dt : sub[b[idx].k]) + if(pos + dt <= n) + val ^= a[pos + dt]; + ans[idx] = val; + } + return; + } + dfs(dep + 1, msk); + for(int dt = 1 << dep, i = 1; i + dt <= n; ++i) + a[i] ^= a[i + dt]; + dfs(dep + 1, msk | 1 << (dep - low)); + for(int dt = 1 << dep, i = n - dt; i >= 1; --i) + a[i] ^= a[i + dt]; +} +int main() { + scanf("%d%d", &n, &m); + for(mx = 0; 1 << mx <= n; ++mx); + low = (mx + 1) >> 1; + for(int i = 0; i < 1 << low; ++i) + for(int j = 0; j <= i; ++j) + if((i & j) == j) + sub[i].push_back(j); + for(int i = 1; i <= n; ++i) + scanf("%d", a + i); + for(int i = 1; i <= m; ++i) { + scanf("%d%d", &b[i].k, &b[i].x); + b[i].k &= (1 << mx) - 1; + e[b[i].k >> low].push_back(i); + b[i].k &= (1 << low) - 1; + } + dfs(low, 0); + for(int i = 1; i <= m; ++i) + printf("%d\n", ans[i]); + return 0; +} \ No newline at end of file diff --git a/self_training/2018-11-09/217A.cpp b/self_training/2018-11-09/217A.cpp new file mode 100644 index 00000000..46637e22 --- /dev/null +++ b/self_training/2018-11-09/217A.cpp @@ -0,0 +1,16 @@ +#include +using namespace std; +int A, B, L; +int check(int x, int y) { + for(int i = 0; ; i ^= 1) { + if(x > L) + return i; + y += ((y - 1) / x + 1) * x; + swap(x, y); + } +} +int main() { + scanf("%d%d%d", &A, &B, &L); + printf("%s %s\n", check(A, B) == 0 ? "Yes" : "No", check(B, A) == 1 ? "Yes" : "No"); + return 0; +} \ No newline at end of file diff --git a/self_training/2018-11-09/217B.cpp b/self_training/2018-11-09/217B.cpp new file mode 100644 index 00000000..0b8f54a2 --- /dev/null +++ b/self_training/2018-11-09/217B.cpp @@ -0,0 +1,26 @@ +#include +using namespace std; +const int maxn = (int)1e5 + 1, maxd = 17, maxp = 8; +const char *seq = "msc", *pat[maxp] = {"022012", "020212", "002212", "012022", "020122", "002122", "010222", "001222"}; +int n, nxt[3][maxn]; +char buf[maxn]; +int main() { + scanf("%d%s", &n, buf); + nxt[0][n] = nxt[1][n] = nxt[2][n] = n; + for(int i = n - 1; i >= 0; --i) + for(int j = 0; j < 3; ++j) + nxt[j][i] = buf[i] == seq[j] ? i : nxt[j][i + 1]; + long long ans = 0; + for(int i = 0; i < n; ++i) { + int low = n; + for(int j = 0; j < maxp; ++j) { + int pos = i - 1; + for(int k = 0; pos < n && k < 6; ++k) + pos = nxt[pat[j][k] - '0'][pos + 1]; + low = min(low, pos); + } + ans += n - low; + } + printf("%lld\n", ans); + return 0; +} diff --git a/self_training/2018-11-09/217C.cpp b/self_training/2018-11-09/217C.cpp new file mode 100644 index 00000000..de39f7cd --- /dev/null +++ b/self_training/2018-11-09/217C.cpp @@ -0,0 +1,64 @@ +#include +using namespace std; +const int maxn = (int)1e3 + 1, INF = 0x3f3f3f3f; +int n, m, a[maxn], ord[maxn], tot, que[maxn], fa[maxn], f[maxn][maxn]; +vector e[maxn]; +bool check(int lim) { + for(int i = n; i >= 1; --i) { + int u = ord[i]; + for(int j = 1; j <= tot; ++j) + f[u][j] = que[j] <= a[u] && a[u] <= que[j] + lim ? 0 : INF; + for(int v : e[u]) { + if(v == fa[u]) + continue; + int low = f[v][1]; + for(int j = 2; j <= tot; ++j) + low = min(low, f[v][j]); + for(int j = 1; j <= tot; ++j) { + if(f[u][j] == INF) + continue; + f[u][j] += min(f[v][j], low + 1); + } + } + } + for(int i = 1; i <= tot; ++i) + if(f[1][i] <= m) + return 1; + return 0; +} +int main() { + scanf("%d%d", &n, &m); + for(int i = 1; i <= n; ++i) { + scanf("%d", a + i); + que[i] = a[i]; + } + sort(que + 1, que + n + 1); + tot = unique(que + 1, que + n + 1) - que - 1; + for(int i = 1; i < n; ++i) { + int u, v; + scanf("%d%d", &u, &v); + e[u].push_back(v); + e[v].push_back(u); + } + ord[1] = 1; + for(int i = 1, tot = 1; i <= tot; ++i) { + int u = ord[i]; + for(int v : e[u]) { + if(v == fa[u]) + continue; + fa[v] = u; + ord[++tot] = v; + } + } + int L = 0, R = (int)2e9; + while(L < R) { + int M = L + ((R - L) >> 1); + if(check(M)) { + R = M; + } else { + L = M + 1; + } + } + printf("%d\n", L); + return 0; +} \ No newline at end of file diff --git a/self_training/2018-11-09/217D.cpp b/self_training/2018-11-09/217D.cpp new file mode 100644 index 00000000..b106e51a --- /dev/null +++ b/self_training/2018-11-09/217D.cpp @@ -0,0 +1,27 @@ +#include +using namespace std; +typedef long long LL; +const int maxn = (int)2e6 + 1, mod = 998244353; +int n, m, q, inv[maxn]; +inline int mod_inv(int x) { + return x < n + m ? inv[x] : mod - (int)(mod / x * (LL)mod_inv(mod % x) % mod); +} +int main() { + scanf("%d%d%d", &n, &m, &q); + inv[1] = 1; + for(int i = 2; i < n + m; ++i) + inv[i] = mod - (int)(mod / i * (LL)inv[mod % i] % mod); + int pos = q >> 1, val = 1; + for(int i = 1; i < n + m; ++i) + val = (LL)val * (pos + i) % mod * inv[i] % mod; + int ans = 0, coeff = 1; + for(int i = 0; i <= n; ++i) { + for( ; pos && i + pos + pos > q; --pos) + val = (LL)val * pos % mod * mod_inv(pos + n + m - 1) % mod; + if(i + pos + pos == q) + ans = (ans + (LL)coeff * val) % mod; + coeff = (LL)coeff * (n - i) % mod * inv[i + 1] % mod; + } + printf("%d\n", ans); + return 0; +} \ No newline at end of file diff --git a/self_training/2018-11-09/217E.cpp b/self_training/2018-11-09/217E.cpp new file mode 100644 index 00000000..bf98ef38 --- /dev/null +++ b/self_training/2018-11-09/217E.cpp @@ -0,0 +1,88 @@ +#include +using namespace std; +typedef long long LL; +const int maxn = (int)1e5 + 1, maxm = 21; +int n, m, q; +LL val[maxn], seg[maxn << 1 | 1][maxm], res[maxm]; +inline int seg_idx(int L, int R) { + return (L + R) | (L < R); +} +void seg_init(int L, int R) { + int rt = seg_idx(L, R); + if(L == R) { + seg[rt][L % m] = val[L] - val[L - 1]; + return; + } + int M = (L + R) >> 1, lch = seg_idx(L, M), rch = seg_idx(M + 1, R); + seg_init(L, M); + seg_init(M + 1, R); + for(int i = 0; i < m; ++i) + seg[rt][i] = seg[lch][i] + seg[rch][i]; +} +void seg_upd(int L, int R, int x) { + int rt = seg_idx(L, R); + if(L == R) { + seg[rt][L % m] = val[L] - val[L - 1]; + return; + } + int M = (L + R) >> 1, lch = seg_idx(L, M), rch = seg_idx(M + 1, R); + if(x <= M) + seg_upd(L, M, x); + if(x + 1 > M) + seg_upd(M + 1, R, x); + for(int i = 0; i < m; ++i) + seg[rt][i] = seg[lch][i] + seg[rch][i]; +} +void seg_que(int L, int R, int l, int r) { + if(l <= L && R <= r) { + int rt = seg_idx(L, R); + for(int i = 0; i < m; ++i) + res[i] += seg[rt][i]; + return; + } + int M = (L + R) >> 1; + if(l <= M) + seg_que(L, M, l, r); + if(r > M) + seg_que(M + 1, R, l, r); +} +int main() { + scanf("%d%d%d", &n, &m, &q); + for(int i = 1; i <= n; ++i) + scanf("%lld", val + i); + seg_init(1, n); + while(q--) { + int typ; + scanf("%d", &typ); + if(typ == 1) { + int l, r; + scanf("%d%d", &l, &r); + if(r - l + 1 < m) { + int ans = 0; + for(int i = l; i <= r; ++i) + ans += val[i] != 0; + printf("%d\n", ans); + continue; + } + for(int i = 0; i < m; ++i) + res[i] = 0; + if(l < r) + seg_que(1, n, l + 1, r); + res[l % m] += val[l]; + res[(r + 1) % m] -= val[r]; + int ans = 0; + map ctr; + for(int i = 0; i < m; ++i) { + res[i] += i ? res[i - 1] : 0; + ans = max(ans, ++ctr[res[i]]); + } + printf("%d\n", m - ans); + } else { + int x; + scanf("%d", &x); + scanf("%lld", val + x); + seg_upd(1, n, x); + } + } + return 0; +} \ No newline at end of file diff --git a/self_training/2018-11-09/217F.cpp b/self_training/2018-11-09/217F.cpp new file mode 100644 index 00000000..cda90c15 --- /dev/null +++ b/self_training/2018-11-09/217F.cpp @@ -0,0 +1,96 @@ +#include +using namespace std; +typedef double DB; +typedef long long LL; +const int maxn = (int)1e5 + 1, maxm = (int)2e5 + 1; +const DB eps = 1e-6, rate = (sqrtl(5) + 1) / 2; +int n, m, dsu[maxn], ord[maxm]; +struct Edge { + int u, v, a, b; + LL c; + void read() { + scanf("%d%d%d%d%lld", &u, &v, &a, &b, &c); + } + DB get(DB t) const { + return (a * t + b) * t + c; + } +} e[maxm]; +DB w[maxm]; +int dsu_find(int x) { + return dsu[x] < 0 ? x : (dsu[x] = dsu_find(dsu[x])); +} +DB calc(DB t) { + DB ret = 0; + int tot = 0; + for(int i = 0; i < m; ++i) { + w[i] = e[i].get(t); + if(w[i] > 0) + ord[tot++] = i; + } + sort(ord, ord + tot, [&](int const &u, int const &v) { + return w[u] > w[v]; + }); + memset(dsu + 1, -1, n * sizeof(int)); + for(int i = 0; i < tot; ++i) { + int u = dsu_find(e[ord[i]].u); + int v = dsu_find(e[ord[i]].v); + if(u != v) { + ret += w[ord[i]]; + if(dsu[u] < dsu[v]) + swap(u, v); + dsu[v] -= dsu[u] == dsu[v]; + dsu[u] = v; + } + } + return ret; +} +int main() { + scanf("%d%d", &n, &m); + for(int i = 0; i < m; ++i) + e[i].read(); + int tL, tR; + LL w; + scanf("%d%d%lld", &tL, &tR, &w); + DB L = tL, R = tR, pL, pM, pR; + while(1) { + DB ML = R - (R - L) / rate, fL = calc(ML); + if(fL < w) { + pM = ML; + break; + } + DB MR = L + (R - L) / rate, fR = calc(MR); + if(fR < w) { + pM = MR; + break; + } + if(fL < fR) { + R = MR; + } else { + L = ML; + } + } + L = tL; + R = pM; + while((R - L) / (tR - tL) >= eps) { + DB M = (L + R) / 2, fM = calc(M); + if(fM < w) { + R = M; + } else { + L = M; + } + } + pL = L; + L = pM; + R = tR; + while((R - L) / (tR - tL) >= eps) { + DB M = (L + R) / 2, fM = calc(M); + if(fM < w) { + L = M; + } else { + R = M; + } + } + pR = L; + printf("%.5f\n", 1 - (pR - pL) / (tR - tL)); + return 0; +} diff --git a/self_training/2018-11-30/272A.cpp b/self_training/2018-11-30/272A.cpp new file mode 100644 index 00000000..6cc85a41 --- /dev/null +++ b/self_training/2018-11-30/272A.cpp @@ -0,0 +1,20 @@ +#include +using namespace std; +const int maxn = (int)1e5 + 1, mod = (int)1e9 + 7; +int n, m, len, val; +char buf[maxn]; +int main() { + scanf("%d%d", &n, &m); + len = max(n, m); + memset(buf, '0', len * sizeof(char)); + buf[0] = buf[len - 1] = '1'; + for(int i = 0; (i + 1) << 1 < m; ++i) { + int u = (len >> 1) + i, v = len - 1 - u; + buf[u] = buf[v] = '1'; + } +// puts(buf); + for(int i = len - 1; i >= 0; --i) + (val = val << 1 | (buf[i] == '1')) >= mod && (val -= mod); + printf("%d\n", val); + return 0; +} diff --git a/self_training/2018-11-30/272B.cpp b/self_training/2018-11-30/272B.cpp new file mode 100644 index 00000000..84f258b9 --- /dev/null +++ b/self_training/2018-11-30/272B.cpp @@ -0,0 +1,48 @@ +#include +using namespace std; +const int maxn = (int)5e5 + 1; +int n, a[maxn], lnk[maxn], ord[maxn], fa[maxn], sz[maxn]; +struct Edge { + int nxt, v; +} e[maxn << 1 | 1]; +int main() { + scanf("%d", &n); + for(int i = 1; i < n; ++i) { + int u, v; + scanf("%d%d", &u, &v); + e[i << 1] = (Edge){lnk[u], v}; + lnk[u] = i << 1; + e[i << 1 | 1] = (Edge){lnk[v], u}; + lnk[v] = i << 1 | 1; + } + for(int i = 1; i <= n; ++i) + scanf("%d", a + i); + ord[1] = 1; + for(int i = 1, tot = 1; i <= tot; ++i) { + int u = ord[i]; + for(int it = lnk[u]; it; it = e[it].nxt) { + int v = e[it].v; + if(v == fa[u]) + continue; + fa[v] = u; + ord[++tot] = v; + } + } + int ans = 0, odd = (n * (n - 1LL) / 2) & 1; + for(int i = n; i >= 1; --i) { + int u = ord[i], chk = odd; + sz[u] = 1; + for(int it = lnk[u]; it; it = e[it].nxt) { + int v = e[it].v; + if(v == fa[u]) + continue; + sz[u] += sz[v]; + chk ^= (sz[v] * (sz[v] - 1LL) / 2) & 1; + } + chk ^= ((n - sz[u]) * (n - sz[u] - 1LL) / 2) & 1; + if(chk) + ans ^= a[u]; + } + printf("%d\n", ans); + return 0; +} diff --git a/self_training/2018-11-30/272C.cpp b/self_training/2018-11-30/272C.cpp new file mode 100644 index 00000000..ed1ab8b6 --- /dev/null +++ b/self_training/2018-11-30/272C.cpp @@ -0,0 +1,8 @@ +#include +using namespace std; +int n; +int main() { + scanf("%d", &n); + printf("%.7f\n", n / 2.0 + 1); + return 0; +} diff --git a/self_training/2018-11-30/272D.cpp b/self_training/2018-11-30/272D.cpp new file mode 100644 index 00000000..0ebf7f38 --- /dev/null +++ b/self_training/2018-11-30/272D.cpp @@ -0,0 +1,77 @@ +#include +using namespace std; +const int maxn = (int)4e5 + 1; +int n, m, tot, dsu[maxn], lnk[maxn], pre[maxn]; +struct Edge { + int nxt, v; +} e[maxn]; +bool bri[maxn]; +int tarjan(int u, int Fa) { + int lowu = pre[u] = tot++; + for(int it = lnk[u]; it != -1; it = e[it].nxt) { + if(Fa == (it ^ 1)) + continue; + int &v = e[it].v; + if(pre[v] == -1) { + int lowv = tarjan(v, it); + if(lowu > lowv) + lowu = lowv; + if(pre[u] < lowv) + bri[it >> 1] = 1; + } else if(lowu > pre[v]) + lowu = pre[v]; + } + return lowu; +} +int dsu_find(int x) { + return x == dsu[x] ? x : (dsu[x] = dsu_find(dsu[x])); +} +int main() { + scanf("%d%d%*d", &n, &m); + for(int i = 1; i <= n; ++i) { + dsu[i] = i; + lnk[i] = -1; + } + vector > > edges; + edges.reserve(m); + while(m--) { + int u, v, w; + scanf("%d%d%d", &u, &v, &w); + edges.push_back(make_pair(w, make_pair(u, v))); + } + sort(edges.begin(), edges.end()); + m = 0; + for(vector > >::iterator it = edges.begin(); it != edges.end(); ) { + vector > >::iterator jt = it; + for( ; jt != edges.end() && it -> first == jt -> first; ++jt) { + pair tmp = jt -> second; + int u = dsu_find(tmp.first); + int v = dsu_find(tmp.second); + if(u != v) { + e[m] = (Edge){lnk[u], v}; + lnk[u] = m++; + e[m] = (Edge){lnk[v], u}; + lnk[v] = m++; + } + } + for( ; it != jt; ++it) { + pair tmp = it -> second; + int u = dsu_find(tmp.first); + int v = dsu_find(tmp.second); + if(u != v) { + lnk[++n] = -1; + dsu[n] = n; + dsu[u] = dsu[v] = n; + } + } + } + memset(pre + 1, -1, n * sizeof(int)); + for(int i = 1; i <= n; ++i) + if(pre[i] == -1) + tarjan(i, -1); + int ans = 0; + for(int i = 0; i < m; i += 2) + ans += bri[i >> 1]; + printf("%d\n", ans); + return 0; +} diff --git a/self_training/2018-11-30/272E.cpp b/self_training/2018-11-30/272E.cpp new file mode 100644 index 00000000..e63418c6 --- /dev/null +++ b/self_training/2018-11-30/272E.cpp @@ -0,0 +1,117 @@ +#include +using namespace std; +const int maxn = (int)1e4 + 1, maxm = (int)2e4 + 1, maxs = maxn * 784 + 1; +int n, m, pos[maxm], etot, qtot, stot, val[maxm]; +struct Segment { + int lch, rch, tim0, tim1; +} seg[maxs]; +vector eids[maxm << 1 | 1]; +struct Event { + int sta, end, typ, L, R; +} e[maxm]; +struct Query { + int L, R; +} que[maxm]; +inline int getIdx(int L, int R) { + return (L + R) | (L < R); +} +void addEvent(int L, int R, int l, int r, int id) { + if(l <= L && R <= r) { + eids[getIdx(L, R)].push_back(id); + } else { + int M = (L + R) >> 1; + if(l <= M) + addEvent(L, M, l, r, id); + if(r > M) + addEvent(M + 1, R, l, r, id); + } +} +int segInit(char *buf, int L, int R) { + int rt = ++stot; + seg[rt].tim0 = seg[rt].tim1 = -1; + if(L == R) { + (buf[L] == '0' ? seg[rt].tim0 : seg[rt].tim1) = 0; + } else { + int M = (L + R) >> 1; + seg[rt].lch = segInit(buf, L, M); + seg[rt].rch = segInit(buf, M + 1, R); + } + return rt; +} +int segUpd(int rt, int L, int R, int l, int r, int typ, int tim) { + int tr = rt; + rt = ++stot; + seg[rt] = seg[tr]; + if(l <= L && R <= r) { + int &cur = typ == 0 ? seg[rt].tim0 : seg[rt].tim1; + if(cur < tim) + cur = tim; + } else { + int M = (L + R) >> 1; + if(l <= M) + seg[rt].lch = segUpd(seg[rt].lch, L, M, l, r, typ, tim); + if(r > M) + seg[rt].rch = segUpd(seg[rt].rch, M + 1, R, l, r, typ, tim); + } + return rt; +} +int segDfs(int rt, int L, int R, int l, int r, int t0 = -1, int t1 = -1) { + if(t0 < seg[rt].tim0) + t0 = seg[rt].tim0; + if(t1 < seg[rt].tim1) + t1 = seg[rt].tim1; + if(L == R) + return t0 < t1; + int M = (L + R) >> 1; + if(r <= M) + return segDfs(seg[rt].lch, L, M, l, r, t0, t1); + if(l > M) + return segDfs(seg[rt].rch, M + 1, R, l, r, t0, t1); + return segDfs(seg[rt].lch, L, M, l, r, t0, t1) + segDfs(seg[rt].rch, M + 1, R, l, r, t0, t1); +} +void solve(int rt, int L, int R) { + vector &cur = eids[getIdx(L, R)]; + for(vector::iterator it = cur.begin(); it != cur.end(); ++it) { + Event &curE = e[*it]; + rt = segUpd(rt, 1, n, curE.L, curE.R, curE.typ, curE.sta); + } + if(L == R) { + val[L] = segDfs(rt, 1, n, que[L].L, que[L].R); + } else { + int M = (L + R) >> 1; + solve(rt, L, M); + solve(rt, M + 1, R); + } +} +int main() { + static char buf[maxn]; + scanf("%d%d%s", &n, &m, buf); + int rt = segInit(buf - 1, 1, n); + for(int i = 1; i <= m; ++i) { + int typ, u, v; + scanf("%d", &typ); + if(typ == 1 || typ == 2) { + scanf("%d%d", &u, &v); + pos[i] = ++etot; + e[etot] = (Event){i, m, typ - 1, u, v}; + } else if(typ == 3) { + scanf("%d", &u); + e[pos[u]].end = min(e[pos[u]].end, i); + } else { + scanf("%d%d", &u, &v); + que[++qtot] = (Query){u, v}; + val[qtot] = i; + } + } + for(int i = 1; i <= etot; ++i) { + int pL = lower_bound(val + 1, val + qtot + 1, e[i].sta) - val; + int pR = upper_bound(val + 1, val + qtot + 1, e[i].end) - val - 1; + if(1 <= pL && pL <= pR && pR <= qtot) + addEvent(1, qtot, pL, pR, i); + } + if(qtot) + solve(rt, 1, qtot); + for(int i = 1; i <= qtot; ++i) + printf("%d\n", val[i]); + return 0; +} diff --git a/self_training/2018-11-30/272F.cpp b/self_training/2018-11-30/272F.cpp new file mode 100644 index 00000000..1842117e --- /dev/null +++ b/self_training/2018-11-30/272F.cpp @@ -0,0 +1,79 @@ +#include +typedef long long LL; +const int maxn = (int)1e5 + 1, maxlen = 18, maxm = 1 << maxlen, mod = 998244353, gen = 3; +int w[maxm], inv2[maxlen + 1]; +int t, n, fact[maxn], f[maxm]; +inline int mod_pow(int x, int k) { + int ret = 1; + for( ; k; k >>= 1, x = (LL)x * x % mod) + if(k & 1) + ret = (LL)ret * x % mod; + return ret; +} +inline int mod_add(int x, int y) { + return x + y >= mod ? x + y - mod : x + y; +} +inline int mod_sub(int x, int y) { + return x - y < 0 ? x - y + mod : x - y; +} +inline void NTT(int len, int x[], int flag) { + static int bitLen, bitrev[maxm]; + for(bitLen = 0; 1 << bitLen < len; ++bitLen); + for(int i = 1; i < len; ++i) { + bitrev[i] = (bitrev[i >> 1] >> 1) | ((i & 1) << (bitLen - 1)); + if(i < bitrev[i]) + std::swap(x[i], x[bitrev[i]]); + } + for(int i = 1, d = 1; d < len; ++i, d <<= 1) + for(int j = 0; j < len; j += d << 1) + for(int k = 0; k < d; ++k) { + int t = (LL)w[(maxm >> i) * k] * x[j + k + d] % mod; + x[j + d + k] = mod_sub(x[j + k], t); + x[j + k] = mod_add(x[j + k], t); + } + if(flag == -1) { + std::reverse(x + 1, x + len); + int val = inv2[bitLen]; + for(int i = 0; i < len; ++i) + x[i] = (LL)x[i] * val % mod; + } +} +void PolyInv(int n, int cur[], int nxt[maxm]) { + int len; + static int tmp[maxm]; + nxt[0] = 1; + for(len = 2; (len >> 1) < n; len <<= 1) { + int lim = std::min(n, len); + memcpy(tmp, cur, lim * sizeof(int)); + memset(tmp + lim, 0, ((len << 1) - lim) * sizeof(int)); + NTT(len << 1, tmp, 1); + memset(nxt + (len >> 1), 0, ((len << 1) - (len >> 1)) * sizeof(int)); + NTT(len << 1, nxt, 1); + for(int i = 0; i < len << 1; ++i) + if((nxt[i] = (2 - (LL)tmp[i] * nxt[i]) % mod * nxt[i] % mod) < 0) + nxt[i] += mod; + NTT(len << 1, nxt, -1); + } +} +int main() { + w[0] = 1; + w[1] = mod_pow(gen, (mod - 1) >> maxlen); + for(int i = 2; i < maxm; ++i) + w[i] = (LL)w[i - 1] * w[1] % mod; + inv2[0] = 1; + inv2[1] = (mod + 1) >> 1; + for(int i = 2; i <= maxlen; ++i) + inv2[i] = (LL)inv2[i - 1] * inv2[1] % mod; + fact[0] = 1; + for(int i = 1; i < maxn; ++i) + fact[i] = (LL)fact[i - 1] * i % mod; + PolyInv(maxn, fact, f); + for(int i = 0; i < maxn; ++i) + (f[i] = fact[i] + f[i]) >= mod && (f[i] -= mod); + scanf("%d", &t); + while(t--) { + scanf("%d", &n); + printf("%d\n", f[n]); + } + return 0; +} diff --git a/self_training/2018-12-01/A.cpp b/self_training/2018-12-01/A.cpp new file mode 100644 index 00000000..6cf2f3d3 --- /dev/null +++ b/self_training/2018-12-01/A.cpp @@ -0,0 +1,117 @@ +#include +using namespace std; +typedef double DB; +const int maxt = 100; +const DB eps = 1e-9, INF = 1e100, pi = acos(-1.0); +inline int sgn(DB x) { + return (x > eps) - (x < -eps); +} +struct Point { + DB x, y; + Point operator - (Point const &t) const { + return (Point){x - t.x, y - t.y}; + } + DB dot(Point const &t) const { + return x * t.x + y * t.y; + } + DB len2() const { + return dot(*this); + } + DB len() const { + return sqrtl(len2()); + } +}; +inline DB solve(Point p, Point q, int len1) { + int s1 = sgn(p.y), s2 = sgn(q.y); + if(s1 < 0 || s2 < 0 || (!s1 && !s2) || sgn((q - p).len() - len1) != 0) + return INF; + return len1 / (sqrtl(5 * max(p.y, (DB)0)) + sqrtl(5 * max(q.y, (DB)0))); +} +inline DB solve(Point p, Point q, int len1, int len2) { + int s1 = sgn(p.y), s2 = sgn(q.y); + if(s1 < 0 || s2 < 0) + return INF; + Point vec = q - p; + DB dis = vec.len(); + int s3 = sgn(len1 + len2 + dis - max(dis, (DB)max(len1, len2)) * 2); + if(s3 < 0 || (!s1 && !s2 && !s3 && sgn(dis) > 0)) + return INF; + if(!sgn(dis)) + return 2 * solve(p, (Point){p.x, p.y + len1}, len1); + DB ang = atan2(vec.y, vec.x); + DB dt = acos(max(min((len1 * len1 + vec.len2() - len2 * len2) / (2 * len1 * dis), (DB)1), (DB)-1)); + auto calc = [&](DB angle) { + Point tp = (Point){p.x + len1 * cos(angle), p.y + len1 * sin(angle)}; + return min(solve(p, tp, len1) + solve(tp, q, len2), INF); + }; + return min(calc(ang - dt), calc(ang + dt)); +} +inline DB solve(Point p, Point q, int len1, int len2, int len3) { + int s1 = sgn(p.y), s2 = sgn(q.y); + if(s1 < 0 || s2 < 0) + return INF; + Point vec = q - p; + DB dis = vec.len(); + int s3 = sgn(len1 + len2 + len3 + dis - max(dis, (DB)max(len1, max(len2, len3))) * 2); + if(s3 < 0 || (!s1 && !s2 && !s3 && sgn(dis) > 0)) + return INF; + auto calc = [&](DB angle) { + Point tp = (Point){p.x + len1 * cos(angle), p.y + len1 * sin(angle)}; + return min(solve(p, tp, len1) + solve(tp, q, len2, len3), INF); + }; + auto ternarySearch = [&calc](DB L, DB R, bool chkL = 0) { + const DB rate = (sqrtl(5) + 1) / 2; + DB dt = (R - L) / rate; + DB ML = R - dt, fL = calc(ML); + DB MR = L + dt, fR = calc(MR); + for(int t = 0; t < maxt; ++t) + if((!chkL && fR >= INF / 2) || (!(chkL && fL >= INF / 2) && fL < fR)) { + R = MR; + MR = ML; + fR = fL; + dt = (R - L) / rate; + ML = R - dt; + fL = calc(ML); + } else { + L = ML; + ML = MR; + fL = fR; + dt = (R - L) / rate; + MR = L + dt; + fR = calc(MR); + } + return !chkL ? fL : fR; + }; + if(!sgn(dis)) + return ternarySearch(0, pi / 2); + DB ang = atan2(vec.y, vec.x); + DB dt = acos(max(min((len1 * len1 + vec.len2() - (len2 + len3) * (len2 + len3)) / (2 * len1 * dis), (DB)1), (DB)-1)); + return min(ternarySearch(ang - dt, ang), ternarySearch(ang, ang + dt, 1)); +} +int main() { + int t, x, y, l[3]; + scanf("%d", &t); + while(t--) { + scanf("%d%d%d%d%d", &x, &y, l + 0, l + 1, l + 2); + if(y > 0) { + puts("Impossible!"); + continue; + } + Point p = (Point){0, 0}, q = (Point){(DB)x, (DB)-y}; + DB ans = INF; + for(int i = 0; i < 3; ++i) { + ans = min(ans, solve(p, q, l[i])); + for(int j = 0; j < 3; ++j) + if(i != j) { + ans = min(ans, solve(p, q, l[i], l[j])); + ans = min(ans, solve(p, q, l[i], l[j], l[3 - i - j])); + } + } + if(ans < INF / 2) { + printf("%.8f\n", (double)ans); + } else { + puts("Impossible!"); + } + } + return 0; +} diff --git a/self_training/2018-12-01/B.cpp b/self_training/2018-12-01/B.cpp new file mode 100644 index 00000000..869c27a2 --- /dev/null +++ b/self_training/2018-12-01/B.cpp @@ -0,0 +1,44 @@ +#include +using namespace std; +const int maxn = (int)1e6 + 1; +int t, n, a[maxn], b[maxn]; +bool check() { + for(int i = 1; i <= n; ++i) + b[i] = 0; + for(int o = 1; o <= 2; ++o) { + for(int i = o; i < n; i += 2) { + int u = a[i], v = a[i + 1]; + if(u > v) + swap(u, v); + b[u] = v; + } + set sp; + sp.insert(maxn); + for(int i = 1; i <= n; ++i) { + if(!b[i]) + continue; + int low = *sp.begin(); + while(low <= i) { + sp.erase(sp.begin()); + low = *sp.begin(); + } + if(b[i] > low) + return 0; + sp.insert(b[i]); + b[i] = 0; + } + } + return 1; +} +int main() { + scanf("%d", &t); + while(t--) { + scanf("%d", &n); + for(int i = 1, x; i <= n; ++i) { + scanf("%d", &x); + a[x] = i; + } + puts(check() ? "Yes" : "No"); + } + return 0; +} \ No newline at end of file diff --git a/self_training/2018-12-01/C.cpp b/self_training/2018-12-01/C.cpp new file mode 100644 index 00000000..5ee4d8f5 --- /dev/null +++ b/self_training/2018-12-01/C.cpp @@ -0,0 +1,62 @@ +#include +using namespace std; +const int maxn = (int)1e6 + 1; +int t, n, fa[maxn], w[maxn], ord[maxn], rem[maxn], sg[maxn]; +bool vis[maxn]; +int main() { + scanf("%d", &t); + while(t--) { + scanf("%d", &n); + memset(rem + 1, 0, n * sizeof(int)); + for(int i = 2; i <= n; ++i) { + scanf("%d%d", fa + i, w + i); + ++rem[fa[i]]; + } + int tot = 0; + for(int i = 1; i <= n; ++i) { + if(!rem[i]) + ord[++tot] = i; + sg[i] = vis[i] = 0; + } + for(int i = 1; i <= tot; ++i) { + int u = ord[i], p = fa[u]; + if(!p) + continue; + if(!(--rem[p])) + ord[++tot] = p; + sg[p] ^= w[u] == 1 ? sg[u] + 1 : sg[u] ^ (w[u] & 1); + } + tot = 0; + rem[1] = 0; + for(int i = n - 1; i >= 0; --i) { + int u = ord[i], p = fa[u]; + if(rem[p] == -1) { + rem[u] = -1; + continue; + } + rem[u] = rem[p] ^ sg[p] ^ (w[u] == 1 ? sg[u] + 1 : sg[u] ^ (w[u] & 1)); + int tmp = w[u] == 1 ? 0 : (w[u] == 2 ? sg[u] + 1 : sg[u] ^ !(w[u] & 1)); + if(rem[u] == tmp) { + ++tot; + vis[u] = 1; + } + if(w[u] == 1) { + --rem[u]; + } else { + rem[u] ^= w[u] & 1; + } + } + printf("%d\n", tot); + for(int i = 1, fir = 1; i <= n; ++i) + if(vis[i]) { + if(fir) { + fir = 0; + } else { + putchar(' '); + } + printf("%d", i); + } + putchar('\n'); + } + return 0; +} \ No newline at end of file diff --git a/self_training/2018-12-01/D.cpp b/self_training/2018-12-01/D.cpp new file mode 100644 index 00000000..c2a29eea --- /dev/null +++ b/self_training/2018-12-01/D.cpp @@ -0,0 +1,45 @@ +#include +using namespace std; +const int maxn = (int)501, mod = (int)1e9 + 7; +int t, n, cur, pre = 1; +char buf[maxn]; +inline void mod_inc(int &x, int y) { + (x += y) >= mod && (x -= mod); +} +struct Info { + int cnt, sum; + void update(Info const &t, int adt) { // adt <= 2 + mod_inc(cnt, t.cnt); + mod_inc(sum, t.sum); + for( ; adt--; mod_inc(sum, t.cnt)); + } +} f[2][2][3]; +int main() { + scanf("%d", &t); + while(t--) { + scanf("%s", buf); + n = strlen(buf); + memset(f[cur], 0, sizeof f[0]); + f[cur][0][0] = f[cur][0][2] = f[cur][1][0] = f[cur][1][2] = (Info){1, 0}; + for(int i = 1; i <= n; ++i) { + int o = buf[n - i] - '0'; + swap(cur, pre); + memset(f[cur], 0, sizeof f[0]); + for(int j = 0; j < 2; ++j) { + int xL = 0, xR = !j ? o : 1; + for(int k = 0; k < 3; ++k) + for(int x = xL; x <= xR; ++x) { + int yL = k & 1 ? x : 0, yR = !k ? x : 1; + for(int y = yL; y <= yR; ++y) { + int msk = !k ? 1 | (x > y) << 1 : (k & 1 ? 1 << (1 + (x < y)) : 1 << 2); + for(int p = 0; p < 3; ++p) + if((msk >> p) & 1) + f[cur][j][k].update(f[pre][j | (x < o)][p], x - y + (k > 0)); + } + } + } + } + printf("%d\n", f[cur][0][0].sum); + } + return 0; +} \ No newline at end of file diff --git a/self_training/2018-12-01/E.cpp b/self_training/2018-12-01/E.cpp new file mode 100644 index 00000000..5c0472ed --- /dev/null +++ b/self_training/2018-12-01/E.cpp @@ -0,0 +1,42 @@ +#include +using namespace std; +typedef long long LL; +const int maxn = (int)1e6 + 1; +int t, n; +LL m, dt[maxn], low[maxn], upp[maxn]; +bool check(LL tot) { + low[0] = upp[0] = m; + for(int i = 1; i < n; ++i) { + low[i] = m - min(tot - m - max(dt[i] - upp[i - 1], 0LL), m); + upp[i] = m - max(dt[i] - (m - low[i - 1]), 0LL); + if(low[i] > upp[i]) + return 0; + } + return m - low[n - 1] >= dt[0]; +} +int main() { + scanf("%d", &t); + while(t--) { + scanf("%d%lld", &n, &m); + LL L = m, R = m; + for(int i = 0; i < n; ++i) { + scanf("%lld", dt + i); + L = max(L, m + dt[i]); + R = max(R, m + dt[i] + dt[i]); + } + if(R + R > L + L + L) { + puts("-1"); + continue; + } + while(L < R) { + LL M = (L + R) >> 1; + if(check(M)) { + R = M; + } else { + L = M + 1; + } + } + printf("%lld\n", L); + } + return 0; +} \ No newline at end of file diff --git a/self_training/2018-12-01/F.cpp b/self_training/2018-12-01/F.cpp new file mode 100644 index 00000000..fddd20e6 --- /dev/null +++ b/self_training/2018-12-01/F.cpp @@ -0,0 +1,147 @@ +#include +using namespace std; +typedef long long LL; +int mod_sqrt(int x, int p) { + if(!x || p <= 2) + return x; + auto mod_pow = [&p](int x, int k) { // 0 <= x < p, k >= 0 + int ret = 1; + for( ; k > 0; k >>= 1, x = (LL)x * x % p) + if(k & 1) + ret = (LL)ret * x % p; + return ret; + }; + if(mod_pow(x, (p - 1) / 2) != 1) + return -1; + int w, y; + do { + w = rand() % (p - 1) + 1; + y = ((LL)w * w + p - x) % p; + } while(mod_pow(y, (p - 1) / 2) == 1); + auto mod_mul = [&p, &y](pair const &u, pair const &v) { + int fir = ((LL)u.first * v.first + (LL)u.second * v.second % p * y) % p; + int sec = ((LL)u.first * v.second + (LL)u.second * v.first) % p; + return make_pair(fir, sec); + }; + pair ret = make_pair(1, 0), tmp = make_pair(w, 1); + for(int k = (p + 1) / 2; k > 0; k >>= 1, tmp = mod_mul(tmp, tmp)) + if(k & 1) + ret = mod_mul(ret, tmp); + return ret.first; +} +const int maxn = (int)1e4 + 1, maxc = 9, maxe = 27, maxs = 1 << 6 | 1, mod = (int)1e9 + 7; +int ptot, pr[maxn], d[maxn]; +int t, n, m, s, cnt, lim[maxc], sz[maxc], ans; +pair pp[maxc], tr[maxc][maxe], ways[maxc][maxs]; +void pfs(int dep, int val, int upp) { + if(dep == cnt) { + int rem = upp / val, &csz = sz[cnt]; + pair *vec = ways[cnt]; + for(int i = 0; i < csz; ++i) { + int dx = val * vec[i].first + rem * vec[i].second; + int dy = max(val * vec[i].second, rem * vec[i].first); + int adt = 0; + if(dx <= n && dy <= m) + adt = (adt + (n - dx + 1LL) * (m - dy + 1)) % mod; + if(dx <= m && dy <= n) + adt = (adt + (m - dx + 1LL) * (n - dy + 1)) % mod; + if(vec[i].second > 1) + (adt += adt) >= mod && (adt -= mod); + (ans += adt) >= mod && (ans -= mod); + } + return; + } + pfs(dep + 1, val, upp); + for(int i = 1; i <= pp[dep].second; ++i) + pfs(dep + 1, val *= pp[dep].first, upp); +} +void dfs(int dep, int val) { + if(dep == cnt) { + pfs(0, 1, s / val); + return; + } + sz[dep + 1] = sz[dep]; + memcpy(ways[dep + 1], ways[dep], sz[dep] * sizeof(pair)); + dfs(dep + 1, val); + for(int i = 1; i <= lim[dep]; ++i) { + int &csz = sz[dep], &nsz = sz[dep + 1] = 0; + pair *cur = ways[dep], *nxt = ways[dep + 1]; + auto append = [&nsz, &nxt](int u, int v) { + if(u > v) + swap(u, v); + nxt[nsz++] = make_pair(u, v); + }; + int a = tr[dep][i].first, b = tr[dep][i].second; + for(int j = 0; j < csz; ++j) { + int c = cur[j].first, d = cur[j].second; + append(a * c + b * d, abs(a * d - b * c)); + append(a * d + b * c, abs(a * c - b * d)); + } + sort(nxt, nxt + nsz); + nsz = unique(nxt, nxt + nsz) - nxt; + --pp[dep].second; + dfs(dep + 1, val *= pp[dep].first); + } + pp[dep].second += lim[dep]; +} +int main() { + for(int i = 2; i < maxn; ++i) { + if(!d[i]) + pr[ptot++] = d[i] = i; + for(int j = 0, k; (k = i * pr[j]) < maxn; ++j) { + d[k] = pr[j]; + if(d[i] == pr[j]) + break; + } + } + pr[ptot] = maxn; + scanf("%d", &t); + while(t--) { + scanf("%d%d%d", &n, &m, &s); + int tmp = s; + cnt = ans = 0; + for(int i = 0; pr[i] * pr[i] <= tmp; ++i) + if(tmp % pr[i] == 0) { + int ex = 0; + for(tmp /= pr[i], ++ex; tmp % pr[i] == 0; tmp /= pr[i], ++ex); + pp[cnt++] = make_pair(pr[i], ex); + } + if(tmp > 1) + pp[cnt++] = make_pair(tmp, 1); + for(int i = 0; i < cnt; ++i) + if(pp[i].first == 2) { + lim[i] = 1; + tr[i][1] = make_pair(1, 1); + } else if((pp[i].first & 3) == 3) { + lim[i] = 0; + } else { // (pp[i].first & 3) == 1 + lim[i] = pp[i].second; + int a = pp[i].first, b = mod_sqrt(pp[i].first - 1, pp[i].first); + for( ; (LL)b * b > pp[i].first; swap(a, b), b %= a); + int tmp = pp[i].first - b * b; + a = (int)sqrt(tmp); + for( ; a * a < tmp; ++a); + for( ; a * a > tmp; --a); + if(a > b) + swap(a, b); + tr[i][1] = make_pair(a, b); + for(int j = 2; j <= lim[i]; ++j) { + int c = tr[i][j - 1].first, d = tr[i][j - 1].second; + int e = a * c + b * d, f = abs(a * d - b * c); + if(__gcd(e, f) > 1) { + e = a * d + b * c; + f = abs(a * c - b * d); + } + if(e > f) + swap(e, f); + tr[i][j] = make_pair(e, f); + } + } + sz[0] = 1; + ways[0][0] = make_pair(0, 1); + dfs(0, 1); + (ans += ans) >= mod && (ans -= mod); + printf("%d\n", ans); + } + return 0; +} diff --git a/self_training/2018-12-01/G.cpp b/self_training/2018-12-01/G.cpp new file mode 100644 index 00000000..072d802c --- /dev/null +++ b/self_training/2018-12-01/G.cpp @@ -0,0 +1,124 @@ +#include +using namespace std; +const int maxn = (int)1e6 + 9, maxl = 31; +long long ans; +int t, n, f[maxn], h[maxn]; +char buf[maxn]; +typedef struct { int fir, dif, las; } Border; +void append(vector &vec, int perL, int perR) { + if(!vec.size()) { + vec.push_back((Border){perL, perL, perR}); + return; + } + Border &tail = vec.back(); + int dif = perL - tail.las; + if(dif == tail.dif) { + tail.las = perR; + } else { + vec.push_back((Border){perL, dif, perR}); + } +} +int exgcd(int a, int b, int &x, int &y) { + int sx = 1, sy = 0, tx = 0, ty = 1; + while(b) { + int q = a / b; + swap(a, b); + b -= a * q; + swap(sx, tx); + tx -= sx * q; + swap(sy, ty); + ty -= sy * q; + } + x = sx; + y = sy; + return a; +} +int calc(Border &u, Border &v, int w) { + if(max(max(u.fir, v.fir), w + 1) > min(u.las, v.las)) + return 0; + int uL = u.fir <= w ? (w - u.fir) / u.dif + 1 : 0, uR = (u.las - u.fir) / u.dif; + int vL = (v.fir - v.las) / v.dif, vR = v.fir <= w ? -((w - v.fir) / v.fir + 1) : 0; + if(uL > uR || vL > vR) + return 0; + if(uL == uR) { + int tmp = u.fir + uL * u.dif; + return v.fir <= tmp && tmp <= v.las && (tmp - v.fir) % v.dif == 0; + } + if(vL == vR) { + int tmp = v.fir - vL * v.dif; + return u.fir <= tmp && tmp <= u.las && (tmp - u.fir) % u.dif == 0; + } + int x, y, r = exgcd(u.dif, v.dif, x, y), adt = u.fir - v.fir; + if(adt % r != 0) + return 0; + long long low = -maxn, upp = maxn; + auto check = [&low, &upp](long long L, long long R, int d) { + low = max(low, L <= 0 ? L / d : (L - 1) / d + 1); + upp = min(upp, R >= 0 ? R / d : (R + 1) / d - 1); + }; + check(uL - (long long)x * (-adt / r), uR - (long long)x * (-adt / r), v.dif / r); + check((long long)y * (-adt / r) - vR, (long long)y * (-adt / r) - vL, u.dif / r); + return low <= upp ? upp - low + 1 : 0; +} +int main() { + scanf("%d", &t); + while(t--) { + scanf("%s", buf); + n = strlen(buf); + ans = -1; + // appeared once + for(int i = 0, mx = -1, id; i < n; ++i) { + h[i] = i < mx ? min(mx - i, h[id + id - i]) : 0; + for( ; i >= h[i] && i + h[i] + 1 < n && buf[i + h[i] + 1] == buf[i - h[i]]; ++h[i]); + if(mx < i + h[i]) { + mx = i + h[i]; + id = i; + } + } + for(int i = 1, j = 0, c = 0; i <= n; ++i) { + for( ; j < i - j && n - i < i - j; ++j) + c += !j || j + h[j - 1] >= n || j - 1 - h[j - 1] < 0; + if(i + h[i - 1] >= n || i - 1 - h[i - 1] < 0) + ans += c; + } + // appeared at least twice + for(int i = 1, j = 0; i < n; ++i) { + for( ; j && buf[i] != buf[j]; j = f[j]); + f[i + 1] = buf[i] == buf[j] ? ++j : 0; + } + vector pat, pal; + for(int i = n; i; i = f[i]) { + int per = n - f[i]; + if(!(per & 1)) + append(pat, per, per); + } + for(int i = 1; i <= n; ++i) { + vector cur; + // append(cur, 1, 1); + if(i > 1 && buf[i - 2] == buf[i - 1]) + append(cur, 2, 2); + for(Border &it : pal) { + if(it.fir + 2 <= i && buf[i - it.fir - 2] == buf[i - 1]) + if(it.fir == it.las) { + append(cur, it.fir + 2, it.las + 2); + } else { + append(cur, it.fir + 2, it.las - it.dif + 2); + } + if(it.fir < it.las && it.las + 2 <= i && buf[i - it.las - 2] == buf[i - 1]) + append(cur, it.las + 2, it.las + 2); + } + pal.swap(cur); + int low = h[i - 1] << 1; + for(vector::iterator it = pat.begin(), jt = pal.begin(); it != pat.end() && jt != pal.end(); ++jt) { + for( ; it != pat.end() && it -> las <= jt -> las; ++it) + ans += calc(*it, *jt, low); + if(it != pat.end()) + ans += calc(*it, *jt, low); + } + for(vector::iterator it = pat.begin(); it != pat.end() && it -> fir <= low; ++it) + ans += (min(it -> las, low) - (it -> fir)) / (it -> dif) + 1; + } + printf("%lld\n", ans); + } + return 0; +} diff --git a/self_training/2018-12-01/H.cpp b/self_training/2018-12-01/H.cpp new file mode 100644 index 00000000..8277fcb6 --- /dev/null +++ b/self_training/2018-12-01/H.cpp @@ -0,0 +1,69 @@ +#include +using namespace std; +const int maxn = 101, INF = 1 << 25; +int t, n, m, q, e[maxn][maxn], p[maxn], cur, pre = 1; +__int128 f[2][maxn]; +pair<__int128, int> g[maxn]; +inline void upd(pair<__int128, int> &cur, __int128 val, int cnt) { + if(cur.first > val || (cur.first == val && cur.second < cnt)) + cur = make_pair(val, cnt); +} +int main() { + scanf("%d", &t); + while(t--) { + scanf("%d%d%d", &n, &m, &q); + for(int i = 1; i <= n; ++i) + for(int j = 1; j <= n; ++j) + e[i][j] = i == j ? 0 : INF; + while(m--) { + int u, v, w; + scanf("%d%d%d", &u, &v, &w); + if(e[u][v] > w) + e[u][v] = e[v][u] = w; + } + for(int i = 1; i <= q; ++i) + scanf("%d", p + i); + for(int k = 1; k <= n; ++k) + for(int i = 1; i <= n; ++i) + for(int j = 1; j <= n; ++j) + e[i][j] = min(e[i][j], e[i][k] + e[k][j]); + for(int i = 0; i <= q; ++i) + g[i] = make_pair((__int128)INF << i, 0); + upd(g[0], e[1][n], 0); + for(int i = 1; i <= q; ++i) { + f[cur][i] = e[1][p[i]]; + upd(g[1], (f[cur][i] << 1) + e[p[i]][n], 1); + for(int j = 1; j <= q; ++j) + if(i != j) + upd(g[0], f[cur][i] + e[p[i]][p[j]], INF); + } + for(int i = 2; i <= q; ++i) { + swap(cur, pre); + for(int j = 1; j <= q; ++j) { + f[cur][j] = (__int128)INF << i; + for(int k = 1; k <= q; ++k) + if(j != k) + f[cur][j] = min(f[cur][j], (f[pre][k] << 1) + e[p[k]][p[j]]); + upd(g[i], (f[cur][j] << 1) + e[p[j]][n], i); + for(int k = 1; k <= q; ++k) + if(j != k) + upd(g[i - 1], f[cur][j] + e[p[j]][p[k]], INF); + } + } + int pos = 0; + for(int i = 1; i <= q; ++i) { + __int128 tmp = g[pos].first << (i - pos); + if(g[i].first < tmp || (g[i].first == tmp && g[i].second > g[pos].second)) + pos = i; + } + double ans = g[pos].first; + for(int i = 0; i < pos; ++i) + ans /= 2; + if(g[pos].second < INF) { + printf("%.20f %d\n", (double)ans, g[pos].second); + } else { + printf("%.20f Burst!\n", (double)ans); + } + } + return 0; +} diff --git a/self_training/2018-12-01/I.cpp b/self_training/2018-12-01/I.cpp new file mode 100644 index 00000000..bfc64fb9 --- /dev/null +++ b/self_training/2018-12-01/I.cpp @@ -0,0 +1,68 @@ +#include +using namespace std; +const int maxn = (int)1e6 + 1; +int main() { + int t; + scanf("%d", &t); + while(t--) { + static char buf[maxn]; + scanf("%s", buf); + int c[3] = {}; + for(int i = 0; buf[i]; ++i) + ++c[buf[i] - '0']; + int mx = max(c[0], max(c[1], c[2])), sum = c[0] + c[1] + c[2]; + if(mx > sum - mx + 1) { + puts("-1"); + } else { + int pos = -1, op[3] = {}, rem[3] = {c[0] - 1, c[1] - 1, c[2] - 1}; + if(mx == sum - mx + 1 && mx >= 2) + if(mx == c[0]) { // c[1] + c[2] + pos = 0; + --rem[0]; + } else if(mx == c[1] && c[2]) { + pos = 1; + --rem[1]; + } + while(rem[0] > 0 || rem[1] > 0 || rem[2] > 0) { + int low = min(rem[0], min(rem[1], rem[2])); + int i = low == rem[2] ? 2 : (low == rem[0] ? 0 : 1); + int j = !i, k = 3 - i - j; + ++op[i]; + --rem[j]; + --rem[k]; + } + printf("%d\n", op[0] + op[1] + op[2] + (pos >= 0)); + for(int i = 0; i < op[0]; ++i) { + int x = c[0] + c[1]; + printf("%d %d\n", x, x + 1); + --c[1]; + --c[2]; + } + if(pos == 1) { + int x = c[0] + c[1]; + printf("%d %d\n", x, x); + --c[1]; + } + for(int i = 0; i < op[2]; ++i) { + int x = c[0]; + printf("%d %d\n", x, x + 1); + --c[0]; + --c[1]; + } + assert(c[1] <= 1); + for(int i = 0; i < op[1]; ++i) { + int x = c[0], y = c[0] + c[1]; + printf("%d %d\n", x, y + 1); + --c[0]; + c[1] = 0; + --c[2]; + } + if(!pos) { + int x = c[0]; + printf("%d %d\n", x, x); + --c[0]; + } + } + } + return 0; +} \ No newline at end of file diff --git a/self_training/2018-12-01/J.cpp b/self_training/2018-12-01/J.cpp new file mode 100644 index 00000000..f9a1b9fd --- /dev/null +++ b/self_training/2018-12-01/J.cpp @@ -0,0 +1,119 @@ +#include +using namespace std; +typedef long long LL; +const int maxn = (int)1e5 + 1; +int t, n, tot, w[maxn], ord[maxn], fa[maxn]; +pair msk[maxn]; +vector e[maxn]; +bool ban[maxn]; +void bfs(int rt) { // fa[rt] was set + tot = 0; + ord[tot++] = rt; + for(int i = 0; i < tot; ++i) { + int u = ord[i]; + for(auto &v : e[u]) + if(v != fa[u] && !ban[v]) { + fa[v] = u; + ord[tot++] = v; + } + } +} +int getRoot(int rt) { + int pos = -1, cnt = maxn; + static int sz[maxn]; + bfs(rt); + for(int i = tot - 1; i >= 0; --i) { + int u = ord[i], mx = 0; + sz[u] = 1; + for(auto &v : e[u]) + if(v != fa[u] && !ban[v]) { + sz[u] += sz[v]; + mx = max(mx, sz[v]); + } + mx = max(mx, tot - sz[u]); + if(mx < cnt) { + pos = u; + cnt = mx; + } + } + return pos; +} +LL solve(int m, int ord[]) { + LL ret = 0; + int cnt[3][3] = {}, eq[3][3] = {}; + sort(ord, ord + m, [&w](int const &u, int const &v) { + return w[u] < w[v]; + }); + for(int i = 0; i < m; ) { + for(int ww = w[ord[i]]; i < m && w[ord[i]] == ww; ++i) { + pair mask = msk[ord[i]]; + for(int x = 0; x < 3; ++x) + for(int y = 0; y < 3; ++y) { + if(x + mask.second < 1 || y + mask.first <= 1) + ret += cnt[x][y]; + if(x + mask.second <= 1 || y + mask.first <= 1) + ret += eq[x][y]; + } + ++eq[mask.first][mask.second]; + } + for(int x = 0; x < 3; ++x) + for(int y = 0; y < 3; ++y) { + cnt[x][y] += eq[x][y]; + eq[x][y] = 0; + } + } + return ret; +} +LL dfs(int rt) { + LL ret = 1; + fa[rt] = -1; + rt = getRoot(rt); + ban[rt] = 1; + int qtot = 0; + static int que[maxn]; + msk[rt] = make_pair(0, 0); + que[qtot++] = rt; + for(auto &u : e[rt]) { + if(ban[u]) + continue; + fa[u] = rt; + bfs(u); + for(int i = 0; i < tot; ++i) { + int u = ord[i], p = fa[u]; + msk[u] = msk[p]; + if(w[p] < w[u]) + msk[u].first = min(msk[u].first + 1, 2); + else if(w[p] > w[u]) + msk[u].second = min(msk[u].second + 1, 2); + que[qtot++] = u; + } + LL tmp = solve(tot, ord); + ret -= tmp; + } + LL tmp = solve(qtot, que); + ret += tmp; + for(auto &u : e[rt]) + if(!ban[u]) + ret += dfs(u); + return ret; +} +int main() { + scanf("%d", &t); + while(t--) { + scanf("%d", &n); + for(int i = 1; i <= n; ++i) + scanf("%d", w + i); + for(int i = 1; i < n; ++i) { + int u, v; + scanf("%d%d", &u, &v); + e[u].push_back(v); + e[v].push_back(u); + } + printf("%lld\n", dfs(1)); + for(int i = 1; i <= n; ++i) { + ban[i] = 0; + vector().swap(e[i]); + } + } + return 0; +} \ No newline at end of file diff --git a/self_training/2018-12-08/ADIMAT.cpp b/self_training/2018-12-08/ADIMAT.cpp new file mode 100644 index 00000000..1e5dc549 --- /dev/null +++ b/self_training/2018-12-08/ADIMAT.cpp @@ -0,0 +1,51 @@ +#include +using namespace std; +typedef long long LL; +const int maxn = 551, mod = (int)1e9 + 7; +int n, m, pw[maxn][maxn], inv[maxn], path[maxn], f[maxn], g[maxn], ans; +void dfs(int cnt, int sum, int low, int ways) { + if(sum == n) { + for(int i = 1; i <= m; ++i) + f[i] = 1; + for(int i = 0; i < cnt; ++i) { + int *coeff = pw[path[i]]; + for(int j = 1; j <= m; ++j) + f[j] = (LL)f[j] * coeff[j] % mod; + } + memcpy(g + 1, f + 1, m * sizeof(int)); + for(int i = 1; i <= m; ++i) { + for(int j = 1; j <= i; ++j) + g[i] = (g[i] + (LL)f[j] * g[i - j]) % mod; + g[i] = (LL)g[i] * inv[i] % mod; + } + ans = (ans + (LL)ways * g[m]) % mod; + return; + } + for(int i = low; sum + i <= n; ++i) { + if(sum + i < n && sum + i + i > n) + continue; + path[cnt] = i; + int ctr = 0; + for(int j = cnt; j >= 0 && path[j] == i; --j, ctr += i); + dfs(cnt + 1, sum + i, i, (LL)ways * inv[ctr] % mod); + } +} +int main() { + pw[0][0] = 1; + for(int i = 1; i < maxn; ++i) { + (pw[0][i] = pw[0][i - 1] << 1) >= mod && (pw[0][i] -= mod); + pw[i][0] = pw[0][i]; + } + for(int i = 1; i < maxn; ++i) + for(int j = i; j < maxn; ++j) + pw[i][j] = pw[j][i] = pw[i][j - i]; + inv[1] = 1; + for(int i = 2; i < maxn; ++i) + inv[i] = mod - (int)(mod / i * (LL)inv[mod % i] % mod); + scanf("%d%d", &n, &m); + if(n > m) + swap(n, m); + dfs(0, 0, 1, 1); + printf("%d\n", ans); + return 0; +} \ No newline at end of file diff --git a/self_training/2018-12-08/ADITREE.cpp b/self_training/2018-12-08/ADITREE.cpp new file mode 100644 index 00000000..c8ed9c97 --- /dev/null +++ b/self_training/2018-12-08/ADITREE.cpp @@ -0,0 +1,109 @@ +#include +using namespace std; +const int maxn = (int)2.5e5 + 1; +int n, m, ord[maxn]; +int lnk[maxn], dep[maxn], fa[maxn], sz[maxn]; +int seq[maxn], idx[maxn], son[maxn], top[maxn]; +struct Edge { + int nxt, v; +} e[maxn << 1 | 1]; +void tree_build(int rt) { + int L = 1, R = 0; + dep[rt] = fa[rt] = 0; + ord[++R] = rt; + while(L <= R) { + int u = ord[L++]; + for(int it = lnk[u]; it != -1; it = e[it].nxt) { + int v = e[it].v; + if(v == fa[u]) + continue; + dep[v] = dep[u] + 1; + fa[v] = u; + ord[++R] = v; + } + } + memset(sz + 1, 0, n * sizeof(int)); + memset(son + 1, 0, n * sizeof(int)); + ++sz[rt]; + for(int i = n; i > 1; --i) { + int u = ord[i], p = fa[u]; + sz[p] += (++sz[u]); + if(sz[son[p]] < sz[u]) + son[p] = u; + } + top[rt] = rt, idx[rt] = 1; + for(int i = 1; i <= n; ++i) { + int u = ord[i], id = idx[u]; + seq[id++] = u; + if(sz[u] == 1) + continue; + top[son[u]] = top[u]; + idx[son[u]] = id; + id += sz[son[u]]; + for(int it = lnk[u]; it != -1; it = e[it].nxt) { + int v = e[it].v; + if(v == fa[u] || v == son[u]) + continue; + top[v] = v; + idx[v] = id; + id += sz[v]; + } + } +} +int seg[maxn << 1 | 1]; +bool rev[maxn << 1 | 1]; +inline int seg_idx(int L, int R) { + return (L + R) | (L < R); +} +void seg_upd(int L, int R, int l, int r) { + int rt = seg_idx(L, R); + if(l <= L && R <= r) { + seg[rt] = R - L + 1 - seg[rt]; + rev[rt] = !rev[rt]; + return; + } + int M = (L + R) >> 1, lch = seg_idx(L, M), rch = seg_idx(M + 1, R); + if(rev[rt]) { + seg[lch] = M - L + 1 - seg[lch]; + rev[lch] = !rev[lch]; + seg[rch] = R - M - seg[rch]; + rev[rch] = !rev[rch]; + rev[rt] = 0; + } + if(l <= M) + seg_upd(L, M, l, r); + if(r > M) + seg_upd(M + 1, R, l, r); + seg[rt] = seg[lch] + seg[rch]; +} +int main() { + scanf("%d", &n); + memset(lnk + 1, -1, n * sizeof(int)); + for(int i = 0; i < n - 1; ++i) { + int u, v; + scanf("%d%d", &u, &v); + e[i << 1] = (Edge){lnk[u], v}; + lnk[u] = i << 1; + e[i << 1 | 1] = (Edge){lnk[v], u}; + lnk[v] = i << 1 | 1; + } + tree_build(1); + scanf("%d", &m); + for(int i = 1; i <= m; ++i) { + int u, v; + scanf("%d%d", &u, &v); + for(int pu = top[u], pv = top[v]; pu != pv; u = fa[pu], pu = top[u]) { + if(dep[pu] < dep[pv]) { + swap(u, v); + swap(pu, pv); + } + seg_upd(1, n, idx[pu], idx[u]); + } + if(dep[u] > dep[v]) + swap(u, v); + if(u != v) + seg_upd(1, n, idx[u] + 1, idx[v]); + printf("%d\n", seg[seg_idx(1, n)]); + } + return 0; +} \ No newline at end of file diff --git a/self_training/2018-12-08/ADJLEAF2.cpp b/self_training/2018-12-08/ADJLEAF2.cpp new file mode 100644 index 00000000..f1a959ee --- /dev/null +++ b/self_training/2018-12-08/ADJLEAF2.cpp @@ -0,0 +1,123 @@ +#include +using namespace std; +const int maxn = (int)5e5 + 1, maxd = 19; +int n, m, ord[maxn], dep[maxn], fa[maxd][maxn], qtot, que[maxn], stot, stk[maxn], gfa[maxn], f[maxn]; +vector e[maxn], g[maxn]; +bool ban[maxd][maxn]; +void pfs(int u) { + ord[u] = ++qtot; + for(int v : e[u]) { + if(v == fa[0][u]) + continue; + dep[v] = dep[u] + 1; + ban[0][v] = e[v].size() >= 3; + fa[0][v] = u; + for(int i = 1; i < maxd && fa[i - 1][v]; ++i) { + ban[i][v] = ban[i - 1][v] || ban[i - 1][fa[i - 1][v]]; + fa[i][v] = fa[i - 1][fa[i - 1][v]]; + } + pfs(v); + } +} +int lca(int u, int v) { + if(dep[u] < dep[v]) + swap(u, v); + for(int i = dep[u] - dep[v], j = 0; i > 0; i >>= 1, ++j) + (i & 1) && (u = fa[j][u]); + if(u == v) + return u; + for(int i = maxd - 1; i >= 0; --i) + if(fa[i][u] != fa[i][v]) { + u = fa[i][u]; + v = fa[i][v]; + } + return fa[0][u]; +} +bool check(int u, int v) { + int p = lca(u, v); + if(u != p && v != p && ban[0][p]) + return 1; + u = fa[0][u]; + for(int i = dep[u] - dep[p], j = 0; i > 0; i >>= 1, ++j) + if(i & 1) { + if(ban[j][u]) + return 1; + u = fa[j][u]; + } + v = fa[0][v]; + for(int i = dep[v] - dep[p], j = 0; i > 0; i >>= 1, ++j) + if(i & 1) { + if(ban[j][v]) + return 1; + v = fa[j][v]; + } + return 0; +} +void dfs(int u, int p) { + int ctr1 = 0, ctr2 = 0, ctr4 = 0; + for(int v : g[u]) { + if(v == p) + continue; + dfs(v, u); + if(!(f[v] & 1) || check(u, v)) { + ++ctr1; + if(f[v] & 2) + ++ctr2; + else + ctr2 = maxn; + } + if(f[v] & 4) + ++ctr4; + } + f[u] = (!ctr1 && e[u].size() == g[u].size()) | (ctr2 <= 1) << 1 | (ctr2 <= 2 || (ctr4 == 1 && g[u].size() - (p != -1) == 1)) << 2; + vector().swap(g[u]); +} +int main() { + scanf("%d%d", &n, &m); + for(int i = 1; i < n; ++i) { + int u, v; + scanf("%d%d", &u, &v); + e[u].push_back(v); + e[v].push_back(u); + } + dep[0] = -1; + ban[0][1] = e[1].size() >= 3; + pfs(1); + while(m--) { + int rt; + scanf("%d%d", &rt, &qtot); + for(int i = 0; i < qtot; ++i) + scanf("%d", que + i); + que[qtot++] = rt; + sort(que, que + qtot, [&ord](int const &u, int const &v) { + return ord[u] < ord[v]; + }); + stot = 0; + g[0].push_back(que[0]); + gfa[que[0]] = 0; + stk[++stot] = que[0]; + for(int i = 1, iLim = qtot; i < iLim; ++i) { + int p = stot ? lca(stk[stot], que[i]) : 0; + for( ; dep[stk[stot]] > dep[p]; --stot); + if(dep[stk[stot]] < dep[p]) { + int v = g[stk[stot]].back(); + gfa[p] = stk[stot]; + g[stk[stot]].back() = p; + gfa[v] = p; + g[p].push_back(v); + stk[++stot] = p; + que[qtot++] = p; + } + g[p].push_back(que[i]); + gfa[que[i]] = p; + stk[++stot] = que[i]; + } + vector().swap(g[0]); + for(int i = 0; i < qtot; ++i) + if(gfa[que[i]]) + g[que[i]].push_back(gfa[que[i]]); + dfs(rt, -1); + puts(f[rt] & 4 ? "YES" : "NO"); + } + return 0; +} diff --git a/self_training/2018-12-08/EBAIT.cpp b/self_training/2018-12-08/EBAIT.cpp new file mode 100644 index 00000000..70aa53ab --- /dev/null +++ b/self_training/2018-12-08/EBAIT.cpp @@ -0,0 +1,66 @@ +#include +using namespace std; +typedef long long LL; +const int maxn = (int)1e6 + 1, INF = 0x3f3f3f3f; +int t, n, xMax, yMax, fz, fm, tfz, tfm; +int main() { + scanf("%d", &t); + while(t--) { + scanf("%d%d%d", &n, &xMax, &yMax); + fz = tfz = tfm = 0; + fm = 1; + for(int i = 0, x; i < n; ++i) { + scanf("%d", &x); + (i & 1 ? tfz : tfm) += x; + if(i + 1 < n && (LL)fz * tfm < (LL)tfz * fm) { + fz = tfz; + fm = tfm; + } + } + if((LL)fz * tfm >= (LL)tfz * fm) { + puts("-1"); + continue; + } + int xL = 0, yL = 1, xR = 1, yR = 0; + while(1) { + int xM = xL + xR, yM = yL + yR; + if(xM > xMax || yM > yMax) + break; + if((LL)fz * yM < (LL)fm * xM) { + int L = 1, R = min(xL ? (xMax - xR) / xL : INF, yL ? (yMax - yR) / yL : INF); + while(L < R) { + int M = (L + R + 1) >> 1; + xM = M * xL + xR; + yM = M * yL + yR; + if((LL)fz * yM < (LL)fm * xM) { + L = M; + } else { + R = M - 1; + } + } + xR += L * xL; + yR += L * yL; + } else { + int L = 1, R = min(xR ? (xMax - xL) / xR : INF, yR ? (yMax - yL) / yR : INF); + while(L < R) { + int M = (L + R + 1) >> 1; + xM = xL + M * xR; + yM = yL + M * yR; + if((LL)fz * yM >= (LL)fm * xM) { + L = M; + } else { + R = M - 1; + } + } + xL += L * xR; + yL += L * yR; + } + } + if((LL)xR * tfm >= (LL)yR * tfz) { + puts("-1"); + } else { + printf("%d %d\n", xR, yR); + } + } + return 0; +} diff --git a/self_training/2018-12-08/GDIST.cpp b/self_training/2018-12-08/GDIST.cpp new file mode 100644 index 00000000..f86e09d6 --- /dev/null +++ b/self_training/2018-12-08/GDIST.cpp @@ -0,0 +1,114 @@ +#include +using namespace std; +#define int long long +const int maxn = 63, maxm = (int)1e4 + 1, INF = 0x3f3f3f3f3f3f3f3fLL; +int n, m, rt, f[maxn][maxn][maxn]; +vector > Ref[maxn]; +vector > e[maxn]; +vector > g[maxn]; +char buf[maxn]; +void dijkstra(int dis[maxn]) { + priority_queue > Q; + static bool vis[maxn]; + memset(vis + 1, 0, m * sizeof(bool)); + for(int i = 1; i <= m; ++i) + if(dis[i] < INF) + Q.push(make_pair(-dis[i], i)); + while(!Q.empty()) { + int u = Q.top().second; + Q.pop(); + if(vis[u]) + continue; + for(auto &it : g[u]) { + int v = it.first, w = dis[u] + it.second; + if(dis[v] > w) { + dis[v] = w; + Q.push(make_pair(-dis[v], v)); + } + } + } +} +signed main() { + int q; + scanf("%lld%lld", &m, &q); + static int dis[maxn]; + static bool vis[maxn]; + memset(dis + 1, 0x3f, m * sizeof(int)); + memset(vis + 1, 0, m * sizeof(bool)); + while(q--) { + int typ, x, y, z; + char ch; + vector vec; + scanf("%lld%lld", &typ, &x); + ++x; + vec.push_back(typ); + if(typ == 0) { + scanf("%lld%lld", &y, &z); + vec.push_back(++y); + vec.push_back(++z); + Ref[y].push_back(make_pair(x, z)); + Ref[z].push_back(make_pair(x, y)); + } else if(typ == 1) { + scanf(" %c", &ch); + vec.push_back((int)ch); + dis[x] = min(dis[x], 1LL); + } else { + dis[x] = min(dis[x], 0LL); + } + e[x].push_back(vec); + } + priority_queue > Q; + for(int i = 1; i <= m; ++i) + if(dis[i] < INF) + Q.push(make_pair(-dis[i], i)); + while(!Q.empty()) { + int u = Q.top().second; + Q.pop(); + if(vis[u]) + continue; + for(auto &it : Ref[u]) { + int x = it.first, v = it.second; + if(dis[x] > dis[u] + dis[v]) { + dis[x] = dis[u] + dis[v]; + Q.push(make_pair(-dis[x], x)); + } + } + } + scanf("%lld%s", &rt, buf + 1); + n = strlen(buf + 1); + memset(f, 0x3f, sizeof f); + for(int i = 1; i <= m; ++i) { + for(int j = 1; j <= n + 1; ++j) + f[j][j - 1][i] = min(f[j][j - 1][i], dis[i]); + for(auto &it : e[i]) + if(it[0] == 0) { + int u = it[1], v = it[2]; + if(dis[u] < INF) + g[v].push_back(make_pair(i, dis[u])); + if(dis[v] < INF) + g[u].push_back(make_pair(i, dis[v])); + } + } + for(int len = 1; len <= n; ++len) + for(int i = 1, j = i + len - 1; j <= n; ++i, ++j) { + static int ctr[257]; + memset(ctr, 0, sizeof ctr); + for(int k = i; k <= j; ++k) + ++ctr[(int)buf[k]]; + for(int k = 1; k <= m; ++k) + for(auto &it : e[k]) { + if(it[0] == 0) { + int lft = it[1], rht = it[2]; + for(int o = i; o < j; ++o) + f[i][j][k] = min(f[i][j][k], f[i][o][lft] + f[o + 1][j][rht]); + } + if(it[0] == 1) + f[i][j][k] = min(f[i][j][k], len - (ctr[it[1]] > 0 ? 1 : 0)); + if(it[0] == 2) + f[i][j][k] = min(f[i][j][k], len); + } + dijkstra(f[i][j]); + } + printf("%lld\n", f[1][n][++rt]); + return 0; +} diff --git a/self_training/2018-12-08/IMPO.cpp b/self_training/2018-12-08/IMPO.cpp new file mode 100644 index 00000000..b45fd61c --- /dev/null +++ b/self_training/2018-12-08/IMPO.cpp @@ -0,0 +1,78 @@ +#include +using namespace std; +typedef long long LL; +typedef long double DB; +const int maxn = (int)1e5 + 1; +int t, n, m, tot; +struct Point { + int x, y; + Point operator - (Point const &t) const { + return (Point){x - t.x, y - t.y}; + } + LL det(Point const &t) const { + return (LL)x * t.y - (LL)y * t.x; + } +} p[maxn], q[maxn]; +struct Event { + int typ, x; + DB a, b; + bool operator < (Event const &t) const { + return x < t.x; + } +} eve[maxn << 2 | 1]; +void readPolygon(int &n, Point p[], int &low, int &upp, int typ) { + LL area = 0; + scanf("%d%d%d", &n, &p[0].x, &p[0].y); + low = upp = p[0].x; + for(int i = 1; i < n; ++i) { + scanf("%d%d", &p[i].x, &p[i].y); + low = min(low, p[i].x); + upp = max(upp, p[i].x); + area += p[i - 1].det(p[i]); + } + area += p[n - 1].det(p[0]); + if(area < 0) + reverse(p, p + n); + for(int i = 0; i < n; ++i) { + int j = i ? i - 1 : n - 1; + if(p[i].x == p[j].x) + continue; + DB a = (DB)(p[i].y - p[j].y) / (p[i].x - p[j].x), b = p[i].y - a * p[i].x; + eve[tot++] = (Event){typ, p[i].x, a, b}; + eve[tot++] = (Event){typ, p[j].x, -a, -b}; + } +} +int main() { + scanf("%d", &t); + while(t--) { + int pxLow, pxUpp, qxLow, qxUpp; + tot = 0; + readPolygon(n, p, pxLow, pxUpp, 0); + readPolygon(m, q, qxLow, qxUpp, 1); + if(pxLow != qxLow || pxUpp != qxUpp) { + puts("-1"); + continue; + } + sort(eve, eve + tot); + DB a = 0, b = 0, c = 0, d = 0, ans = 0; + for(int i = 0; i < tot; ) { + int cur = eve[i].x; + for( ; i < tot && eve[i].x == cur; ++i) + if(!eve[i].typ) { + a += eve[i].a; + b += eve[i].b; + } else { + c += eve[i].a; + d += eve[i].b; + } + if(i < tot) { + int nxt = eve[i].x; + DB coeff3 = a * c / 3, coeff2 = (a * d + b * c) / 2, coeff1 = b * d; + ans += ((coeff3 * nxt + coeff2) * nxt + coeff1) * nxt; + ans -= ((coeff3 * cur + coeff2) * cur + coeff1) * cur; + } + } + printf("%.20f\n", (double)ans); + } + return 0; +} \ No newline at end of file diff --git a/self_training/2018-12-08/RECOVER.cpp b/self_training/2018-12-08/RECOVER.cpp new file mode 100644 index 00000000..73f8aa2e --- /dev/null +++ b/self_training/2018-12-08/RECOVER.cpp @@ -0,0 +1,139 @@ +#include +using namespace std; +const int maxn = 201, maxs = 40001, maxd = 13; +vector > dt; +int t, n, m, s, mat[maxn][maxn]; +set e[maxs]; +bool solve() { + static int ctr[maxd], deg[maxn][maxn]; + static bool vis[maxs]; + memset(ctr, 0, sizeof ctr); + for(int i = 1; i <= n; ++i) + for(int j = 1; j <= n; ++j) { + deg[i][j] = 0; + for(auto &it : dt) { + int x = i + it.first, y = j + it.second; + if(x < 1 || x > n || y < 1 || y > n) + continue; + ++deg[i][j]; + } + ++ctr[deg[i][j]]; + } + for(int i = 1; i <= s; ++i) { + if(e[i].size() >= maxd) + return 0; + --ctr[e[i].size()]; + vis[i] = 0; + } + for(int i = 1; i < maxd; ++i) + if(ctr[i]) + return 0; + if(n <= 2) { + for(int i = 1, k = 1; i <= n; ++i) + for(int j = 1; j <= n; ++j, ++k) + mat[i][j] = k; + } else { + auto calc = [&](int x1, int y1, int x2, int y2) { + int ret = 0; + for(auto &it : dt) { + int x3 = x2 + it.first, y3 = y2 + it.second; + if(x3 < 1 || x3 > n || y3 < 1 || y3 > n) + continue; + int dis = abs(x1 - x3) + abs(y1 - y3); + ret += dis == 1 || dis == 2; + } + return ret; + }; + for(int i = 1; i <= n; ++i) + for(int j = 1; j <= n; ++j) { + int p, q, r; + if(j == 1) { + p = i > 1 ? mat[i - 1][j + 1] : -1; + q = i > 1 ? mat[i - 1][j] : -1; + r = i > 1 ? calc(i, j, i - 1, j) : -1; + } else if(j == 2) { + p = i > 1 ? mat[i - 1][j] : -1; + q = mat[max(i - 1, 1)][j - 1]; + r = calc(i, j, max(i - 1, 1), j - 1); + } else { + p = mat[i][j - 1]; + q = mat[i][j - 2]; + r = calc(i, j, i, j - 2); + } + mat[i][j] = -1; + if(p == -1) { + for(int u = 1; u <= s; ++u) + if(!vis[u] && e[u].size() == deg[i][j]) { + if(r != -1) { + int ret = 0; + for(auto &v : e[u]) + ret += e[q].count(v); + if(ret != r) + continue; + } + mat[i][j] = u; + vis[u] = 1; + break; + } + } else { + for(auto &u : e[q]) { + if(!vis[u] && e[u].size() == deg[i][j] && e[p].count(u)) { + int ret = 0; + for(auto &v : e[u]) + ret += e[q].count(v); + if(ret != r) + continue; + mat[i][j] = u; + vis[u] = 1; + break; + } + } + } + if(mat[i][j] == -1) + return 0; + } + } + for(int i = 1; i <= n; ++i) + for(int j = 1; j <= n; ++j) { + int u = mat[i][j]; + for(auto &it : dt) { + int x = i + it.first, y = j + it.second; + if(x < 1 || x > n || y < 1 || y > n) + continue; + int v = mat[x][y]; + if(!e[u].count(v)) + return 0; + e[u].erase(v); + } + } + return 1; +} +int main() { + for(int x = -2; x <= 2; ++x) + for(int y = -2; y <= 2; ++y) { + int dis = abs(x) + abs(y); + if(dis == 1 || dis == 2) + dt.push_back(make_pair(x, y)); + } + scanf("%d", &t); + while(t--) { + scanf("%d%d", &n, &m); + s = n * n; + while(m--) { + int u, v; + scanf("%d%d", &u, &v); + e[u].insert(v); + e[v].insert(u); + } + if(solve()) { + for(int i = 1; i <= n; ++i) + for(int j = 1; j <= n; ++j) + printf("%d%c", mat[i][j], " \n"[j == n]); + } else { + puts("-1"); + } + for(int i = 1; i <= s; ++i) + set().swap(e[i]); + } + return 0; +} diff --git a/self_training/2018-12-08/RECTLIT.cpp b/self_training/2018-12-08/RECTLIT.cpp new file mode 100644 index 00000000..2dbd517d --- /dev/null +++ b/self_training/2018-12-08/RECTLIT.cpp @@ -0,0 +1,62 @@ +#include +using namespace std; +const int maxn = 11; +int t, n, m, x[maxn], y[maxn], qx[maxn], qy[maxn], c[maxn][maxn]; +int main() { + scanf("%d", &t); + while(t--) { + scanf("%d%d", &m, &n); + if(m > 4) { + puts("yes"); + while(m--) + scanf("%*d%*d"); + continue; + } + qx[0] = qy[0] = 0; + qx[++qx[0]] = qy[++qy[0]] = 0; + qx[++qx[0]] = qy[++qy[0]] = n; + for(int i = 0; i < m; ++i) { + scanf("%d%d", x + i, y + i); + qx[++qx[0]] = x[i]; + qx[++qx[0]] = x[i] + 1; + qy[++qy[0]] = y[i]; + qy[++qy[0]] = y[i] + 1; + } + sort(qx + 1, qx + qx[0] + 1); + qx[0] = unique(qx + 1, qx + qx[0] + 1) - qx - 2; + sort(qy + 1, qy + qy[0] + 1); + qy[0] = unique(qy + 1, qy + qy[0] + 1) - qy - 2; + for(int i = 0; i < m; ++i) { + x[i] = lower_bound(qx + 1, qx + qx[0] + 1, x[i]) - qx - 1; + y[i] = lower_bound(qy + 1, qy + qy[0] + 1, y[i]) - qy - 1; + } + bool chk = 0; + for(int i = 0; !chk && i < 1 << (m + m); ++i) { + for(int j = 0; j < qx[0]; ++j) + memset(c[j], 0, qy[0] * sizeof(int)); + for(int j = 0, tmp = i; j < m; ++j, tmp >>= 2) { + int xL = tmp & 1 ? x[j] : 0; + int xR = tmp & 1 ? qx[0] : x[j] + 1; + int yL = tmp & 2 ? y[j] : 0; + int yR = tmp & 2 ? qy[0] : y[j] + 1; + ++c[xL][yL]; + --c[xL][yR]; + --c[xR][yL]; + ++c[xR][yR]; + } + for(int j = 1; j < qx[0]; ++j) + for(int k = 0; k < qy[0]; ++k) + c[j][k] += c[j - 1][k]; + chk = 1; + for(int j = 0; chk && j < qx[0]; ++j) { + chk &= c[j][0] > 0; + for(int k = 1; chk && k < qy[0]; ++k) { + c[j][k] += c[j][k - 1]; + chk &= c[j][k] > 0; + } + } + } + puts(chk ? "yes" : "no"); + } + return 0; +} \ No newline at end of file diff --git a/self_training/2018-12-08/SFXPAL.cpp b/self_training/2018-12-08/SFXPAL.cpp new file mode 100644 index 00000000..00976f85 --- /dev/null +++ b/self_training/2018-12-08/SFXPAL.cpp @@ -0,0 +1,13 @@ +#include +using namespace std; +typedef long long LL; +const int maxn = (int)1e3 + 1; +int n, m, p, f[maxn]; +int main() { + scanf("%d%d%d", &n, &m, &p); + f[1] = m; + for(int i = 2; i <= n; ++i) + (f[i] = ((LL)m * f[i - 1] - f[(i + 1) >> 1]) % p) < 0 && (f[i] += p); + printf("%d\n", f[n]); + return 0; +} \ No newline at end of file diff --git a/self_training/2018-12-08/XORTABLE.cpp b/self_training/2018-12-08/XORTABLE.cpp new file mode 100644 index 00000000..c170894c --- /dev/null +++ b/self_training/2018-12-08/XORTABLE.cpp @@ -0,0 +1,106 @@ +#include +using namespace std; +const int maxn = (int)2e3 + 1, maxm = (int)1e6 + 1; +int t, n, m, low[maxn], upp[maxn], mat[maxn][maxn], ans[maxn], qtot, que[maxn], dis[maxn], etot, vtot, val[maxm], ctr[maxm]; +pair eve[maxm]; +bool vis[maxn]; +void addEvent(int dep, int lft, int low, int upp, int msk) { + int rht = lft + (1 << dep) - 1; + if(low <= lft && rht <= upp) { + int mask = (msk >> dep) << dep; + eve[etot++] = make_pair(lft ^ mask, rht ^ mask); + return; + } + int mid = lft + (1 << (dep - 1)); + if(low < mid) + addEvent(dep - 1, lft, low, upp, msk); + if(upp >= mid) + addEvent(dep - 1, mid, low, upp, msk); +} +int main() { + scanf("%d", &t); + while(t--) { + scanf("%d%d", &n, &m); + for(int i = 0; i < n + m; ++i) { + scanf("%d%d", low + i, upp + i); + vis[i] = 0; + } + for(int i = 0; i < n; ++i) + for(int j = 0; j < m; ++j) + scanf("%d", mat[i] + j); + bool chk = 1; + for(int u = 0; u < n + m; ++u) { + if(vis[u]) + continue; + qtot = 0; + vis[u] = 1; + dis[u] = 0; + que[qtot++] = u; + for(int i = 0; i < qtot; ++i) { + int u = que[i]; + if(u < n) { + for(int j = 0; chk && j < i; ++j) + chk &= que[j] < n || mat[u][que[j] - n] == -1 || mat[u][que[j] - n] == (dis[u] ^ dis[que[j]]); + if(!chk) + break; + for(int j = 0; j < m; ++j) + if(mat[u][j] != -1 && !vis[n + j]) { + vis[n + j] = 1; + dis[n + j] = dis[u] ^ mat[u][j]; + que[qtot++] = n + j; + } + } else { + for(int j = 0; chk && j < i; ++j) + chk &= que[j] >= n || mat[que[j]][u - n] == -1 || mat[que[j]][u - n] == (dis[u] ^ dis[que[j]]); + if(!chk) + break; + for(int j = 0; j < n; ++j) + if(mat[j][u - n] != -1 && !vis[j]) { + vis[j] = 1; + dis[j] = dis[u] ^ mat[j][u - n]; + que[qtot++] = j; + } + } + } + if(!chk) + break; + etot = 0; + for(int i = 0; i < qtot; ++i) + addEvent(30, 0, low[que[i]], upp[que[i]], dis[que[i]]); + vtot = 0; + for(int i = 0; i < etot; ++i) { + val[vtot++] = eve[i].first; + val[vtot++] = eve[i].second + 1; + } + sort(val, val + vtot); + vtot = unique(val, val + vtot) - val; + memset(ctr, 0, vtot * sizeof(int)); + for(int i = 0; i < etot; ++i) { + int L = lower_bound(val, val + vtot, eve[i].first) - val; + int R = lower_bound(val, val + vtot, eve[i].second + 1) - val; + ++ctr[L]; + --ctr[R]; + } + for(int i = 1; i < vtot; ++i) + ctr[i] += ctr[i - 1]; + chk = 0; + for(int i = 0; i < vtot; ++i) + if(ctr[i] == qtot) { + chk = 1; + for(int j = 0; j < qtot; ++j) + ans[que[j]] = val[i] ^ dis[que[j]]; + break; + } + if(!chk) + break; + } + if(chk) { + puts("YES"); + for(int i = 0; i < n + m; ++i) + printf("%d%c", ans[i], " \n"[i == n - 1 || i == n + m - 1]); + } else { + puts("NO"); + } + } + return 0; +} diff --git a/self_training/2019-04-06/A.cpp b/self_training/2019-04-06/A.cpp new file mode 100644 index 00000000..4f6f893c --- /dev/null +++ b/self_training/2019-04-06/A.cpp @@ -0,0 +1,23 @@ +#include +using namespace std; +const int maxn = 109; +int t, n, m; +char a[maxn], b[maxn]; +int main() { + scanf("%d", &t); + for(int Case = 1; Case <= t; ++Case) { + scanf("%s", a); + n = strlen(a); + for(int i = 0; i < n; ++i) { + b[i] = '0'; + if(a[i] == '4') { + --a[i]; + ++b[i]; + } + } + b[n] = '\0'; + for(m = 0; b[m] == '0'; ++m); + printf("Case #%d: %s %s\n", Case, a, b + m); + } + return 0; +} \ No newline at end of file diff --git a/self_training/2019-04-06/B.cpp b/self_training/2019-04-06/B.cpp new file mode 100644 index 00000000..b0a280f8 --- /dev/null +++ b/self_training/2019-04-06/B.cpp @@ -0,0 +1,16 @@ +#include +using namespace std; +const int maxn = (int)1e5 + 9; +int t, n; +char buf[maxn]; +int main() { + scanf("%d", &t); + for(int Case = 1; Case <= t; ++Case) { + scanf("%d%s", &n, buf); + n = (n - 1) * 2; + for(int i = 0; i < n; ++i) + buf[i] = 'S' - buf[i] + 'E'; + printf("Case #%d: %s\n", Case, buf); + } + return 0; +} \ No newline at end of file diff --git a/self_training/2019-04-06/C.py b/self_training/2019-04-06/C.py new file mode 100644 index 00000000..7468eead --- /dev/null +++ b/self_training/2019-04-06/C.py @@ -0,0 +1,49 @@ +T = int(input()) +for Case in range(1, T + 1) : + N, L = map(int, input().split()) + prd = list(map(int, input().split())) + pr = [] + for i in range(1, L) : + lft, rht = prd[i - 1], prd[i] + def gcd(x, y) : + return gcd(y, x % y) if y else x + com = gcd(lft, rht) + if com < min(lft, rht) : + pr += [com, lft // com, rht // com] + pr = dict(zip(pr, [True] * len(pr))) + while len(pr) < 26 : + upd = False + for item in prd : + for pp in pr : + if item % pp > 0 : + continue + qq = item // pp + if not qq in pr : + prd[qq], upd = True, True + break + if upd : + break + if not upd : + break + assert len(pr) == 26 + pr = dict(zip(sorted(list(pr.keys())), [chr(ord('A') + i) for i in range(26)])) + for i in range(L) : + item = prd[i] + for pp in pr : + if item % pp == 0 : + prd[i] = [pr[pp], pr[item // pp]] + break + for i in range(2) : + chk, ans = True, prd[0][i] + for j in range(L) : + vec = prd[j] + if ans[-1] == vec[0] : + ans += vec[1] + elif ans[-1] == vec[1] : + ans += vec[0] + else : + chk = False + break + if chk : + break + print("Case #%d: %s" % (Case, ans)) diff --git a/self_training/2019-04-06/D.cpp b/self_training/2019-04-06/D.cpp new file mode 100644 index 00000000..d6d41412 --- /dev/null +++ b/self_training/2019-04-06/D.cpp @@ -0,0 +1,43 @@ +#include +using namespace std; +const int maxn = 1 << 10 | 1; +int t, n, m, d, msk[maxn]; +char buf[maxn]; +int main() { + scanf("%d", &t); + for(int Case = 1; Case <= t; ++Case) { + scanf("%d%d%d", &n, &m, &d); + memset(msk, 0, (n - m) * sizeof(int)); + for(int i = 0; i < d; ++i) { + for(int j = 0; j < n; ++j) + buf[j] = "01"[(j >> i) & 1]; + buf[n] = '\0'; + printf("%s\n", buf); + fflush(stdout); + scanf("%s", buf); + if(!strcmp(buf, "-1")) + exit(0); + for(int j = 0; j < n - m; ++j) + msk[j] |= (buf[j] == '1') << i; + } + const int BMSK = (1 << d) - 1; + int cnt = 0; + for(int i = 0; i < n; ++i) { + if(i - cnt < n - m && msk[i - cnt] == (i & BMSK)) + continue; + assert(cnt < m); + if((cnt++) > 0) + printf(" "); + printf("%d", i); + } + assert(cnt == m); + printf("\n"); + fflush(stdout); + scanf("%s", buf); + if(strcmp(buf, "1")) { + fprintf(stderr, "Case #%d: failed [resp=\"%s\"]\n", Case, buf); + exit(0); + } + } + return 0; +} \ No newline at end of file diff --git a/self_training/2019-04-13/A.cpp b/self_training/2019-04-13/A.cpp new file mode 100644 index 00000000..4471de65 --- /dev/null +++ b/self_training/2019-04-13/A.cpp @@ -0,0 +1,101 @@ +#include +using namespace std; +const int maxn = 21, maxs = 401; +int t, n, m, tot, out[maxs][2]; +bool validate() { + if(tot < n * m) { + int low = min(n, m), upp = max(n, m); + if(low == 2) + return upp <= 4; + if(low == 3) + return upp <= 3; + return 0; + } + static bool vis[maxn][maxn]; + memset(vis, 0, sizeof vis); + int px, py, cx = out[0][0], cy = out[0][1]; + vis[cx][cy] = 1; + for(int i = 1; i < tot; ++i) { + px = cx; + py = cy; + cx = out[i][0]; + cy = out[i][1]; + if(vis[cx][cy]) { + printf("(%d, %d) duplicated\n", cx, cy); + return 0; + } + vis[cx][cy] = 1; + if(px == cx || py == cy || px - py == cx - cy || px + py == cx + cy) { + printf("(%d, %d) (%d, %d) colinear\n", px, py, cx, cy); + return 0; + } + } + return 1; +} +int main() { + scanf("%d", &t); + for(int Case = 1; Case <= t; ++Case) { + tot = 0; + scanf("%d%d", &n, &m); + bool rev = 0; + if(n > m) { + swap(n, m); + rev = 1; + } + auto append = [&](int x, int y) { + assert(0 <= x && x < n); + assert(0 <= y && y < m); + out[tot][0] = x; + out[tot][1] = y; + ++tot; + }; + if(n == 2) { + if(m >= 5) { + for(int i = 0; i < m; ++i) { + append(0, (i + 2) % m); + append(1, i); + } + } + } else if(n == 3) { + if(m >= 4) { + for(int i = 0; i < m; ++i) { + append(0, i); + append(1, (i + 2) % m); + append(2, i); + } + } + } else if(n == 4 && m == 4) { + vector idx = vector{3, 0, 1, 2}; + for(int i : idx) { + append(0, i); + append(1, (i + 2) % m); + append(2, i); + append(3, (i + 2) % m); + } + } else { // n >= 4 && m >= 5 + if(n & 1) { + for(int i = 0; i < m; ++i) { + append(0, i); + append(1, (i + 2) % m); + append(2, i); + } + } + for(int i = n & 1 ? 3 : 0; i < n; i += 2) + for(int j = 0; j < m; ++j) { + append(i, (j + 2) % m); + append(i + 1, j); + } + } + if(rev) { + for(int i = 0; i < tot; ++i) + swap(out[i][0], out[i][1]); + swap(n, m); + rev = 0; + } + printf("Case #%d: %s\n", Case, tot == n * m ? "POSSIBLE" : "IMPOSSIBLE"); + for(int i = 0; i < tot; ++i) + printf("%d %d\n", out[i][0] + 1, out[i][1] + 1); + // printf("%s\n", validate() ? "True" : "False"); + } + return 0; +} \ No newline at end of file diff --git a/self_training/2019-04-13/B.cpp b/self_training/2019-04-13/B.cpp new file mode 100644 index 00000000..d51bc480 --- /dev/null +++ b/self_training/2019-04-13/B.cpp @@ -0,0 +1,54 @@ +#include +using namespace std; +typedef long long LL; +const int maxd = 18, maxn = 7, mod[maxn] = {16, 9, 5, 7, 11, 13, 17}; +int t, n, m, coeff[maxn]; +inline int mod_inv(int x, int m) { + int y = m, u = 1, v = 0; + while(x) { + int q = y / x; + swap(x, y); + x -= q * y; + swap(u, v); + u -= q * v; + } + return v < 0 ? v + m : v; +} +int main() { + int MOD = 1; + for(int i = 0; i < maxn; ++i) + MOD *= mod[i]; + for(int i = 0; i < maxn; ++i) { + int tmp = MOD / mod[i]; + coeff[i] = (LL)mod_inv(tmp % mod[i], mod[i]) * tmp % MOD; + } + scanf("%d%d%d", &t, &n, &m); + for(int Case = 1; Case <= t; ++Case) { + int ans = 0; + for(int i = 0; i < maxn; ++i) { + for(int j = 0; j < maxd; ++j) + printf("%d%c", mod[i], " \n"[j == maxd - 1]); + fflush(stdout); + int adt = 0; + for(int j = 0; j < maxd; ++j) { + int ret = -1; + scanf("%d", &ret); + if(ret == -1) { + // fprintf(stderr, "crushed [ret = %d]\n", ret); + exit(0); + } + adt += ret; + } + ans = (ans + (LL)coeff[i] * adt) % MOD; + } + printf("%d\n", ans); + fflush(stdout); + int ret = -1; + scanf("%d", &ret); + if(ret != 1) { + // fprintf(stderr, "crushed [ret = %d]\n", ret); + exit(0); + } + } + return 0; +} \ No newline at end of file diff --git a/self_training/2019-04-13/C.cpp b/self_training/2019-04-13/C.cpp new file mode 100644 index 00000000..afeea4b7 --- /dev/null +++ b/self_training/2019-04-13/C.cpp @@ -0,0 +1,45 @@ +#include +using namespace std; +const int maxl = 51, maxs = (int)5e4 + 1, maxd = 26; +int t, n, tot; +struct Trie { + int cnt, nxt[maxd]; +} e[maxs]; +int dfs(int u) { + int adt = e[u].cnt; + for(int i = 0; i < maxd; ++i) + if(e[u].nxt[i]) + adt += dfs(e[u].nxt[i]); + if(u > 0) { + int tmp = min(adt - (adt & 1), 2); + n += tmp; + adt -= tmp; + } + return adt; +} +int main() { + scanf("%d", &t); + for(int Case = 1; Case <= t; ++Case) { + tot = 0; + memset(e + (tot++), 0, sizeof e[0]); + scanf("%d", &n); + while(n--) { + static char buf[maxl]; + scanf("%s", buf); + int len = strlen(buf), rt = 0; + for(int i = len - 1; i >= 0; --i) { + int o = buf[i] - 'A'; + if(!e[rt].nxt[o]) { + e[rt].nxt[o] = tot; + memset(e + (tot++), 0, sizeof e[0]); + } + rt = e[rt].nxt[o]; + } + ++e[rt].cnt; + } + n = 0; + dfs(0); + printf("Case #%d: %d\n", Case, n); + } + return 0; +} \ No newline at end of file diff --git a/self_training/2019-05-18/A.cpp b/self_training/2019-05-18/A.cpp new file mode 100644 index 00000000..329f4f81 --- /dev/null +++ b/self_training/2019-05-18/A.cpp @@ -0,0 +1,48 @@ +#include +using namespace std; +typedef long long LL; +const int maxn = 301, maxm = 90001; +int t, n, m; +struct Point { + int x, y; + bool operator == (Point const &t) const { + return x == t.x && y == t.y; + } + LL det(Point const &t) const { + return (LL)x * t.y - (LL)y * t.x; + } + LL dot(Point const &t) const { + return (LL)x * t.x + (LL)y * t.y; + } + bool operator < (Point const &t) const { + return det(t) > 0; + } +} p[maxn], vec[maxm]; +int main() { + scanf("%d", &t); + for(int Case = 1; Case <= t; ++Case) { + scanf("%d", &n); + m = 0; + vec[m++] = (Point){1, 0}; + vec[m++] = (Point){0, 1}; + for(int i = 0; i < n; ++i) { + scanf("%d%d", &p[i].x, &p[i].y); + for(int j = 0; j < i; ++j) { + int dx = p[i].x - p[j].x; + int dy = p[j].y - p[i].y; + if(dx < 0 || (!dx && dy < 0)) { + dx = -dx; + dy = -dy; + } + if(dy < 0) + continue; + int com = __gcd(dx, dy); + vec[m++] = (Point){dx / com, dy / com}; + } + } + sort(vec, vec + m); + m = unique(vec, vec + m) - vec; + printf("Case #%d: %d\n", Case, m - 1); + } + return 0; +} \ No newline at end of file diff --git a/self_training/2019-05-18/C.cpp b/self_training/2019-05-18/C.cpp new file mode 100644 index 00000000..bf76dc65 --- /dev/null +++ b/self_training/2019-05-18/C.cpp @@ -0,0 +1,83 @@ +#include +using namespace std; +typedef long long LL; +const int maxn = 11; +int t, n; +struct Point { + LL x, y; + LL det(Point const &t) const { + return (LL)x * t.y - (LL)y * t.x; + } + bool operator < (Point const &t) const { + return det(t) < 0; + } +} p[maxn], low, upp; +LL solve(Point L, Point R) { + LL dt = L.x / L.y; + L.x -= dt * L.y; + R.x -= dt * R.y; + if(!L.x || R.x > R.y) + return R.y / R.x + 1; + return solve((Point){R.y, R.x}, (Point){L.y, L.x}) * R.y / R.x + 1; +} +void solve() { + low = (Point){0, 1}; + upp = (Point){1, 0}; + scanf("%d", &n); + bool chk = 1; + for(int i = 0; i < n; ++i) { + scanf("%lld%lld", &p[i].x, &p[i].y); + if(!chk) + continue; + for(int j = 0; j < i; ++j) { + if(!chk) + continue; + LL dx = p[i].x - p[j].x; + LL dy = p[i].y - p[j].y; + LL com = __gcd(abs(dx), abs(dy)); + if(dy < 0) { + if(dx <= 0) { + chk = 0; + continue; + } + upp = min(upp, (Point){dx / com, -dy / com}); + } else if(!dy) { + if(dx <= 0) { + chk = 0; + continue; + } + } else { + if(dx < 0) + low = max(low, (Point){-dx / com, dy / com}); + } + } + } + if(!chk || !(low < upp)) { + puts("IMPOSSIBLE"); + return; + } + assert(low.y > 0); + // for(LL x = 1; ; ++x) { + // LL L = x * low.x / low.y + 1; + // LL R = upp.y ? (x * upp.x - 1) / upp.y : L; + // if(L <= R) { + // printf("%lld %lld\n", x, L); + // return; + // } + // } + if(!upp.y) { + printf("%lld %lld\n", 1LL, low.x / low.y + 1); + return; + } + LL x = max(solve(low, upp), 1LL); + LL y = max(x * low.x / low.y + 1, 1LL); + printf("%lld %lld\n", x, y); +} +int main() { + scanf("%d", &t); + for(int Case = 1; Case <= t; ++Case) { + printf("Case #%d: ", Case); + solve(); + } + return 0; +} \ No newline at end of file diff --git a/self_training/2019-05-18/D.cpp b/self_training/2019-05-18/D.cpp new file mode 100644 index 00000000..19e7d76c --- /dev/null +++ b/self_training/2019-05-18/D.cpp @@ -0,0 +1,64 @@ +#include +using namespace std; +typedef long long LL; +const int maxn = (int)1e5 + 1, mod = (int)1e9 + 7; +int t, n, m, a[maxn], b[maxn], c[maxn], f[maxn], que[maxn]; +vector e[maxn]; +bool can[maxn]; +int main() { + scanf("%d", &t); + for(int Case = 1; Case <= t; ++Case) { + scanf("%d", &n); + for(int i = 1; i <= n; ++i) { + scanf("%d%d", a + i, b + i); + e[a[i]].push_back(i); + e[b[i]].push_back(i); + } + m = 0; + memset(can + 1, 0, n * sizeof(bool)); + que[m++] = 1; + can[1] = 1; + for(int i = 0; i < m; ++i) { + int u = que[i]; + for(int v : e[u]) + if(!can[v]) { + can[v] = 1; + que[m++] = v; + } + } + m = 0; + for(int i = 1; i <= n; ++i) { + scanf("%d", c + i); + if(c[i]) + que[m++] = i; + } + can[1] = 0; + + + m = min(n + 1, 1000); + memset(f[cur] + 1, 0, n * sizeof(int)); + f[cur][1] = 1; + for(int i = 1; i <= m; ++i) { + swap(cur, pre); + for(int j = 1; j <= n; ++j) + if(vis[a[j]] || vis[b[j]]) + (f[cur][j] = f[pre][a[j]] + f[pre][b[j]]) >= mod && (f[cur][j] -= mod); + else + f[cur][j] = f[pre][j]; + } + bool chk = 1; + for(int i = 1; i <= n; ++i) { + chk &= !c[i] || f[cur][i] == f[pre][i]; + vector().swap(e[i]); + } + if(!chk) { + printf("Case #%d: UNBOUNDED\n", Case); + } else { + int sum = 0; + for(int i = 1; i <= n; ++i) + sum = (sum + (LL)f[cur][i] * c[i]) % mod; + printf("Case #%d: %d\n", Case, sum); + } + } + return 0; +} diff --git a/self_training/2019-07-26/A.cpp b/self_training/2019-07-26/A.cpp new file mode 100644 index 00000000..149c76d4 --- /dev/null +++ b/self_training/2019-07-26/A.cpp @@ -0,0 +1,81 @@ +#include +using namespace std; +typedef long long LL; +const int maxn = (int)1e6 + 1, maxm = 169; +int n, tot, pr[maxn], d[maxn], dd[maxn], idx[maxn]; +bitset f[maxn], g[maxm], cur, res; +vector pos; +bool vis[maxn]; +int main() { + scanf("%d", &n); + d[1] = dd[1] = 1; + for(int i = 2; i <= n; ++i) { + if(!d[i]) { + idx[i] = tot; + pr[tot++] = d[i] = i; + dd[i] = (LL)i * i <= n ? 1 : i; + } + for(int j = 0, k; (k = i * pr[j]) <= n; ++j) { + d[k] = pr[j]; + dd[k] = dd[i]; + if(d[i] == pr[j]) + break; + } + } + int sp = dd[n]; + res.reset(); + for(int tmp = n / sp; tmp > 1; ) { + int pp = d[tmp], cc = 0; + for( ; d[tmp] == pp; tmp /= pp, ++cc); + if(cc & 1) + res.set(idx[pp]); + } + if(sp == 1 && !res.any()) { + printf("%d\n", n); + return 0; + } + if(d[n] == n) { + puts("-1"); + return 0; + } + for( ; --n; ) { + int nn = dd[n]; + cur.reset(); + for(int tmp = n / nn; tmp > 1; ) { + int pp = d[tmp], cc = 0; + for( ; d[tmp] == pp; tmp /= pp, ++cc); + if(cc & 1) + cur.set(idx[pp]); + } + if(nn > 1 && vis[nn]) + cur ^= f[nn]; + for(int i : pos) + if(cur.test(i)) + cur ^= g[i]; + if(nn > 1 && !vis[nn]) { + vis[nn] = 1; + f[nn] = cur; + if(nn == sp) { + res ^= cur; + for(int i : pos) + if(res.test(i)) + res ^= g[i]; + } + } else { + int i = cur._Find_first(); + if(i < (int)cur.size()) { + pos.push_back(i); + g[i] = cur; + if(sp == 1 || vis[sp]) { + if(res.test(i)) + res ^= cur; + } + } + } + if((sp == 1 || vis[sp]) && !res.any()) + break; + } + n -= !n; + printf("%d\n", n); + return 0; +} \ No newline at end of file diff --git a/self_training/2019-07-26/B.cpp b/self_training/2019-07-26/B.cpp new file mode 100644 index 00000000..39110652 --- /dev/null +++ b/self_training/2019-07-26/B.cpp @@ -0,0 +1,20 @@ +#include +using namespace std; +const int maxn = (int)2e5 + 1; +int n, mx, ans; +bool vis[maxn]; +int main() { + scanf("%d", &n); + while(n--) { + scanf("%d", &mx); + vis[--mx] = 1; + } + for(int i = 1; i <= mx; ++i) + if(vis[i]) { + ++ans; + for(int j = i; j <= mx; j += i) + vis[j] = 0; + } + printf("%d\n", max(ans, 1)); + return 0; +} \ No newline at end of file diff --git a/self_training/2019-07-26/C.cpp b/self_training/2019-07-26/C.cpp new file mode 100644 index 00000000..66dc8948 --- /dev/null +++ b/self_training/2019-07-26/C.cpp @@ -0,0 +1,53 @@ +#include +using namespace std; +const int maxn = (int)1e6 + 1, maxd = 20, maxv = (int)1e7 + 1; +int tot, pr[maxv >> 3 | 1], d[maxv]; +int n, a[maxn], b[maxn], Log[maxn], st[maxd][maxn]; +struct Record { + int las, val, pre; +} his[maxv]; +int main() { + for(int i = 2; i < maxv; ++i) { + if(!d[i]) + pr[tot++] = d[i] = i; + for(int j = 0, k; (k = i * pr[j]) < maxv; ++j) { + d[k] = pr[j]; + if(d[i] == pr[j]) + break; + } + } + scanf("%d", &n); + Log[0] = -1; + int *f = st[0]; + for(int i = 1; i <= n; ++i) { + Log[i] = Log[i >> 1] + 1; + scanf("%d%d", a + i, b + i); + for(int tmp = b[i]; tmp > 1; ) { + int pp = d[tmp], cc = 0; + for( ; d[tmp] == pp; tmp /= pp, ++cc); + for(int j = 0, pw = 1; j < cc; ++j) { + pw *= pp; + Record &cur = his[pw]; + int tp = a[i] % pw; + if(!cur.pre || cur.val != tp) { + cur.las = cur.pre; + cur.val = tp; + } + f[i] = max(f[i], cur.las + 1); + cur.pre = i; + } + } + } + for(int i = 1; i <= Log[n]; ++i) + for(int j = 1; j + (1 << i) - 1 <= n; ++j) + st[i][j] = max(st[i - 1][j], st[i - 1][j + (1 << (i - 1))]); + scanf("%d", &n); + while(n--) { + int L, R; + scanf("%d%d", &L, &R); + int d = Log[R - L + 1]; + int val = max(st[d][L], st[d][R - (1 << d) + 1]); + puts(val <= L ? "Yes" : "No"); + } + return 0; +} diff --git a/self_training/2019-07-26/D.cpp b/self_training/2019-07-26/D.cpp new file mode 100644 index 00000000..c1f67be4 --- /dev/null +++ b/self_training/2019-07-26/D.cpp @@ -0,0 +1,21 @@ +#include +using namespace std; +typedef long long LL; +int main() { + int m; + LL n; + scanf("%lld%d", &n, &m); + while(m--) { + LL x1, y1, x2, y2, ans = 0; + scanf("%lld%lld%lld%lld", &x1, &y1, &x2, &y2); + ans += max(min(x2, y2) - max(x1, y1) + 1, 0LL); + tie(x1, x2) = make_pair(n - 1 - x2, n - 1 - x1); + ans += max(min(x2, y2) - max(x1, y1) + 1, 0LL); + if(n & 1) { + LL half = n >> 1; + ans -= x1 <= half && half <= x2 && y1 <= half && half <= y2; + } + printf("%lld\n", ans); + } + return 0; +} \ No newline at end of file diff --git a/self_training/2019-07-26/E.cpp b/self_training/2019-07-26/E.cpp new file mode 100644 index 00000000..a9b09bd2 --- /dev/null +++ b/self_training/2019-07-26/E.cpp @@ -0,0 +1,73 @@ +#include +using namespace std; +typedef long long LL; +const int maxn = (int)2e5 + 1, mod = 998244353, inv2 = (mod + 1) / 2; +int n, cnt[maxn], typ[maxn], ord[maxn], f[2][2][maxn]; +vector e[maxn]; +bool vis[maxn]; +int main() { + scanf("%d", &n); + for(int i = 1; i <= n; ++i) { + int par; + scanf("%d%d%d", &par, cnt + i, typ + i); + if(par) { + e[par].push_back(i); + } else { + assert(!ord[1]); + ord[1] = i; + vis[i] = 1; + } + } + for(int i = 1, tot = 1; i <= n; ++i) { + int u = ord[i]; + for(int v : e[u]) + if(!vis[v]) + ord[++tot] = v; + } + for(int i = n; i >= 1; --i) { + int u = ord[i], g[2][2][2], cur = 0, pre = 1; + bool fir = 1; + if((int)e[u].size() < cnt[u]) { + int tp = 1; + for(int prd = inv2, rem = cnt[u] - (int)e[u].size(); rem > 0; rem >>= 1, prd = (LL)prd * prd % mod) + (rem & 1) && (tp = (LL)tp * prd % mod); + assert(tp); + g[cur][1][1] = tp; + g[cur][0][1] = g[cur][1][0] = 0; + g[cur][0][0] = 1 - tp; + g[cur][0][0] < 0 && (g[cur][0][0] += mod); + fir = 0; + } + for(int v : e[u]) + if(fir) { + g[cur][0][0] = f[0][0][v]; + g[cur][0][1] = f[0][1][v]; + g[cur][1][0] = f[1][0][v]; + g[cur][1][1] = f[1][1][v]; + fir = 0; + } else { + swap(pre, cur); + g[cur][1][1] = (LL)g[pre][1][1] * f[1][1][v] % mod; + g[cur][1][0] = ((LL)(g[pre][1][0] + g[pre][1][1]) * (f[1][0][v] + f[1][1][v]) - g[cur][1][1]) % mod; + g[cur][1][0] < 0 && (g[cur][1][0] += mod); + g[cur][0][1] = ((LL)(g[pre][0][1] + g[pre][1][1]) * (f[0][1][v] + f[1][1][v]) - g[cur][1][1]) % mod; + g[cur][0][1] < 0 && (g[cur][0][1] += mod); + g[cur][0][0] = (1LL - g[cur][0][1] - g[cur][1][0] - g[cur][1][1]) % mod; + g[cur][0][0] < 0 && (g[cur][0][0] += mod); + } + swap(g[cur][0][0], g[cur][1][1]); + swap(g[cur][0][1], g[cur][1][0]); + f[0][0][u] = g[cur][0][0]; + f[0][1][u] = g[cur][0][1]; + f[1][0][u] = g[cur][1][0]; + f[1][1][u] = g[cur][1][1]; + if(typ[u] != -1) { + (f[0][typ[u]][u] += f[0][!typ[u]][u]) >= mod && (f[0][typ[u]][u] -= mod); + f[0][!typ[u]][u] = 0; + (f[1][typ[u]][u] += f[1][!typ[u]][u]) >= mod && (f[1][typ[u]][u] -= mod); + f[1][!typ[u]][u] = 0; + } + } + printf("%d\n", (f[0][0][ord[1]] + f[1][1][ord[1]]) % mod); + return 0; +} \ No newline at end of file diff --git a/self_training/2019-07-28/A.cpp b/self_training/2019-07-28/A.cpp new file mode 100644 index 00000000..bbaae0bb --- /dev/null +++ b/self_training/2019-07-28/A.cpp @@ -0,0 +1,33 @@ +#include +using namespace std; +const int maxn = (int)1e5 + 1, maxd = 1 << 10 | 1; +int t, n, m, a[maxn]; +bool odd[maxd]; +int main() { + for(int i = 1; i < maxd; ++i) + odd[i] = odd[i >> 1] ^ (i & 1); + scanf("%d", &t); + for(int Case = 1; Case <= t; ++Case) { + scanf("%d%d", &n, &m); + set pos; + for(int i = 1; i <= n; ++i) { + scanf("%d", a + i); + if(odd[a[i]]) + pos.insert(i); + } + printf("Case #%d:", Case); + for(int i = 1; i <= m; ++i) { + int x, y; + scanf("%d%d", &x, &y); + a[++x] = y; + if(odd[y]) { + pos.insert(x); + } else { + pos.erase(x); + } + printf(" %d", n - (pos.size() & 1 ? min(*pos.begin(), n - *pos.rbegin() + 1) : 0)); + } + printf("\n"); + } + return 0; +} diff --git a/self_training/2019-07-28/B.cpp b/self_training/2019-07-28/B.cpp new file mode 100644 index 00000000..be5abd33 --- /dev/null +++ b/self_training/2019-07-28/B.cpp @@ -0,0 +1,74 @@ +#include +using namespace std; +const int maxn = (int)1e5 + 9; +int t, n, m, dt, typ[maxn], fir[maxn], las[maxn], ord[2][maxn], tim[maxn], ans[maxn]; +inline bool cmp(int const &u, int const &v) { + return las[u] < las[v]; +} +int main() { + scanf("%d", &t); + for(int Case = 1; Case <= t; ++Case) { + scanf("%d%d%d", &n, &m, &dt); + ord[0][0] = ord[1][0] = 0; + for(int i = 1; i <= m; ++i) { + char op; + scanf("%d %c", fir + i, &op); + typ[i] = op == 'C'; + las[i] = (fir[i] + (typ[i] ? dt : -dt) - 1) % n + 1; + if(las[i] <= 0) + las[i] += n; + ord[typ[i]][++ord[typ[i]][0]] = i; + } + sort(ord[0] + 1, ord[0] + ord[0][0] + 1, cmp); + sort(ord[1] + 1, ord[1] + ord[1][0] + 1, cmp); + memset(tim + 1, 0, n * sizeof(int)); + ++dt; + for(int i = 1, j; i <= ord[0][0]; i = j) { + for(j = i + 1; j <= ord[0][0] && fir[ord[0][i]] == fir[ord[0][j]]; ++j); + int sta = las[ord[0][i]], len = (j <= ord[0][0] ? las[ord[0][j]] : las[ord[0][1]]) - sta; + if(len <= 0) + len += n; + len = min(len, dt); + for(int k = 0, pos = sta; k < len; ++k, (++pos) > n && (pos = 1)) + tim[pos] = max(tim[pos], dt - k); + } + for(int i = 1, j; i <= ord[1][0]; i = j) { + for(j = i + 1; j <= ord[1][0] && fir[ord[1][i]] == fir[ord[1][j]]; ++j); + int sta = las[ord[1][i]], len = sta - (i > 1 ? las[ord[1][i - 1]] : las[ord[1][ord[1][0]]]); + if(len <= 0) + len += n; + len = min(len, dt); + for(int k = 0, pos = sta; k < len; ++k, (--pos) < 1 && (pos = n)) + tim[pos] = max(tim[pos], dt - k); + } + for(int i = 1, j; i <= ord[0][0]; i = j) { + for(j = i + 1; j <= ord[0][0] && fir[ord[0][i]] == fir[ord[0][j]]; ++j); + int sta = las[ord[0][i]], len = (j <= ord[0][0] ? las[ord[0][j]] : las[ord[0][1]]) - sta; + if(len <= 0) + len += n; + len = min(len, dt); + int cnt = 0; + for(int k = 0, pos = sta; k < len; ++k, (++pos) > n && (pos = 1)) + cnt += tim[pos] == dt - k; + for(int k = i; k < j; ++k) + ans[ord[0][k]] = cnt; + } + for(int i = 1, j; i <= ord[1][0]; i = j) { + for(j = i + 1; j <= ord[1][0] && fir[ord[1][i]] == fir[ord[1][j]]; ++j); + int sta = las[ord[1][i]], len = sta - (i > 1 ? las[ord[1][i - 1]] : las[ord[1][ord[1][0]]]); + if(len <= 0) + len += n; + len = min(len, dt); + int cnt = 0; + for(int k = 0, pos = sta; k < len; ++k, (--pos) < 1 && (pos = n)) + cnt += tim[pos] == dt - k; + for(int k = i; k < j; ++k) + ans[ord[1][k]] = cnt; + } + printf("Case #%d:", Case); + for(int i = 1; i <= m; ++i) + printf(" %d", ans[i]); + printf("\n"); + } + return 0; +} diff --git a/self_training/2019-07-28/C.cpp b/self_training/2019-07-28/C.cpp new file mode 100644 index 00000000..a70be145 --- /dev/null +++ b/self_training/2019-07-28/C.cpp @@ -0,0 +1,86 @@ +#include +using namespace std; +typedef long long LL; +const int maxn = (int)1e5 + 9; +const LL INF = 0x3f3f3f3f3f3f3f3fLL; +struct Fenwick { + int len, mx, que[maxn], cnt[maxn]; + LL sum[maxn]; + void init(int n, int arr[]) { + len = n; + memcpy(que, arr, len * sizeof(int)); + sort(que, que + len); + len = unique(que, que + len) - que; + memset(cnt, 0, (len + 1) * sizeof(int)); + memset(sum + 1, 0, len * sizeof(LL)); + for(mx = 1; mx <= len; mx <<= 1); + mx >>= 1; + } + void update(int x, int v) { + LL vx = v * x; + x = lower_bound(que, que + len, x) - que + 1; + cnt[0] += v; + for( ; x <= len; x += x & -x) { + cnt[x] += v; + sum[x] += vx; + } + } + LL lowest(int k) { + if(k > cnt[0]) + return INF; + int x = 0; + LL ret = 0; + for(int y = mx, z; y; y >>= 1) + if((z = x | y) <= len && cnt[z] < k) { + x = z; + k -= cnt[z]; + ret += sum[z]; + } + return ret + que[x] * k; + } +} lft, rht; +int t, n, m; +pair seq[maxn]; +int main() { + scanf("%d", &t); + for(int Case = 1; Case <= t; ++Case) { + scanf("%d%d", &m, &n); + for(int i = 0; i < n; ++i) + scanf("%d", &seq[i].first); + for(int i = 0; i < n; ++i) + scanf("%d", &seq[i].second); + sort(seq, seq + n); + static int tmp[maxn]; + for(int i = 0; i < n; ++i) + tmp[i] = seq[i].second - seq[i].first; + lft.init(n, tmp); + for(int i = 0; i < n; ++i) + tmp[i] = seq[i].second + seq[i].first; + rht.init(n, tmp); + for(int i = 0; i < n; ++i) + rht.update(tmp[i], 1); + LL ans = INF; + for(int i = 0; i < n; ++i) { + rht.update(seq[i].second + seq[i].first, -1); + int L = max(m - rht.cnt[0], 0), R = min(lft.cnt[0], m); + while(R - L + 1 >= 6) { + int step = (R - L + 1) / 3; + int ML = L + step, MR = R - step; + LL fL = (LL)(ML + ML - m) * seq[i].first + lft.lowest(ML) + rht.lowest(m - ML); + LL fR = (LL)(MR + MR - m) * seq[i].first + lft.lowest(MR) + rht.lowest(m - MR); + if(fL <= fR) { + R = MR; + } else { + L = ML; + } + } + for( ; L <= R; ++L) { + LL tmp = (LL)(L + L - m) * seq[i].first + lft.lowest(L) + rht.lowest(m - L); + ans = min(ans, tmp + seq[i].second); + } + lft.update(seq[i].second - seq[i].first, 1); + } + printf("Case #%d: %lld\n", Case, ans); + } + return 0; +} diff --git a/self_training/2019-08-17-2/1001.cpp b/self_training/2019-08-17-2/1001.cpp new file mode 100644 index 00000000..98e8a20d --- /dev/null +++ b/self_training/2019-08-17-2/1001.cpp @@ -0,0 +1,31 @@ +#include +using namespace std; +const int maxn = (int)1e3 + 1; +int T, n, m, a[maxn], b[maxn]; +int main() { + scanf("%d", &T); + while(T--) { + scanf("%d", &n); + m = n; + for(int i = 0; i < n; ++i) + scanf("%d", a + i); + for(int i = 0; i < n; ++i) + scanf("%d", b + i); + for( ; n && !a[n - 1]; --n); + for( ; m && !b[m - 1]; --m); + int fz, fm; + if(n > m) { + fz = 1; + fm = 0; + } else if(n < m) { + fz = 0; + fm = 1; + } else { + fz = a[n - 1]; + fm = b[m - 1]; + } + int r = __gcd(fz, fm); + printf("%d/%d\n", fz / r, fm / r); + } + return 0; +} \ No newline at end of file diff --git a/self_training/2019-08-17-2/1002.cpp b/self_training/2019-08-17-2/1002.cpp new file mode 100644 index 00000000..3ddc698d --- /dev/null +++ b/self_training/2019-08-17-2/1002.cpp @@ -0,0 +1,48 @@ +#include +using namespace std; +const int maxn = (int)1e3 + 1, INF = 0x3f3f3f3f; +int t, n, pL[maxn], pR[maxn], sz[maxn], seq[maxn][4], f[maxn][4]; +int main() { + scanf("%d", &t); + while(t--) { + scanf("%d", &n); + for(int i = 1; i <= n; ++i) + scanf("%d%d", pL + i, pR + i); + for(int i = 1, qL = -INF, qR = INF; i <= n; ++i) { + qL = max(qL, pL[i]); + qR = min(qR, pR[i]); + sz[i] = 0; + for(int j = 0; j < 2 && pL[i] + j <= pR[i]; ++j) { + seq[i][sz[i]++] = pL[i] + j; + seq[i][sz[i]++] = pR[i] - j; + } + sort(seq[i], seq[i] + sz[i]); + sz[i] = unique(seq[i], seq[i] + sz[i]) - seq[i]; + for(int j = 0; j < sz[i]; ++j) + f[i][j] = qL <= seq[i][j] && seq[i][j] <= qR ? 0 : INF; + for(int j = i - 1, rL = -INF, rR = INF; j >= 1; --j) { + rL = max(rL, pL[j]); + rR = min(rR, pR[j]); + if(rL > rR) + break; + for(int k = 0; k < sz[j]; ++k) + if(rL <= seq[j][k] && seq[j][k] <= rR) + for(int o = 0; o < sz[i]; ++o) { + int dis = abs(seq[i][o] - seq[j][k]); + dis = (dis + 1) >> 1; + f[i][o] = min(f[i][o], f[j][k] + dis); + } + } + } + int ans = INF; + for(int i = n, qL = -INF, qR = INF; i >= 1; --i) { + qL = max(qL, pL[i]); + qR = min(qR, pR[i]); + for(int j = 0; j < sz[i]; ++j) + if(qL <= seq[i][j] && seq[i][j] <= qR) + ans = min(ans, f[i][j]); + } + printf("%d\n", ans); + } + return 0; +} \ No newline at end of file diff --git a/self_training/2019-08-17-2/1003.cpp b/self_training/2019-08-17-2/1003.cpp new file mode 100644 index 00000000..3ea62fcd --- /dev/null +++ b/self_training/2019-08-17-2/1003.cpp @@ -0,0 +1,86 @@ +#include +using namespace std; +typedef double DB; +typedef pair Edge; +const DB INF = 1e20; +const int maxn = 201, maxs = 409, maxm = (int)1.7e5 + 1; +int t, n, xtot, xque[maxs], ytot, yque[maxs], c[maxs][maxs]; +struct Square { + int x1, y1, x2, y2; + void init() { + scanf("%d%d%d%d", &x1, &y1, &x2, &y2); + xque[xtot++] = x1; + yque[ytot++] = y1; + xque[xtot++] = x2; + yque[ytot++] = y2; + } +} a[maxn]; +vector e[maxm]; +double dis[maxm]; +bool vis[maxm]; +int main() { + scanf("%d", &t); + while(t--) { + xtot = ytot = 0; + scanf("%d", &n); + for(int i = 0; i <= n; ++i) + a[i].init(); + sort(xque, xque + xtot); + xtot = unique(xque, xque + xtot) - xque; + sort(yque, yque + ytot); + ytot = unique(yque, yque + ytot) - yque; + for(int i = 0; i < xtot; ++i) + memset(c[i], 0, ytot * sizeof(int)); + for(int i = 0; i < n; ++i) { + int x1 = lower_bound(xque, xque + xtot, a[i].x1) - xque; + int y1 = lower_bound(yque, yque + ytot, a[i].y1) - yque; + int x2 = lower_bound(xque, xque + xtot, a[i].x2) - xque; + int y2 = lower_bound(yque, yque + ytot, a[i].y2) - yque; + for(int x = x1; x < x2; ++x) + for(int y = y1; y < y2; ++y) + ++c[x][y]; + } + priority_queue Q; + int res = -1; + for(int i = 0, idx = 0; i < xtot; ++i) + for(int j = 0; j < ytot; ++j, ++idx) { + vector().swap(e[idx]); + if(i) { + DB tmp = (xque[i] - xque[i - 1]) / (max(c[i - 1][j], j ? c[i - 1][j - 1] : 0) + 1.0); + e[idx].push_back((Edge){tmp, idx - ytot}); + e[idx - ytot].push_back((Edge){tmp, idx}); + } + if(j) { + DB tmp = (yque[j] - yque[j - 1]) / (max(c[i][j - 1], i ? c[i - 1][j - 1] : 0) + 1.0); + e[idx].push_back((Edge){tmp, idx - 1}); + e[idx - 1].push_back((Edge){tmp, idx}); + } + if(xque[i] == a[n].x1 && yque[j] == a[n].y1) { + dis[idx] = 0; + Q.push((Edge){-dis[idx], idx}); + } else { + dis[idx] = INF; + } + if(xque[i] == a[n].x2 && yque[j] == a[n].y2) + res = idx; + vis[idx] = 0; + } + while(!Q.empty()) { + int u = Q.top().second; + Q.pop(); + if(vis[u]) + continue; + vis[u] = 1; + for(Edge &it : e[u]) { + int v = it.second; + DB tmp = dis[u] + it.first; + if(dis[v] > tmp) { + dis[v] = tmp; + Q.push((Edge){-dis[v], v}); + } + } + } + printf("%.5f\n", (double)dis[res]); + } + return 0; +} \ No newline at end of file diff --git a/self_training/2019-08-17-2/1005.cpp b/self_training/2019-08-17-2/1005.cpp new file mode 100644 index 00000000..23426bc8 --- /dev/null +++ b/self_training/2019-08-17-2/1005.cpp @@ -0,0 +1,104 @@ +#include +using namespace std; +namespace fastIO{ + #define BUF_SIZE 100000 + #define OUT_SIZE 100000 + #define ll long long + //fread->read + bool IOerror=0; + inline char nc(){ + static char buf[BUF_SIZE],*p1=buf+BUF_SIZE,*pend=buf+BUF_SIZE; + if (p1==pend){ + p1=buf; pend=buf+fread(buf,1,BUF_SIZE,stdin); + if (pend==p1){IOerror=1;return -1;} + } + return *p1++; + } + inline bool blank(char ch){return ch==' '||ch=='\n'||ch=='\r'||ch=='\t';} + template + inline void read(T &x){ + bool sign=0; char ch=nc(); x=0; + for (;blank(ch);ch=nc()); + if (IOerror)return; + if (ch=='-')sign=1,ch=nc(); + for (;ch>='0'&&ch<='9';ch=nc())x=x*10+ch-'0'; + if (sign)x=-x; + } + //fwrite->write + struct Ostream_fwrite{ + char *buf,*p1,*pend; + Ostream_fwrite(){buf=new char[BUF_SIZE];p1=buf;pend=buf+BUF_SIZE;} + void out(char ch){ + if (p1==pend){ + fwrite(buf,1,BUF_SIZE,stdout);p1=buf; + } + *p1++=ch; + } + void print(ll x){ + static char s[25],*s1;s1=s; + if (!x)*s1++='0';if (x<0)out('-'),x=-x; + while(x)*s1++=x%10+'0',x/=10; + while(s1--!=s)out(*s1); + } + void print(char *s){while (*s)out(*s++);} + void flush(){if (p1!=buf){fwrite(buf,1,p1-buf,stdout);p1=buf;}} + ~Ostream_fwrite(){flush();} + }Ostream; + inline void print(ll x){Ostream.print(x);} + inline void print(char *s){Ostream.print(s);} + inline void flush(){Ostream.flush();} + #undef ll + #undef OUT_SIZE + #undef BUF_SIZE +}; +using namespace fastIO; +typedef long long LL; +const int maxn = 11, coeff[maxn] = {1, 1, 0, -1, -1, 0, -1, -1, 0, 1, 1}, row[maxn] = {0, 1, 1, 0, 3, 0, 3, 5, 4, 1, 9}; +int t; +LL m, f[2][maxn], deg[2], vec[maxn << 1 | 1]; +inline void poly_mod() { + for(int i = deg[0] - deg[1]; i >= 0; --i) { + if(!vec[i + deg[1]]) + continue; + LL d = vec[i + deg[1]]; + for(int j = 0; j <= deg[1]; ++j) + vec[i + j] -= d * f[1][j]; + } + for( ; deg[0] && !vec[deg[0]]; --deg[0]); + for(int i = 0; i <= deg[0]; ++i) + f[0][i] = vec[i]; +} +int main() { + read(t); + while(t--) { + read(m); + deg[0] = 0; + f[0][0] = 1; + deg[1] = maxn - 1; + for(int i = 0; i <= deg[1]; ++i) + f[1][i] = coeff[i]; + int mx = 0; + for( ; (1LL << mx) <= m; ++mx); + for(int i = mx - 1; i >= 0; --i) { + memset(vec, 0, (deg[0] << 1 | 1) * sizeof(LL)); + for(int j = 0; j <= deg[0]; ++j) + for(int k = 0; k <= deg[0]; ++k) + vec[j + k] += f[0][j] * f[0][k]; + deg[0] <<= 1; + poly_mod(); + if((m >> i) & 1) { + vec[0] = 0; + for(int j = 0; j <= deg[0]; ++j) + vec[j + 1] = f[0][j]; + ++deg[0]; + poly_mod(); + } + } + LL ans = 0; + for(int i = 0; i <= deg[0]; ++i) + ans += f[0][i] * row[i]; + print(ans); + print("\n"); + } + return 0; +} diff --git a/self_training/2019-08-17/A.cpp b/self_training/2019-08-17/A.cpp new file mode 100644 index 00000000..fe610dee --- /dev/null +++ b/self_training/2019-08-17/A.cpp @@ -0,0 +1,49 @@ +#include +using namespace std; +typedef long double DB; +const int maxn = 501; +DB f[2][maxn][maxn], logF[maxn]; +int n, vL, vR, a[maxn], cur, pre = 1; +int main() { + scanf("%d%d%d", &n, &vL, &vR); + for(int i = 0; i < n; ++i) + scanf("%d", a + i); + sort(a, a + n); + f[cur][0][0] = 1; + for(int i = 0, upp = 0; i < n; ++i) { + swap(cur, pre); + int nxt = min(upp + a[i], vL); + for(int j = 0; j <= i + 1; ++j) + for(int k = 0; k <= nxt; ++k) + f[cur][j][k] = 0; + for(int j = 0; j <= i; ++j) + for(int k = 0; k <= upp; ++k) { + f[cur][j][k] += f[pre][j][k]; + if(k + a[i] <= nxt) + f[cur][j + 1][k + a[i]] += f[pre][j][k]; + } + upp = nxt; + } + DB ans = 0; + for(int i = 1; i <= n; ++i) + logF[i] = logF[i - 1] + log((DB)i); + for(int i = 0; i < n; ++i) { + for(int j = 1; j <= vL; ++j) + f[cur][i][j] += f[cur][i][j - 1]; + DB ways = 0; + for(int j = 0, k; j < n; ) { + for(k = j; j < n && a[j] == a[k]; ++j); + int L = vR - a[k], R = vL; + if(L >= R) + continue; + DB cnt = 0; + for(int pos = i, sgn = 1; pos >= 0 && R >= 0; --pos, sgn = -sgn, L -= a[k], R -= a[k]) + cnt += sgn * (f[cur][pos][R] - (L >= 0 ? f[cur][pos][L] : 0)); + ways += cnt * (j - k); + } + assert(ways >= 0); + ans += ways * exp(logF[i] + logF[n - 1 - i] - logF[n]); + } + printf("%.20e\n", (double)(1 - ans)); + return 0; +} diff --git a/self_training/2019-08-17/C.cpp b/self_training/2019-08-17/C.cpp new file mode 100644 index 00000000..a7b6e88e --- /dev/null +++ b/self_training/2019-08-17/C.cpp @@ -0,0 +1,62 @@ +#include +using namespace std; +typedef long long LL; +const int maxn = (int)1e6 + 1, maxc = 7, m1 = (int)1e9 + 7, m2 = (int)1e9 + 9; +const char *pat[maxc] = {"do", "re", "mi", "fa", "sol", "la", "si"}; +int n, pL, pR, sta[maxn], ans[maxn + 2]; +LL hh[maxn << 1 | 1], pw[maxn]; +inline LL get_hash(int sta, int len) { + int r11 = hh[sta - 1] >> 32, r12 = hh[sta - 1]; + int r21 = hh[sta + len - 1] >> 32, r22 = hh[sta + len - 1]; + int p1 = pw[len] >> 32, p2 = pw[len]; + r11 = (r21 - (LL)r11 * p1) % m1; + r12 = (r22 - (LL)r12 * p2) % m2; + return (LL)(r11 < 0 ? r11 + m1 : r11) << 32 | (r12 < 0 ? r12 + m2 : r12); +} +int main() { + scanf("%d", &n); + pL = pR = n + 1; + for(int i = 1; i <= n; ++i) { + char op1[3], op2[5]; + scanf("%s%s", op1, op2); + for(int j = 0; j < 7; ++j) { + if(strcmp(op2, pat[j]) != 0) + continue; + if(op1[0] == 'p') { + hh[--pL] = j; + } else { + hh[pR++] = j; + } + break; + } + sta[i] = pL; + } + pw[0] = 1LL << 32 | 1; + for(int i = pL; i < pR; ++i) { + int ch = hh[i], r1 = hh[i - 1] >> 32, r2 = hh[i - 1]; + r1 = ((LL)r1 * maxc + ch) % m1; + r2 = ((LL)r2 * maxc + ch) % m2; + hh[i] = (LL)r1 << 32 | r2; + r1 = pw[i - pL] >> 32; + r2 = pw[i - pL]; + r1 = (LL)r1 * maxc % m1; + r2 = (LL)r2 * maxc % m2; + pw[i - pL + 1] = (LL)r1 << 32 | r2; + } + for(int i = 1; i <= n; ++i) { + int L = i, R = n; + while(L < R) { + int M = (L + R + 1) >> 1; + if(get_hash(sta[M], M - i) == get_hash(sta[M] + i, M - i)) { + L = M; + } else { + R = M - 1; + } + } + ++ans[i]; + --ans[R + 1]; + ans[i] += ans[i - 1]; + printf("%d\n", ans[i]); + } + return 0; +} diff --git a/self_training/2019-08-17/D.py b/self_training/2019-08-17/D.py new file mode 100644 index 00000000..75621ec5 --- /dev/null +++ b/self_training/2019-08-17/D.py @@ -0,0 +1,28 @@ +import sys + +def exgcd(a, b) : + r_cur, r_nxt = a, b + s_cur, t_cur = 1, 0 + s_nxt, t_nxt = 0, 1 + while r_nxt != 0 : + q_cur = r_cur // r_nxt + r_cur, r_nxt = r_nxt, r_cur - q_cur * r_nxt + s_cur, s_nxt = s_nxt, s_cur - q_cur * s_nxt + t_cur, t_nxt = t_nxt, t_cur - q_cur * t_nxt + return s_cur, t_cur, r_cur + +n, m = map(int, input().split(' ')) +a1, m1 = 0, 1 +for i in range(n) : + m2, a2 = map(int, input().split(' ')) + x, y, r = exgcd(m1, m2) + if (a2 - a1) % r != 0 : + print('he was definitely lying') + sys.exit(0) + x = (a2 - a1) % m2 * x % m2 // r + a1 += m1 * x + m1 *= m2 // r +if a1 > m : + print('he was probably lying') +else : + print(a1) diff --git a/self_training/2019-08-17/F.cpp b/self_training/2019-08-17/F.cpp new file mode 100644 index 00000000..4679a4cb --- /dev/null +++ b/self_training/2019-08-17/F.cpp @@ -0,0 +1,95 @@ +#include +using namespace std; +const int maxn = (int)1e5 + 1, maxs = 3 * maxn; +int n, m; +pair a[maxn]; +struct Coordinates { + int tot, que[maxs], pR[maxs]; + vector pos[maxs]; + void build() { + sort(que, que + tot); + tot = unique(que, que + tot) - que; + memset(pR, 0x3f, tot * sizeof(int)); + for(int i = 0, j = 0; i < tot; ++i) { + for( ; j < i && que[i] - que[j] > m; ++j); + if(j < i && que[i] - que[j] == m) + pR[j] = i; + } + } + void add(int x) { + que[tot++] = x; + } + void add(int x, int id) { + pos[query(x)].push_back(id); + } + int query(int x) { + return lower_bound(que, que + tot, x) - que; + } +} X, Y; +struct MaxSet { + int tot, seq[maxs], mx, ctr[maxn]; + void build() { + ctr[mx = 0] = 0; + for(int i = 0; i < tot; ++i) { + for( ; mx < seq[i]; ctr[++mx] = 0); + ++ctr[seq[i]]; + } + } + void add(int x) { + --ctr[seq[x]]; + ++seq[x]; + if(mx < seq[x]) + ctr[++mx] = 0; + ++ctr[seq[x]]; + } + void sub(int x) { + --ctr[seq[x]]; + --seq[x]; + if(mx && !ctr[mx]) + --mx; + ++ctr[seq[x]]; + } +} F; +int main() { + scanf("%d%d", &n, &m); + for(int i = 0; i < n; ++i) { + scanf("%d%d", &a[i].first, &a[i].second); + for(int j = 0; j < 3; ++j) { + X.add(a[i].first - j * m); + Y.add(a[i].second - j * m); + } + } + X.build(); + Y.build(); + for(int i = 0; i < n; ++i) { + X.add(a[i].first, i); + Y.add(a[i].second, i); + } + F.tot = Y.tot; + for(int i = 0; i < Y.tot; ++i) { + F.seq[i] = 0; + for(int j = 0, k = i; j < 3 && k < Y.tot; ++j, k = Y.pR[k]) + F.seq[i] += (int)Y.pos[k].size(); + } + F.build(); + int ans = F.mx; + for(int i = 0; i < X.tot; ++i) { + int adt = 0; + for(int j = 0, k = i; j < 3 && k < X.tot; ++j, k = X.pR[k]) + for(int id : X.pos[k]) { + int u = Y.query(a[id].second - m - m); + for(int o = 0; o < 3; ++o, u = Y.pR[u]) + F.sub(u); + ++adt; + } + ans = max(ans, adt + F.mx); + for(int j = 0, k = i; j < 3 && k < X.tot; ++j, k = X.pR[k]) + for(int id : X.pos[k]) { + int u = Y.query(a[id].second - m - m); + for(int o = 0; o < 3; ++o, u = Y.pR[u]) + F.add(u); + } + } + printf("%d\n", ans); + return 0; +} \ No newline at end of file diff --git a/self_training/2019-08-17/J.cpp b/self_training/2019-08-17/J.cpp new file mode 100644 index 00000000..2b80cbe5 --- /dev/null +++ b/self_training/2019-08-17/J.cpp @@ -0,0 +1,39 @@ +#include +using namespace std; +typedef long long LL; +const int maxn = 5001, maxm = 2001; +int n, m, cur, pre = 1; +LL f[2][maxn], c[maxn], s[maxn]; +pair a[maxn]; +void solve(int L, int R, int pL, int pR) { + if(L > R) + return; + int M = (L + R) >> 1, pM = pL; + for(int i = pL; i < M && i <= pR; ++i) { + LL tmp = f[pre][i] + s[M] - s[i] - a[i + 1].first * (c[M] - c[i]); + if(f[cur][M] > tmp) { + f[cur][M] = tmp; + pM = i; + } + } + solve(L, M - 1, pL, pM); + solve(M + 1, R, pM, pR); +} +int main() { + scanf("%d%d", &n, &m); + for(int i = 1; i <= n; ++i) + scanf("%d%d", &a[i].second, &a[i].first); + sort(a + 1, a + n + 1); + for(int i = 1; i <= n; ++i) { + c[i] = c[i - 1] + a[i].second; + s[i] = s[i - 1] + (LL)a[i].first * a[i].second; + } + memset(f[cur] + 1, 0x3f, n * sizeof(LL)); + for(int i = 1; i <= m; ++i) { + swap(cur, pre); + memset(f[cur], 0x3f, (n + 1) * sizeof(LL)); + solve(0, n, 0, n); + } + printf("%lld\n", f[cur][n]); + return 0; +} \ No newline at end of file diff --git a/self_training/2019-08-18/A.cpp b/self_training/2019-08-18/A.cpp new file mode 100644 index 00000000..d066b195 --- /dev/null +++ b/self_training/2019-08-18/A.cpp @@ -0,0 +1,47 @@ +#include +using namespace std; + +typedef long long LL; +typedef unsigned long long ULL; +typedef double DB; +typedef pair Edge; + +namespace std { + template<> struct hash > { + size_t operator()(pair const &t) const { +#if __SIZEOF_SIZE_T__ < __SIZEOF_LONG_LONG__ + return (size_t)t.first ^ t.second; +#else + return (size_t)t.first << 32 | t.second; +#endif + } + }; +} + +const int maxn = (int)1e5 + 1, mod = (int)1e9 + 7; + +int t, n, m; + +int main() { + scanf("%d", &t); + while(t--) { + scanf("%d", &n); + static char buf[11]; + int len = sprintf(buf, "%d", n); + m = 0; + for(int i = 0; i < len; ++i) + m += buf[i] - '0'; + int cnt = 0; + for(int i = 1; i <= m; ++i) + cnt += m % i == 0 && n % i == 0; + printf("%d\n", cnt); + for(int i = 1; i <= m; ++i) + if(m % i == 0 && n % i == 0) { + if(i > 1) + printf(" "); + printf("%d", i); + } + printf("\n"); + } + return 0; +} \ No newline at end of file diff --git a/self_training/2019-08-18/B.cpp b/self_training/2019-08-18/B.cpp new file mode 100644 index 00000000..e52be48c --- /dev/null +++ b/self_training/2019-08-18/B.cpp @@ -0,0 +1,69 @@ +#include +using namespace std; + +typedef long long LL; +typedef unsigned long long ULL; +typedef double DB; +typedef pair Edge; + +namespace std { + template<> struct hash > { + size_t operator()(pair const &t) const { +#if __SIZEOF_SIZE_T__ < __SIZEOF_LONG_LONG__ + return (size_t)t.first ^ t.second; +#else + return (size_t)t.first << 32 | t.second; +#endif + } + }; +} + +const int maxn = 51; + +int t, n, m, match[maxn]; +bool e[maxn][maxn], vis[maxn]; +bool path(int u) { + for(int v = 0; v < n; ++v) { + if(vis[v] || !e[u][v]) + continue; + vis[v] = 1; + if(match[v] == -1 || path(match[v])) { + match[v] = u; + return 1; + } + } + return 0; +} +int main() { + scanf("%d", &t); + while(t--) { + scanf("%d%d", &m, &n); + for(int i = 0; i < n; ++i) { + match[i] = -1; + for(int j = 0; j < n; ++j) + e[i][j] = 1; + } + while(m--) { + static char a[maxn], b[maxn]; + scanf("%s%s", a, b); + for(int i = 0; i < n; ++i) + for(int j = 0; j < n; ++j) + e[i][j] &= a[j] == b[i]; + } + bool chk = 1; + for(int i = n - 1; i >= 0; --i) { + memset(vis, 0, n * sizeof(bool)); + if(!path(i)) { + chk = 0; + break; + } + } + if(chk) { + for(int i = 0; i < n; ++i) + printf("%d%c", match[i] + 1, " \n"[i == n - 1]); + } else { + puts("-1"); + } + } + return 0; +} \ No newline at end of file diff --git a/self_training/2019-08-18/C.cpp b/self_training/2019-08-18/C.cpp new file mode 100644 index 00000000..3fa42482 --- /dev/null +++ b/self_training/2019-08-18/C.cpp @@ -0,0 +1,57 @@ +#include +using namespace std; + +typedef long long LL; +typedef unsigned long long ULL; +typedef double DB; +typedef pair Edge; + +namespace std { + template<> struct hash > { + size_t operator()(pair const &t) const { +#if __SIZEOF_SIZE_T__ < __SIZEOF_LONG_LONG__ + return (size_t)t.first ^ t.second; +#else + return (size_t)t.first << 32 | t.second; +#endif + } + }; +} + +const int maxd = 21, maxn = 1 << maxd | 1, mod = (int)1e9 + 7; + +int t, n, mx, cnt[maxn]; +char buf[maxn]; + +int main() { + scanf("%d", &t); + while(t--) { + mx = 0; + scanf("%s", buf); + for(int i = 0, c = 1; ; ++i) + if(buf[i] == '?') { + ++c; + } else { + for( ; mx <= c; cnt[mx++] = 0); + ++cnt[c]; + if(!buf[i]) + break; + c = 1; + } + int ctr = 0, ans = 0; + for(int i = mx - 1, j = maxd - 1; i >= 1; --i) { + if(!cnt[i]) + continue; + for( ; (1 << j) > i; --j); + while(j && cnt[i] && !((ans >> j) & 1)) { + ans |= 1 << j; + --cnt[i]; + ++cnt[i - (1 << j)]; + --j; + } + ctr ^= (i & 1) & (cnt[i] & 1); + } + printf("%d\n", ans ^ ctr); + } + return 0; +} diff --git a/self_training/2019-08-18/D.cpp b/self_training/2019-08-18/D.cpp new file mode 100644 index 00000000..3b5feed7 --- /dev/null +++ b/self_training/2019-08-18/D.cpp @@ -0,0 +1,66 @@ +#include +using namespace std; + +typedef long long LL; +typedef unsigned long long ULL; +typedef double DB; +typedef pair Edge; + +namespace std { + template<> struct hash > { + size_t operator()(pair const &t) const { +#if __SIZEOF_SIZE_T__ < __SIZEOF_LONG_LONG__ + return (size_t)t.first ^ t.second; +#else + return (size_t)t.first << 32 | t.second; +#endif + } + }; +} + +const int maxn = (int)2e5 + 1, mod = (int)1e9 + 7; + +int fact[maxn], iact[maxn]; +int t, n, a[maxn], m, c[maxn], f[2][3], cur, pre = 1; + +int main() { + iact[1] = 1; + for(int i = 2; i < maxn; ++i) + iact[i] = mod - (int)(mod / i * (LL)iact[mod % i] % mod); + fact[0] = iact[0] = 1; + for(int i = 1; i < maxn; ++i) { + fact[i] = (LL)fact[i - 1] * i % mod; + iact[i] = (LL)iact[i - 1] * iact[i] % mod; + } + scanf("%d", &t); + while(t--) { + scanf("%d", &n); + n <<= 1; + for(int i = 0; i < n; ++i) + scanf("%d", a + i); + m = 0; + for(int i = 0; i < n; ++i) { + if(!i || a[i - 1] != a[i]) + c[m++] = 0; + ++c[m - 1]; + } + f[cur][1] = 1; + f[cur][0] = f[cur][2] = 0; + for(int i = 0; i < m; ++i) { + swap(cur, pre); + for(int j = 0; j < 3; ++j) { + f[cur][j] = 0; + for(int k = 0; k < 3; ++k) { + if((c[i] + j - k) & 1 || !f[pre][k]) + continue; + int lft = (c[i] + j - k) >> 1, rht = c[i] - lft; + if(lft < 0 || rht < 0) + continue; + f[cur][j] = (f[cur][j] + (LL)fact[c[i]] * iact[lft] % mod * iact[rht] % mod * f[pre][k]) % mod; + } + } + } + printf("%d\n", f[cur][1]); + } + return 0; +} \ No newline at end of file diff --git a/self_training/2019-08-18/E.cpp b/self_training/2019-08-18/E.cpp new file mode 100644 index 00000000..79588979 --- /dev/null +++ b/self_training/2019-08-18/E.cpp @@ -0,0 +1,89 @@ +#include +using namespace std; + +typedef long long LL; +typedef unsigned long long ULL; +typedef double DB; +typedef pair Edge; + +namespace std { + template<> struct hash > { + size_t operator()(pair const &t) const { +#if __SIZEOF_SIZE_T__ < __SIZEOF_LONG_LONG__ + return (size_t)t.first ^ t.second; +#else + return (size_t)t.first << 32 | t.second; +#endif + } + }; +} + +const int maxn = (int)1e5 + 3, maxn2 = maxn << 1 | 1; + +int t, n, dep[maxn], idx[maxn], pL[maxn], pR[maxn], tot, cL[maxn2], cR[maxn2], upp[maxn2]; +set sp[maxn]; +bool fir; +void dfs(int u) { + if(u <= n) + return; + if(fir) { + fir = 0; + } else { + printf(" "); + } + printf("%d", upp[cL[u]]); + dfs(cL[u]); + dfs(cR[u]); +} +int main() { + scanf("%d", &t); + while(t--) { + scanf("%d", &n); + for(int i = 0; i < n; ++i) + sp[i].clear(); + pR[0] = 1; + for(int i = 1; i <= n; ++i) { + scanf("%d", dep + i); + idx[i] = i; + pL[i] = i - 1; + pR[i] = i + 1; + upp[i] = i; + sp[dep[i]].insert(i); + } + pL[n + 1] = n; + tot = n; + bool chk = 1; + for(int mx = n - 1; mx >= 1; --mx) { + if(!sp[mx].size()) + continue; + int u = *sp[mx].begin(); + int v = pR[u]; + if(v > n || dep[v] != mx) { + chk = 0; + break; + } + sp[mx].erase(u); + sp[mx].erase(v); + ++tot; + cL[tot] = idx[u]; + cR[tot] = idx[v]; + upp[tot] = upp[cR[tot]]; + pL[pR[u]] = pL[u]; + pR[pL[u]] = pR[u]; + idx[v] = tot; + sp[--dep[v]].insert(v); + if(sp[mx].size()) + ++mx; + } + chk &= (int)sp[0].size() == 1; + if(!chk) { + puts("Impossible"); + } else { + puts("Possible"); + fir = 1; + dfs(tot); + printf("\n"); + } + } + return 0; +} diff --git a/self_training/2019-08-18/F.cpp b/self_training/2019-08-18/F.cpp new file mode 100644 index 00000000..da0ffd92 --- /dev/null +++ b/self_training/2019-08-18/F.cpp @@ -0,0 +1,65 @@ +#include +using namespace std; + +typedef long long LL; +typedef unsigned long long ULL; +typedef double DB; +typedef pair Edge; + +namespace std { + template<> struct hash > { + size_t operator()(pair const &t) const { +#if __SIZEOF_SIZE_T__ < __SIZEOF_LONG_LONG__ + return (size_t)t.first ^ t.second; +#else + return (size_t)t.first << 32 | t.second; +#endif + } + }; +} + +const int maxd = 21, maxn = 1 << maxd | 1, mod = (int)1e9 + 7; + +int t, n, m, seq[maxn], mx, f[maxd + 2][maxn]; +char buf[maxn]; +inline void mod_inc(int &x, int y) { + (x += y) >= mod && (x -= mod); +} +int main() { + scanf("%d", &t); + while(t--) { + scanf("%s", buf); + n = strlen(buf) + 1; + m = 0; + for(int i = 0, c = 1; i < n; ++i) + if(buf[i] == '+') { + ++c; + } else { + seq[++m] = c; + ++c; + } + for(mx = 0; (1 << mx) <= n; ++mx); + for(int i = 0; i <= mx; ++i) + f[i][0] = 1; + for(int i = 1, o = 1; i <= n; ++i) { + if((n & i) == i) + for(int j = 0; j <= mx; ++j) { + f[j][i] = 0; + if(j) + mod_inc(f[j][i], f[j - 1][i]); + if((i >> j) & 1) + mod_inc(f[j][i], f[j][i ^ (1 << j)]); + } + if(o < m && i == seq[o]) { + if((n & i) == i) { + int adt = f[mx][i]; + for(int j = 0; j <= mx; ++j) + mod_inc(f[j][i], adt); + } + ++o; + } + } + printf("%d %d\n", n, f[mx][n]); + } + return 0; +} diff --git a/self_training/2019-08-23/1005.cpp b/self_training/2019-08-23/1005.cpp new file mode 100644 index 00000000..32a959f7 --- /dev/null +++ b/self_training/2019-08-23/1005.cpp @@ -0,0 +1,51 @@ +#include +using namespace std; +typedef long long LL; +const int maxn = (int)1e6 + 1, maxs = (int)1e3 + 1; +int tot, pr[maxn >> 3 | 1], d[maxn], n; +LL f[maxn]; +__int128_t g[maxs]; +inline LL num1(int L, int R) { + return (L + R) * (R - L + 1LL) / 2; +} +__int128_t calc_iphi(int x) { + if(x < maxn) + return f[x]; + __int128_t &ret = g[n / x]; + if(ret == -1) { + ret = x * (x + 1LL) * (__int128_t)(x + x + 1) / 6; + for(int i = 2, j; i <= x; i = j + 1) { + j = x / (x / i); + ret -= calc_iphi(x / i) * num1(i, j); + } + } + return ret; +} +int main() { + d[1] = f[1] = 1; + for(int i = 2; i < maxn; ++i) { + if(!d[i]) { + pr[tot++] = d[i] = i; + f[i] = i - 1; + } + for(int j = 0, k; (k = i * pr[j]) < maxn; ++j) { + d[k] = pr[j]; + f[k] = f[i] * pr[j]; + if(d[i] == pr[j]) + break; + f[k] -= f[i]; + } + f[i] = f[i - 1] + i * f[i]; + } + int t; + scanf("%d", &t); + while(t--) { + scanf("%d%*d%*d", &n); + if(n >= maxn) + memset(g, -1, (n / maxn + 1) * sizeof(__int128_t)); + __int128_t ans = (calc_iphi(n) - 1) >> 1; + const int mod = (int)1e9 + 7; + printf("%d\n", (int)(ans % mod)); + } + return 0; +} \ No newline at end of file diff --git a/self_training/2019-08-23/1009.cpp b/self_training/2019-08-23/1009.cpp new file mode 100644 index 00000000..656220d2 --- /dev/null +++ b/self_training/2019-08-23/1009.cpp @@ -0,0 +1,72 @@ +#include +using namespace std; +typedef long long LL; +typedef unsigned long long ULL; +const int maxn = 31; +int t, n[2], mod, c[maxn][maxn], pw2[maxn * maxn], prb[maxn][maxn], cur, pre = 1; +LL f[2][maxn][maxn][maxn]; +inline void dp_clear(int step) { + step &= 1; + int lim0 = n[!step], lim1 = n[step]; + for(int i = 0; i <= lim0; ++i) + for(int j = 1; j <= lim1; ++j) + memset(f[cur][i][j] + 1, 0, j * sizeof(LL)); +} +inline int mod_fix(LL &x) { + return x < mod ? x : (x %= mod); +} +inline void mod_inc(LL &x, LL y) { + (x += y) < 0 && (x = (ULL)x % mod); +} +inline int mod_inv(int x) { + return x <= 1 ? x : mod - (int)(mod / x * (LL)mod_inv(mod % x) % mod); +} +int main() { + scanf("%d", &t); + while(t--) { + scanf("%d%d%d", n + 0, n + 1, &mod); + for(int i = 0; i <= n[0] || i <= n[1]; ++i) { + c[i][0] = c[i][i] = 1; + for(int j = 1; j < i; ++j) + (c[i][j] = c[i - 1][j] + c[i - 1][j - 1]) >= mod && (c[i][j] -= mod); + } + pw2[0] = 1; + pw2[1] = (mod + 1) >> 1; + for(int i = 1; i <= n[0] * n[1]; ++i) + pw2[i] = (LL)pw2[i - 1] * pw2[1] % mod; + for(int i = 0; i <= max(n[0], n[1]); ++i) { + prb[i][0] = 1; + (prb[i][1] = i ? prb[i - 1][1] << 1 | 1 : 0) >= mod && (prb[i][1] -= mod); + for(int j = 2; j <= max(n[0], n[1]); ++j) + prb[i][j] = (LL)prb[i][j - 1] * prb[i][1] % mod; + } + dp_clear(0); + f[cur][0][1][1] = 1; + LL ans = 0; + for(int i = 1; i < min(n[0], n[1]) * 2; ++i) { + swap(cur, pre); + dp_clear(i); + int lim0 = n[!(i & 1)], lim1 = n[i & 1]; + for(int j = 0; j < lim1; ++j) + for(int k = 1; k <= lim0; ++k) + for(int x = 1; x <= k; ++x) { + if(!mod_fix(f[pre][j][k][x])) + continue; + int coeff = pw2[x * (lim1 - j)] * f[pre][j][k][x] % mod; + for(int y = 1, jj = j + 1; jj <= lim1; ++y, ++jj) + mod_inc(f[cur][k][jj][y], (LL)coeff * c[lim1 - j][y] % mod * prb[x][y]); + } + if(i & 1) + for(int j = 0; j <= lim0; ++j) + for(int k = 1; k <= lim1; ++k) + for(int x = 1; x <= k; ++x) { + if(!mod_fix(f[cur][j][k][x])) + continue; + mod_inc(ans, (LL)x * i * f[cur][j][k][x]); + } + } + ans = (LL)mod_fix(ans) * mod_inv(n[1]) % mod; + printf("%d\n", mod_fix(ans)); + } + return 0; +} \ No newline at end of file diff --git a/self_training/2019-08-23/1010.cpp b/self_training/2019-08-23/1010.cpp new file mode 100644 index 00000000..cd41ebac --- /dev/null +++ b/self_training/2019-08-23/1010.cpp @@ -0,0 +1,147 @@ +#include +using namespace std; +namespace fastIO{ + #define BUF_SIZE 1000000 + #define OUT_SIZE 1000000 + #define ll long long + //fread->read + bool IOerror=0; + inline char nc(){ + static char buf[BUF_SIZE],*p1=buf+BUF_SIZE,*pend=buf+BUF_SIZE; + if (p1==pend){ + p1=buf; pend=buf+fread(buf,1,BUF_SIZE,stdin); + if (pend==p1){IOerror=1;return -1;} + } + return *p1++; + } + inline bool blank(char ch){return ch==' '||ch=='\n'||ch=='\r'||ch=='\t';} + inline void read(int &x){ + bool sign=0; char ch=nc(); x=0; + for (;blank(ch);ch=nc()); + if (IOerror)return; + if (ch=='-')sign=1,ch=nc(); + for (;ch>='0'&&ch<='9';ch=nc())x=x*10+ch-'0'; + if (sign)x=-x; + } + //fwrite->write + struct Ostream_fwrite{ + char *buf,*p1,*pend; + Ostream_fwrite(){buf=new char[BUF_SIZE];p1=buf;pend=buf+BUF_SIZE;} + void out(char ch){ + if (p1==pend){ + fwrite(buf,1,BUF_SIZE,stdout);p1=buf; + } + *p1++=ch; + } + void println(unsigned x){ + static char s[15],*s1;s1=s; + if (!x)*s1++='0';if (x<0)out('-'),x=-x; + while(x)*s1++=x%10+'0',x/=10; + while(s1--!=s)out(*s1); + out('\n'); + } + void flush(){if (p1!=buf){fwrite(buf,1,p1-buf,stdout);p1=buf;}} + ~Ostream_fwrite(){flush();} + }Ostream; + inline void println(unsigned x){Ostream.println(x);} + inline void flush(){Ostream.flush();} + #undef ll + #undef OUT_SIZE + #undef BUF_SIZE +}; +using namespace fastIO; +typedef long long LL; +const int maxn = (int)1e5 + 1, maxv = (int)1e7 + 1, maxe = 9, maxs = maxn << 8 | 1; +int tot, pr[maxv >> 3 | 1], d[maxv]; +int t, n, m, sq, a[maxn], b[maxn], pos[maxn], ord[maxn], ans[maxn]; +struct Query { + int L, R; + bool operator < (Query const &t) const { + if(pos[L] != pos[t.L]) + return pos[L] < pos[t.L]; + if(pos[L] & 1) + return R < t.R; + return R > t.R; + } +} que[maxn]; +int ptot, p[maxe], tail[maxn], seq[maxs]; +void dfs(int dep, int val) { + if(dep == ptot) { + seq[tot++] = val; + return; + } + dfs(dep + 1, val); + dfs(dep + 1, -val * p[dep]); +} +int sum0, sum1, cnt[maxv]; +void mo_upd(int idx, int sgn) { + int res = 0, val = a[idx] * sgn; + for(int i = tail[idx - 1]; i < tail[idx]; ++i) { + int pos = seq[i]; + if(pos < 0) { + res -= cnt[-pos]; + cnt[-pos] += val / -pos; + } else { + res += cnt[pos]; + cnt[pos] += val / pos; + } + } + sum0 += val * b[idx]; + sum1 += val * (sgn < 0 ? res - b[idx] : res); + // printf("after %d (%+d): %d %d\n", v, sgn, sum0, sum1); +} +int main() { + d[1] = 1; + for(int i = 2; i < maxv; ++i) { + if(!d[i]) + pr[tot++] = d[i] = i; + for(int j = 0, k; (k = i * pr[j]) < maxv; ++j) { + d[k] = pr[j]; + if(d[i] == pr[j]) + break; + } + } + read(t); // scanf("%d", &t); + while(t--) { + read(n), read(m); // scanf("%d%d", &n, &m); + for(sq = 1; sq * sq <= m; ++sq); + sq = min(max((int)ceil(n / (sq - 1.0)), 1), n); + tot = tail[0] = 0; + for(int i = 1; i <= n; ++i) { + read(a[i]); // scanf("%d", a + i); + pos[i] = i <= sq ? 1 : pos[i - sq] + 1; + int tmp = a[i], &phi = b[i] = tmp; + for(ptot = 0; tmp > 1; ++ptot) { + phi -= phi / d[tmp]; + for(int pp = p[ptot] = d[tmp]; d[tmp] == pp; tmp /= pp); + } + dfs(0, 1); + tail[i] = tot; + } + for(int i = 1; i <= m; ++i) { + read(que[i].L), read(que[i].R); // scanf("%d%d", &que[i].L, &que[i].R); + ord[i] = i; + } + sort(ord + 1, ord + m + 1, [&](int const &u, int const &v) { + return que[u] < que[v]; + }); + int qL = que[ord[1]].L, qR = qL - 1; + for(int i = 1; i <= m; ++i) { + int pL = que[ord[i]].L, pR = que[ord[i]].R; + for( ; qR < pR; ++qR) + mo_upd(qR + 1, 1); + for( ; qL > pL; --qL) + mo_upd(qL - 1, 1); + for( ; qL < pL; ++qL) + mo_upd(qL, -1); + for( ; qR > pR; --qR) + mo_upd(qR, -1); + ans[ord[i]] = sum0 + sum1 + sum1; + } + for( ; qL <= qR; ++qL) + mo_upd(qL, -1); + for(int i = 1; i <= m; ++i) + println((unsigned)ans[i]); // printf("%u\n", ans[i]); + } + return 0; +} diff --git a/self_training/2019-08-23/1011.cpp b/self_training/2019-08-23/1011.cpp new file mode 100644 index 00000000..3e5ecd23 --- /dev/null +++ b/self_training/2019-08-23/1011.cpp @@ -0,0 +1,87 @@ +#include +using namespace std; +typedef long long LL; +const int maxn = (int)1e5 + 1, mod = 998244353; +int mod_pow(int x, int k, int m) { + int ret = m > 1 ? 1 : 0; + for( ; k > 0; k >>= 1, x = (LL)x * x % m) + if(k & 1) + ret = (LL)ret * x % m; + return ret; +} +pair solve(int n) { + static const int mod = 1 << 23; + static bool enabled = 0; + static int fact[mod + 1]; + if(!enabled) { + fact[0] = 1; + for(int i = 1; i < mod; ++i) { + fact[i] = fact[i - 1]; + if(i & 1) + fact[i] = (LL)fact[i - 1] * i % mod; + } + enabled = 1; + } + if(!n) + return make_pair(1, 0); + pair ret = solve(n >> 1); + ret.first = (LL)ret.first * mod_pow(fact[mod - 1], n / mod, mod) % mod * fact[n % mod] % mod; + ret.second += n >> 1; + return ret; +} +int solve(int n, int x, int y) { + // n! / x! / y! / (n - x - y)! + pair t1 = solve(n), t2 = solve(x), t3 = solve(y), t4 = solve(n - x - y); + int mod = 1 << 23, res = (LL)t1.first * mod_pow((LL)t2.first * t3.first % mod * t4.first % mod, (mod >> 1) - 1, mod) % mod; + for(int i = min(t1.second - t2.second - t3.second - t4.second, 23); i > 0; --i) + (res <<= 1) >= mod && (res -= mod); + static bool enabled = 0; + static int c7[7][7], c17[17][17]; + if(!enabled) { + for(int i = 0; i < 7; ++i) + c7[0][i] = i ? 0 : 1; + for(int i = 1; i < 7; ++i) + for(int j = 0; j < 7; ++j) + c7[i][j] = (c7[i - 1][j] + (j ? c7[i - 1][j - 1] : 0)) % 7; + for(int i = 0; i < 17; ++i) + c17[0][i] = i ? 0 : 1; + for(int i = 1; i < 17; ++i) + for(int j = 0; j < 17; ++j) + c17[i][j] = (c17[i - 1][j] + (j ? c17[i - 1][j - 1] : 0)) % 17; + enabled = 1; + } + int r7 = 1; + for(int u = n, v = x + y, w = x; u > 0; u /= 7, v /= 7, w /= 7) { + int A = u % 7, B = v % 7, C = w % 7; + r7 = r7 * c7[A][B] * c7[B][C] % 7; + } + for( ; res % 7 != r7; res += mod); + mod *= 7; + int r17 = 1; + for(int u = n, v = x + y, w = x; u > 0; u /= 17, v /= 17, w /= 17) { + int A = u % 17, B = v % 17, C = w % 17; + r17 = r17 * c17[A][B] * c17[B][C] % 17; + } + for( ; res % 17 != r17; res += mod); + mod *= 17; + return res; +} +int t1, t2, p, q, n, m, x, y, v, ans; +int main() { + while(scanf("%d%d%d%d%d%d", &t1, &t2, &p, &q, &n, &m) == 6) { + ans = 1; + while(m--) { + scanf("%d%d%d", &x, &y, &v); + if(x % p > 0 || y % q > 0 || v <= 1) + continue; + x /= p; + y /= q; + if(x + y > n) + continue; + int pw = (LL)solve(n, x, y) * mod_pow(t1, x, mod - 1) % (mod - 1) * mod_pow(t2, y, mod - 1) % (mod - 1); + ans = (LL)ans * mod_pow(v, pw, mod) % mod; + } + printf("%d\n", ans); + } + return 0; +} diff --git a/self_training/2019-08-31/A.cpp b/self_training/2019-08-31/A.cpp new file mode 100644 index 00000000..471eead7 --- /dev/null +++ b/self_training/2019-08-31/A.cpp @@ -0,0 +1,36 @@ +#include +using namespace std; +typedef long long LL; +const int maxn = (int)1e5 + 1; +int t, n, a[2][maxn]; +LL f[2][maxn]; +vector e[maxn]; +void dfs(int u, int fa) { + f[0][u] = f[1][u] = 0; + for(int v : e[u]) { + if(v == fa) + continue; + dfs(v, u); + f[0][u] += max(f[0][v] + abs(a[0][u] - a[0][v]), f[1][v] + abs(a[0][u] - a[1][v])); + f[1][u] += max(f[0][v] + abs(a[1][u] - a[0][v]), f[1][v] + abs(a[1][u] - a[1][v])); + } +} +int main() { + scanf("%d", &t); + while(t--) { + scanf("%d", &n); + for(int i = 1; i < n; ++i) { + int u, v; + scanf("%d%d", &u, &v); + e[u].push_back(v); + e[v].push_back(u); + } + for(int i = 1; i <= n; ++i) + scanf("%d%d", a[0] + i, a[1] + i); + dfs(1, -1); + printf("%lld\n", max(f[0][1], f[1][1])); + for(int i = 1; i <= n; ++i) + vector().swap(e[i]); + } + return 0; +} \ No newline at end of file diff --git a/self_training/2019-08-31/B.cpp b/self_training/2019-08-31/B.cpp new file mode 100644 index 00000000..216cea4e --- /dev/null +++ b/self_training/2019-08-31/B.cpp @@ -0,0 +1,40 @@ +#include +using namespace std; +typedef long long LL; +const int maxd = 101; +int t, len; +LL a, b, c, d; +char buf[maxd]; +int main() { + scanf("%d", &t); + while(t--) { + scanf("%lld%lld%lld%lld", &a, &b, &c, &d); + c = c - a; + d = b - d; + LL tp = a - b; + if(!tp) { + if(!c && !d) { + puts("Yes\n"); + } else { + puts("No"); + } + continue; + } + if(c % tp != 0 || d % tp != 0) { + puts("No"); + continue; + } + c /= tp; + d /= tp; + if(c < 0 || d < 0 || (c & d) || ((c + d) & (c + d + 1))) { + puts("No"); + continue; + } + for(len = 0; c || d; c >>= 1, d >>= 1) + buf[len++] = "AB"[c & 1]; + buf[len] = '\0'; + puts("Yes"); + puts(buf); + } + return 0; +} diff --git a/self_training/2019-08-31/C.cpp b/self_training/2019-08-31/C.cpp new file mode 100644 index 00000000..d8d0214a --- /dev/null +++ b/self_training/2019-08-31/C.cpp @@ -0,0 +1,58 @@ +#include +using namespace std; +typedef long long LL; +const int maxn = (int)1e5 + 1, seed = 233, mod = (int)1e9 + 7; +int t, n, rt, ch[2][maxn], sz[maxn], low[maxn], tot, idx[maxn]; +void pfs(int u) { + if(sz[u] != -1) + return; + sz[u] = 1; + int v = ch[0][u]; + if(v) { + pfs(v); + sz[u] += sz[v]; + low[u] = min(low[u], low[v]); + } + v = ch[1][u]; + if(v) { + pfs(v); + sz[u] += sz[v]; + low[u] = min(low[u], low[v]); + } +} +void dfs(int u) { + int o = 0; + if(low[u] == u) { + if(sz[ch[0][u]] > sz[ch[1][u]] || (sz[ch[0][u]] == sz[ch[1][u]] && low[ch[0][u]] > low[ch[1][u]])) + o ^= 1; + } else if(!ch[0][u] || (ch[1][u] && low[ch[0][u]] > low[ch[1][u]])) + o ^= 1; + if(ch[o][u]) + dfs(ch[o][u]); + idx[u] = ++tot; + o ^= 1; + if(ch[o][u]) + dfs(ch[o][u]); +} +int main() { + scanf("%d", &t); + while(t--) { + scanf("%d", &n); + for(int i = 1; i <= n; ++i) { + scanf("%d%d", ch[0] + i, ch[1] + i); + sz[i] = -1; + low[i] = i; + } + for(int i = 1; i <= n; ++i) { + pfs(i); + if(sz[i] == n) + rt = i; + } + tot = 0; + dfs(rt); + int ans = 0; + for(int i = n; i >= 0; --i) + ans = ((LL)ans * seed + (idx[i] ^ i)) % mod; + printf("%d\n", ans); + } +} \ No newline at end of file diff --git a/self_training/2019-08-31/E.cpp b/self_training/2019-08-31/E.cpp new file mode 100644 index 00000000..4c85bbe1 --- /dev/null +++ b/self_training/2019-08-31/E.cpp @@ -0,0 +1,63 @@ +#include +using namespace std; +typedef long long LL; +const int maxn = (int)1e6 + 1, mod = (int)1e9 + 7; + +int inv[maxn], sum[maxn]; + +inline int mod_inv(int x) { + assert(x); + return x < maxn ? inv[x] : (mod - (int)(mod / x * (LL)mod_inv(mod % x) % mod)); +} + +int solve1(int n, int m, int t) { + int ret = (((LL)m * m - 1) * n * 3 - ((LL)m * m * m - 1) % mod * (t + 1)) % mod * t % mod * mod_inv(6) % mod; + return ret < 0 ? ret + mod : ret; +} + +int solve2(int n, int t) { + int ret = ((LL)n * n * n % mod * sum[t] + (t + 1LL - n * 3) * t) % mod * mod_inv(6) % mod; + return ret < 0 ? ret + mod : ret; +} + +int solve(int n, int m) { + int ret = solve2(n, n), pos = (n - 1) / m; + if(pos >= 1) + ret = (ret + solve1(n, m, pos) - solve2(n, pos)) % mod; + ret = (((LL)n * n - 1) * (m - 1) % mod * mod_inv(2) - ret) % mod; + return ret < 0 ? ret + mod : ret; +} + +double foo(int a, int b, int c, int d, int lim = (int)1e7) { + mt19937_64 rnd(time(NULL)); + double sum = 0, cnt = 0; + while(cnt < lim) { + double p = (double)rnd() / (1LL << 32) / (1LL << 32) * (b - a) + a; + double q = (double)rnd() / (1LL << 32) / (1LL << 32) * (d - c) + c; + while(p >= q) + p -= q; + while(p < 0) + p += q; + sum += p; + ++cnt; + } + return sum / cnt; +} + +int main() { + inv[1] = sum[1] = 1; + for(int i = 2; i < maxn; ++i) { + inv[i] = mod - (int)(mod / i * (LL)inv[mod % i] % mod); + sum[i] = (sum[i - 1] + (LL)inv[i] * inv[i]) % mod; + assert((LL)inv[i] * i % mod == 1); + } + int t; + scanf("%d", &t); + while(t--) { + int a, b, c, d; + scanf("%d%d%d%d", &a, &b, &c, &d); + int ans = ((LL)solve(b, d) + solve(a, c) - solve(a, d) - solve(b, c)) * mod_inv((LL)(b - a) * (d - c) % mod) % mod; + printf("%d\n", ans < 0 ? ans + mod : ans); + } + return 0; +} \ No newline at end of file diff --git a/self_training/2019-09-07/H.cpp b/self_training/2019-09-07/H.cpp new file mode 100644 index 00000000..7b9650e1 --- /dev/null +++ b/self_training/2019-09-07/H.cpp @@ -0,0 +1,87 @@ +// (n+1)k - p * C(k+1, 2) +#include +using namespace std; +typedef long long LL; +const int maxn = (int)1e5 + 1, maxs = (int)2e5 + 1, mod = 998244353; +int tot, pr[maxn], d[maxn], c[2][maxn], cnt, f[2][maxs]; +LL n, fct[maxs], sq[maxs]; +inline void mod_inc(int &x, int y) { + (x += y) >= mod && (x -= mod); +} +inline LL iSqrt(LL x) { + LL r; + for(r = (int)sqrtl(x); r * r <= x; ++r); + for( ; r * r > x; --r); + return r; +} +int main() { + scanf("%lld", &n); + int sqN = iSqrt(n); + assert(sqN < maxn); + d[1] = 1; + for(int i = 2; i <= sqN; ++i) { + c[0][i] = c[0][i - 1]; + c[1][i] = c[1][i - 1]; + if(!d[i]) { + pr[tot++] = d[i] = i; + ++c[0][i]; + mod_inc(c[1][i], i); + } + for(int j = 0, k; (k = i * pr[j]) <= sqN; ++j) { + d[k] = pr[j]; + if(d[i] == pr[j]) + break; + } + } + for(int i = 1; i <= sqN; ++i) + fct[++cnt] = i; + for(int i = sqN - (n / sqN == sqN); i >= 1; --i) + fct[++cnt] = n / i; + for(int i = 1; i <= cnt; ++i) { + sq[i] = iSqrt(fct[i]); + f[0][i] = fct[i] % mod; + f[1][i] = f[0][i] * (f[0][i] + 1LL) / 2 % mod; + } //int ctr = 0; + for(int i = 0; i < tot; ++i) { //printf("#%d (%d): ", i + 1, pr[i]); + for(int j = cnt; j >= 1; --j) { //++ctr; + LL nxt = fct[j] / pr[i]; + if(nxt < pr[i]) + break; + int pos = nxt <= sqN ? nxt : cnt - n / nxt + 1; + int v0 = f[0][pos], v1 = f[1][pos]; + if(i && pr[i - 1] > sq[pos]) { + mod_inc(v0, c[0][sq[pos]]); + mod_inc(v0, mod - c[0][pr[i - 1]]); + mod_inc(v1, c[1][sq[pos]]); + mod_inc(v1, mod - c[1][pr[i - 1]]); + } + v1 = (LL)v1 * pr[i] % mod; + mod_inc(f[0][j], mod - v0); + mod_inc(f[1][j], mod - v1); + //printf(" (%lld: %d, %d)", fct[j], f[0][j], f[1][j]); + } //printf("\n"); + } //fprintf(stderr, "ctr: %d, cnt: %d\n", ctr, cnt); + for(int i = 1; i <= cnt; ++i) { + mod_inc(f[0][i], c[0][sq[i]]); + mod_inc(f[1][i], c[1][sq[i]]); + //printf("#%d (%lld): %d %d\n", i, fct[i], f[0][i], f[1][i]); + } + int ans = 0; + for(LL i = 2, j, k; i <= n; i = j + 1) { + k = n / i; + j = n / k; + int pos = j <= sqN ? j : cnt - k + 1; + int v0 = f[0][pos] - f[0][pos - 1], v1 = f[1][pos] - f[1][pos - 1]; + //printf("k=%lld, [%lld, %lld]: %d %d\n", k, i, j, v0 < 0 ? v0 + mod : v0, v1 < 0 ? v1 + mod : v1); + k %= mod; + ans = (ans + (n + 1) % mod * k % mod * v0 - k * (k + 1LL) / 2 % mod * v1) % mod; + } + for(int i = 0; i < tot; ++i) + for(LL val = (LL)pr[i] * pr[i]; val <= n; val *= pr[i]) { + int adt = n / val % mod; + //printf("p^e=%lld, k=%lld\n", val, n / val); + ans = (ans + (n + 1) % mod * adt - adt * (adt + 1LL) / 2 % mod * (val % mod)) % mod; + } + printf("%d\n", ans < 0 ? ans + mod : ans); + return 0; +} diff --git a/self_training/2019-09-08/A.cpp b/self_training/2019-09-08/A.cpp new file mode 100644 index 00000000..5d525478 --- /dev/null +++ b/self_training/2019-09-08/A.cpp @@ -0,0 +1,147 @@ +#include +using namespace std; +const int maxn = (int)1e7 + 1, maxm = 100, maxv = (int)1.5e8, maxs = (int)2e4 + 1, maxc = (int)1e3 + 1; +int tot, pr[maxn >> 3 | 1], d[maxn], phi[maxn], f[maxm + 1]; +void sieve(int L, int R) { // [L, R] + if(L > R) + return; + if(R < maxn) { + memcpy(f, phi + L, (R - L + 1) * sizeof(int)); + return; + } + assert(R - L + 1 <= maxm); + static int g[maxm + 1]; + int *val = f - L, *rem = g - L, lim = (int)ceil(sqrtl(R)); + for(int i = L; i <= R; ++i) { + val[i] = 1; + rem[i] = i; + } + for(int i = 0, phi; pr[i] <= lim; ++i) + for(int j = R / pr[i] * pr[i]; j >= L; j -= pr[i]) { + for(phi = pr[i] - 1, rem[j] /= pr[i]; rem[j] % pr[i] == 0; rem[j] /= pr[i], phi *= pr[i]); + val[j] *= phi; + } + for(int i = L; i <= R; ++i) + if(rem[i] > 1) + val[i] *= rem[i] - 1; +} +int cnt, pp[maxc], sz, que[maxs]; +void dfs(int dep, int val, int phi) { + if(phi & 1 || dep == cnt) { + if(phi == 1) + que[sz++] = val; + } else { + for(int i = dep; pp[i] - 1 <= phi; ++i) { + if(phi % (pp[i] - 1) > 0) + continue; + int upp = (maxv + maxm - 1) / pp[i]; + if(val > upp) + break; + int nxt_val = val * pp[i], nxt_phi = phi / (pp[i] - 1); + dfs(i + 1, nxt_val, nxt_phi); + while(nxt_val <= upp && nxt_phi % pp[i] == 0) + dfs(i + 1, nxt_val *= pp[i], nxt_phi /= pp[i]); + } + } +} +bool isPrime(int x) { + if(x < maxn) + return x == d[x]; + for(int i = 0; pr[i] * pr[i] <= x; ++i) + if(x % pr[i] == 0) + return 0; + return 1; +} +void getCandidates(int n) { + sz = 0; + if(n & 1) { + if(n == 1) { + que[sz++] = 1; + que[sz++] = 2; + } + } else { + cnt = 0; + for(int i = 0; (pr[i] - 1) * (pr[i] - 1) <= n; ++i) + if(n % (pr[i] - 1) == 0) + pp[cnt++] = pr[i]; + for(int i = 1; i * i <= n; ++i) + if(n % i == 0 && isPrime(n / i + 1)) + pp[cnt++] = n / i + 1; + assert(cnt < maxc); + sort(pp, pp + cnt); + cnt = unique(pp, pp + cnt) - pp; + pp[cnt] = n + 2; + dfs(0, 1, n); + } + assert(sz < maxs); + sort(que, que + sz); + sz = unique(que, que + sz) - que; +} +int seq[maxm + 1]; +bool can[maxm + 1][maxm + 1]; +int main() { + d[1] = phi[1] = 1; + for(int i = 2; i < maxn; ++i) { + if(!d[i]) { + pr[tot++] = d[i] = i; + phi[i] = i - 1; + } + for(int j = 0, k; (k = i * pr[j]) < maxn; ++j) { + d[k] = pr[j]; + phi[k] = phi[i] * pr[j]; + if(d[i] == pr[j]) + break; + phi[k] -= phi[i]; + } + } + pr[tot] = maxn; + int t; + scanf("%d", &t); + while(t--) { + int pos = 0; + for(int i = 0; i < maxm; ++i) { + scanf("%d", seq + i); + if(seq[i] < seq[pos]) + pos = i; + } + bool good = 1; + for(int i = 0; pr[i] <= maxm; ++i) { + for(int j = 0; j < pr[i]; ++j) + can[i][j] = 1; + for(int j = 0, k = 0; j < maxm; ++j, k = k ? k - 1 : pr[i] - 1) + if(seq[j] % (pr[i] - 1) != 0) + can[i][k] = 0; + good = 0; + for(int j = 0; j < pr[i]; ++j) + good |= can[i][j]; + if(!good) + break; + } + if(!good) { + puts("NO"); + continue; + } + getCandidates(seq[pos]); + bool chk = 0; + for(int i = 0; i < sz; ++i) { + int tmp = que[i] - pos; + bool bad = tmp < 1 || tmp > maxv; + for(int j = 0; !bad && pr[j] <= maxm; ++j) + bad |= !can[j][tmp % pr[j]]; + if(bad) + continue; + sieve(tmp, tmp + maxm - 1); + for(int j = 0; !bad && j < maxm; ++j) + bad |= seq[j] != f[j]; + if(!bad) { + chk = 1; + puts("YES"); + printf("%d\n", tmp); + break; + } + } + if(!chk) + puts("NO"); + } + return 0; +} diff --git a/self_training/2019-09-08/E.cpp b/self_training/2019-09-08/E.cpp new file mode 100644 index 00000000..0944e69d --- /dev/null +++ b/self_training/2019-09-08/E.cpp @@ -0,0 +1,28 @@ +#include +using namespace std; +int t, n, m, q, dt; +int main() { + scanf("%d", &t); + while(t--) { + scanf("%d%d%d", &n, &dt, &q); + ++dt; + while(q--) { + scanf("%d", &m);int tp = m; + --m; + int idx = 0; + while(m) { + if(m >= dt) { + int k = m / dt; + idx += k; + m -= k * dt; + } + if(m) { + ++idx; + (m = (m - dt) % (n - idx)) < 0 && (m += n - idx); + } + } + printf("%d\n", idx + 1); + } + } + return 0; +} diff --git a/self_training/2019-09-08/H.cpp b/self_training/2019-09-08/H.cpp new file mode 100644 index 00000000..254449f2 --- /dev/null +++ b/self_training/2019-09-08/H.cpp @@ -0,0 +1,62 @@ +#include +using namespace std; +typedef long long LL; +typedef unsigned long long ULL; +typedef LL Matrix[2][2]; +const int maxd = 2, BLEN = 15, BMSK = (1 << BLEN) - 1, mod = 998244353, per = (mod - 1) >> 1; +inline int mod_fix(LL &x) { // assert(x >= 0); + return x < mod ? x : (x %= mod); +} +inline LL mod_inc_mul(LL &x, int y, int z) { + y && z && (x += (LL)y * z) < 0 && (x = (ULL)x % mod); +} +inline void mat_mul(Matrix &A, Matrix &B, Matrix &C) { // A = B * C + A[0][0] = A[0][1] = A[1][0] = A[1][1] = 0; + mod_inc_mul(A[0][0], mod_fix(B[0][0]), mod_fix(C[0][0])); + mod_inc_mul(A[0][1], mod_fix(B[0][0]), mod_fix(C[0][1])); + mod_inc_mul(A[0][0], mod_fix(B[0][1]), mod_fix(C[1][0])); + mod_inc_mul(A[0][1], mod_fix(B[0][1]), mod_fix(C[1][1])); + mod_inc_mul(A[1][0], mod_fix(B[1][0]), mod_fix(C[0][0])); + mod_inc_mul(A[1][1], mod_fix(B[1][0]), mod_fix(C[0][1])); + mod_inc_mul(A[1][0], mod_fix(B[1][1]), mod_fix(C[1][0])); + mod_inc_mul(A[1][1], mod_fix(B[1][1]), mod_fix(C[1][1])); +} +Matrix f[maxd][BMSK + 2], g[2]; +int main() { + for(int i = 0; i < maxd; ++i) { + { + Matrix &cur = f[i][0]; + cur[0][0] = cur[1][1] = 1; + cur[0][1] = cur[1][0] = 0; + } + if(i) { + mat_mul(f[i][1], f[i - 1][1], f[i - 1][BMSK]); + } else { + Matrix &cur = f[i][1]; + cur[0][0] = 0; + cur[0][1] = 2; + cur[1][0] = 1; + cur[1][1] = 3; + } + for(int j = 2; j <= BMSK; ++j) + mat_mul(f[i][j], f[i][1], f[i][j - 1]); + } + int q, ans = 0, cur = 0, pre = 1; + LL n, tp; + scanf("%d%lld", &q, &n); + for(int i = 0; i < q; ++i) { + g[cur][0][0] = g[cur][1][1] = 1; + g[cur][0][1] = g[cur][1][0] = 0; + tp = n % per; + for(int j = 0; tp > 0; ++j, tp >>= BLEN) + if(tp & BMSK) { + swap(cur, pre); + mat_mul(g[cur], g[pre], f[j][tp & BMSK]); + } + int res = mod_fix(g[cur][1][0]); + ans ^= res; + n ^= (LL)res * res; + } + printf("%d\n", ans); + return 0; +} \ No newline at end of file diff --git a/self_training/2019-09-14/I.cpp b/self_training/2019-09-14/I.cpp new file mode 100644 index 00000000..7927e600 --- /dev/null +++ b/self_training/2019-09-14/I.cpp @@ -0,0 +1,151 @@ +#include +using namespace std; +const int maxn = 5001, maxm = maxn << 1 | 1, INF = 0x3f3f3f3f; +int N, M, S, T, lev[maxn], lnk[maxn], cur[maxn], side[maxn]; +struct Edge {int nxt, v, w;} e[maxm<<1|1]; +inline int newNode(int belong = -1) { + lnk[N] = -1; + side[N] = belong; + return N++; +} +inline void addEdge(int u, int v, int w) { + e[M] = (Edge){lnk[u], v, w}, lnk[u] = M++; + e[M] = (Edge){lnk[v], u, 0}, lnk[v] = M++; +} +bool bfs() { + int L = 0, R = 0; static int que[maxn]; + memset(lev, -1, N * sizeof(int)); + lev[S] = 0, que[R++] = S; + while(L < R) { + int u = que[L++]; + for(int it = lnk[u]; ~it; it = e[it].nxt) + if(e[it].w > 0 && lev[e[it].v] == -1) + lev[e[it].v] = lev[u] + 1, + que[R++] = e[it].v; + } + return lev[T] != -1; +} +int dfs(int u, int upp) { + if(u == T) return upp; // if !upp + int ret = 0, tmp; + for(int &it = cur[u]; ~it; it = e[it].nxt) + if(lev[e[it].v] == lev[u] + 1 && e[it].w > 0 + && (tmp = dfs(e[it].v, std::min(upp - ret, e[it].w))) > 0) { + e[it].w -= tmp, e[it ^ 1].w += tmp; + if((ret += tmp) == upp) break; + } + if(!ret) lev[u] = -1; + return ret; +} +bool vis[maxn], ban[maxn]; +void check(int u, int o) { + vis[u] = 1; + if(side[u] == o) + ban[u] = 1; + for(int it = lnk[u]; ~it; it = e[it].nxt) + if(e[it ^ o].w && !vis[e[it].v]) + check(e[it].v, o); +} +const int maxl = 11, maxq = 21, maxs = 101; +int r, c, q, que[maxq], ans[maxq], cntX, cntY, idx[maxs][maxs]; +char buf[maxl][maxl]; +inline bool isValid(int x, int y) { + return x >= 0 && x < r && y >= 0 && y < c && buf[x][y] != '#'; +} +void pfs(int o, int u1, int u2) { + int x1 = u1 / c, y1 = u1 % c; + int x2 = u2 / c, y2 = u2 % c; + idx[u1][u2] = newNode(o); + if(o == 0) { + ++cntX; + addEdge(S, idx[u1][u2], 1); + } else { + ++cntY; + addEdge(idx[u1][u2], T, 1); + } + for(int dx = -1; dx <= 1; ++dx) + for(int dy = -1; dy <= 1; ++dy) { + if(abs(dx) + abs(dy) != 1) + continue; + if(isValid(x1 + dx, y1 + dy)) { + int v1 = u1 + dx * c + dy; + int v2 = u2; + if(v1 > v2) + swap(v1, v2); + if(v1 != v2 && idx[v1][v2] == -1) + pfs(!o, v1, v2); + if(v1 != v2 && !o) + addEdge(idx[u1][u2], idx[v1][v2], 1); + } + if(isValid(x2 + dx, y2 + dy)) { + int v1 = u1; + int v2 = u2 + dx * c + dy; + if(v1 > v2) + swap(v1, v2); + if(v1 != v2 && idx[v1][v2] == -1) + pfs(!o, v1, v2); + if(v1 != v2 && !o) + addEdge(idx[u1][u2], idx[v1][v2], 1); + } + } +} +int main() { + while(scanf("%d%d%d", &r, &c, &q) == 3) { + int rc = r * c; + for(int i = 0; i < rc; ++i) + memset(idx[i], -1, rc * sizeof(int)); + for(int i = 0; i < r; ++i) + scanf("%s", buf[i]); + for(int i = 0; i < q; ++i) { + int x1, y1, x2, y2; + scanf("%d%d%d%d", &x1, &y1, &x2, &y2); + int u1 = (x1 - 1) * c + y1 - 1; + int u2 = (x2 - 1) * c + y2 - 1; + if(u1 > u2) + swap(u1, u2); + que[i] = u1 * rc + u2; + ans[i] = -1; + } + for(int i = 0; i < q; ++i) { + if(ans[i] != -1) + continue; + int u1 = que[i] / rc, u2 = que[i] % rc; + int x1 = u1 / c, y1 = u1 % c; + int x2 = u2 / c, y2 = u2 % c; + assert(buf[x1][y1] != '#' && buf[x2][y2] != '#'); + N = M = cntX = cntY = 0; + S = newNode(); + T = newNode(); + pfs(0, u1, u2); + int flow = 0, tmp; + while(bfs()) { + memcpy(cur, lnk, N * sizeof(int)); + while((tmp = dfs(S, INF))) flow += tmp; + } + memset(ban, 0, N * sizeof(bool)); + memset(vis, 0, N * sizeof(bool)); + check(S, 0); + memset(vis, 0, N * sizeof(bool)); + check(T, 1); + for(int j = i; j < q; ++j) { + if(ans[j] != -1) + continue; + int u1 = que[j] / rc, u2 = que[j] % rc; + if(idx[u1][u2] == -1) + continue; + if(!ban[idx[u1][u2]]) { + ans[j] = 0; + } else { + ans[j] = 1; + } + } + } + for(int i = 0; i < q; ++i) + if(!ans[i]) { + puts("ghh wins."); + } else { + puts("gfh wins."); + } + } + return 0; +} \ No newline at end of file diff --git a/self_training/2019-09-15/K.cpp b/self_training/2019-09-15/K.cpp new file mode 100644 index 00000000..8b793006 --- /dev/null +++ b/self_training/2019-09-15/K.cpp @@ -0,0 +1,139 @@ +#include +using namespace std; +typedef long long LL; +int mod_sqrt(int x, int p) { + if(!x || p <= 2) + return x; + auto mod_pow = [&p](int x, int k) { // 0 <= x < p, k >= 0 + int ret = 1; + for( ; k > 0; k >>= 1, x = (LL)x * x % p) + if(k & 1) + ret = (LL)ret * x % p; + return ret; + }; + if(mod_pow(x, (p - 1) / 2) != 1) + return -1; + static mt19937 rnd(19260817); + int w, y; + do { + w = rnd() % (p - 1) + 1; + y = ((LL)w * w + p - x) % p; + } while(mod_pow(y, (p - 1) / 2) == 1); + auto mod_mul = [&p, &y](pair const &u, pair const &v) { + int fir = ((LL)u.first * v.first + (LL)u.second * v.second % p * y) % p; + int sec = ((LL)u.first * v.second + (LL)u.second * v.first) % p; + return make_pair(fir, sec); + }; + pair ret = make_pair(1, 0), tmp = make_pair(w, 1); + for(int k = (p + 1) / 2; k > 0; k >>= 1, tmp = mod_mul(tmp, tmp)) + if(k & 1) + ret = mod_mul(ret, tmp); + return ret.first; +} +const int maxn = (int)3.2e4 + 1, maxc = 9, maxs = 1 << 16 | 1; +int ptot, pr[maxn], d[maxn]; +vector > solve(int n) { + int cnt = 0, sz[2] = {}, cur = 0, pre = 1; + static pair pp[maxc], vec[2][maxs]; + int tmp = n; + for(int i = 0; pr[i] * pr[i] <= tmp; ++i) + if(tmp % pr[i] == 0) { + int ex = 0; + for(tmp /= pr[i], ++ex; tmp % pr[i] == 0; tmp /= pr[i], ++ex); + pp[cnt++] = make_pair(pr[i], ex << 1); + } + if(tmp > 1) + pp[cnt++] = make_pair(tmp, 2); + sz[cur] = 1; + vec[cur][0] = make_pair(0, 1); + for(int i = 0; i < cnt; ++i) { + pair tr; + if(pp[i].first == 2) { + tr = make_pair(1, 1); + } else if((pp[i].first & 3) == 3) { + tr = make_pair(0, pp[i].first); + } else { + int a = pp[i].first, b = mod_sqrt(pp[i].first - 1, pp[i].first); + for( ; (LL)b * b > pp[i].first; swap(a, b), b %= a); + int tmp = pp[i].first - b * b; + a = (int)sqrt(tmp); + for( ; a * a < tmp; ++a); + for( ; a * a > tmp; --a); + if(a > b) + swap(a, b); + tr = make_pair(a, b); + } + for(int rem = pp[i].second; rem > 0; rem -= (pp[i].first & 3) == 3 ? 2 : 1) { + swap(pre, cur); + sz[cur] = 0; + for(int j = 0; j < sz[pre]; ++j) { + int a = vec[pre][j].first, b = vec[pre][j].second; + { + int c = a * tr.first + b * tr.second; + int d = abs(a * tr.second - b * tr.first); + if(c > d) + swap(c, d); + vec[cur][sz[cur]++] = make_pair(c, d); + } + { + int c = abs(a * tr.first - b * tr.second); + int d = a * tr.second + b * tr.first; + if(c > d) + swap(c, d); + vec[cur][sz[cur]++] = make_pair(c, d); + } + } + assert(sz[cur] <= maxs); + sort(vec[cur], vec[cur] + sz[cur]); + sz[cur] = unique(vec[cur], vec[cur] + sz[cur]) - vec[cur]; + } + } + vector > ret; + ret.reserve(sz[cur] << 3); + for(int i = 0; i < sz[cur]; ++i) { + int a = vec[cur][i].first, b = vec[cur][i].second; + ret.push_back(make_pair(a, b)); + ret.push_back(make_pair(-a, b)); + ret.push_back(make_pair(a, -b)); + ret.push_back(make_pair(-a, -b)); + ret.push_back(make_pair(b, a)); + ret.push_back(make_pair(-b, a)); + ret.push_back(make_pair(b, -a)); + ret.push_back(make_pair(-b, -a)); + } + sort(ret.begin(), ret.end()); + ret.erase(unique(ret.begin(), ret.end()), ret.end()); + return ret; +} +int main() { + for(int i = 2; i < maxn; ++i) { + if(!d[i]) + pr[ptot++] = d[i] = i; + for(int j = 0, k; (k = i * pr[j]) < maxn; ++j) { + d[k] = pr[j]; + if(d[i] == pr[j]) + break; + } + } + pr[ptot] = maxn; + int t, a, b, c; + scanf("%d", &t); + while(t--) { + scanf("%d%d%d", &a, &b, &c); + vector > lft = solve(a), rht = solve(b); + vector, pair > > ans; + for(auto &it : lft) + for(auto &jt : rht) { + int dx = it.first - jt.first; + int dy = it.second - jt.second; + if((LL)dx * dx + (LL)dy * dy == (LL)c * c) + ans.push_back(make_pair(it, jt)); + } + printf("%d\n", (int)ans.size()); + for(auto &it : ans) { + auto &lft = it.first, &rht = it.second; + printf("%d %d %d %d\n", lft.first, lft.second, rht.first, rht.second); + } + } + return 0; +} \ No newline at end of file diff --git a/self_training/2019-09-15/L.cpp b/self_training/2019-09-15/L.cpp new file mode 100644 index 00000000..b6dbc0b2 --- /dev/null +++ b/self_training/2019-09-15/L.cpp @@ -0,0 +1,96 @@ +#include +using namespace std; +typedef unsigned long long ULL; +const int maxn = 151, maxd = 8, maxs = 1 << maxd | 1; +ULL table[maxs][maxs]; +ULL getPrd(ULL x, ULL y, int dep = 32) { + if(x > y) + swap(x, y); + if(x == 0) + return 0; + if(x == 1) + return y; + ULL mx = max(x, y); + assert(!((mx >> dep) >> dep)); + if(mx < 1ULL << maxd && table[x][y]) + return table[x][y]; + for( ; mx < 1ULL << dep; dep >>= 1); + ULL msk = (1ULL << dep) - 1; + ULL xL = x & msk, xH = x >> dep; + ULL yL = y & msk, yH = y >> dep; + ULL t0 = getPrd(xL, yL, dep >> 1); + ULL t1 = getPrd(xL ^ xH, yL ^ yH, dep >> 1); + ULL t2 = getPrd(getPrd(xH, yH, dep >> 1), 1ULL << (dep - 1), dep >> 1); + if(mx < 1ULL << maxd && !table[x][y]) + return table[x][y] = (t0 ^ t1) << dep | (t0 ^ t2); + return (t0 ^ t1) << dep | (t0 ^ t2); +} +ULL getInv(ULL x, int dep = 32) { // assert(x); + static const int BLEN = 64; + static bitset row[BLEN + 1]; + if(x == 1) + return 1ULL; + for( ; x < 1ULL << dep; dep >>= 1); + dep <<= 1; + for(int i = 0; i < dep; ++i) + row[i].reset(); + for(int i = 0; i < dep; ++i) { + ULL tp = getPrd(x, 1ULL << i); + for(int j = 0; j < dep; ++j, tp >>= 1) + if(tp & 1) + row[j].set(i); + } + row[0].set(dep); + for(int i = 0; i < dep; ++i) { + for(int j = i; j < dep; ++j) + if(row[j].test(i)) { + swap(row[i], row[j]); + break; + } + if(!row[i].test(i)) + continue; + for(int j = 0; j < dep; ++j) + if(i != j && row[j].test(i)) + row[j] ^= row[i]; + } + ULL ret = 0; + for(int i = dep - 1; i >= 0; --i) { + if(!row[i].test(i)) + assert(!row[i].test(dep)); + ret = ret << 1 | row[i].test(dep); + } + return ret; +} +int n; +ULL mat[maxn][maxn], ans; +int main() { + scanf("%d", &n); + for(int i = 0; i < n; ++i) + for(int j = 0; j < n; ++j) + scanf("%llu", mat[i] + j); + ans = 1; + for(int i = 0; i < n; ++i) { + for(int j = i; j < n; ++j) + if(mat[j][i]) { + for(int k = i; k < n; ++k) + swap(mat[i][k], mat[j][k]); + break; + } + if(!mat[i][i]) { + ans = 0; + break; + } + ULL ivs = getInv(mat[i][i]); + for(int j = i; j < n; ++j) + mat[i][j] = getPrd(mat[i][j], ivs); + for(int j = i + 1; j < n; ++j) { + if(!mat[j][i]) + continue; + ULL prd = mat[j][i]; + for(int k = i; k < n; ++k) + mat[j][k] ^= getPrd(mat[i][k], prd); + } + } + puts(ans ? "First" : "Second"); + return 0; +} \ No newline at end of file diff --git a/self_training/2019-09-20/E.cpp b/self_training/2019-09-20/E.cpp new file mode 100644 index 00000000..5c19f3f7 --- /dev/null +++ b/self_training/2019-09-20/E.cpp @@ -0,0 +1,59 @@ +#include +using namespace std; +const int maxn = (int)1e5 + 1, maxm = (int)3e4 + 1; +int n, a[maxn], ord[maxn], tot, in[maxn], out[maxn], bit[2][maxn]; +vector e[maxn]; +void dfs(int u, int fa) { + in[u] = ++tot; + for(int v : e[u]) { + if(v == fa) + continue; + dfs(v, u); + } + out[u] = tot; +} +void bit_upd(int bit[maxn], int x, int v) { + for( ; x <= n; x += x & -x) + bit[x] += v; +} +int bit_que(int bit[maxn], int x) { + int ret = 0; + for( ; x > 0; x -= x & -x) + ret += bit[x]; + return ret; +} +int main() { + scanf("%d", &n); + for(int i = 1; i <= n; ++i) { + scanf("%d", a + i); + ord[i] = i; + } + sort(ord + 1, ord + n + 1, [&](int const &u, int const &v) { + return a[u] < a[v]; + }); + for(int i = 1; i < n; ++i) { + int u, v; + scanf("%d%d", &u, &v); + e[u].push_back(v); + e[v].push_back(u); + } + dfs(1, -1); + bitset msk; + msk.set(0); + for(int i = 1; i <= n; ++i) { + int o = ord[i]; + int v0 = bit_que(bit[0], in[o]); + int v1 = bit_que(bit[1], out[o]) - bit_que(bit[1], in[o] - 1); + msk = msk << v0 | msk << v1; + bit_upd(bit[0], in[o] + 1, 1); + bit_upd(bit[0], out[o] + 1, -1); + bit_upd(bit[1], in[o], 1); + } + scanf("%d", &n); + while(n--) { + int k; + scanf("%d", &k); + puts(msk.test(k) ? "Orz" : "QAQ"); + } + return 0; +} \ No newline at end of file diff --git a/self_training/2019-10-20/1239A.cpp b/self_training/2019-10-20/1239A.cpp new file mode 100644 index 00000000..1b3f20ba --- /dev/null +++ b/self_training/2019-10-20/1239A.cpp @@ -0,0 +1,13 @@ +#include +using namespace std; +const int maxn = (int)1e5 + 9, mod = (int)1e9 + 7; +int n, m, fib[maxn]; +int main() { + scanf("%d%d", &n, &m); + fib[1] = 1; + for(int i = 1; i <= n || i <= m; ++i) + fib[i + 1] = (fib[i] + fib[i - 1]) % mod; + int ans = (fib[n + 1] + fib[m + 1] - 1LL) * 2 % mod; + printf("%d\n", ans < 0 ? ans + mod : ans); + return 0; +} \ No newline at end of file diff --git a/self_training/2019-10-23/D.cpp b/self_training/2019-10-23/D.cpp new file mode 100644 index 00000000..aba9d033 --- /dev/null +++ b/self_training/2019-10-23/D.cpp @@ -0,0 +1,60 @@ +#include +using namespace std; +typedef long long LL; +typedef LL Matrix[2][2]; +inline LL &modFixed(LL &x, LL p) { + x = x >= 0 && x < p ? x : x % p; + return x < 0 ? (x += p) : x; +} +inline LL modMulti(LL x, LL y, LL p, LL adt = 0) { + LL ret = modFixed(adt, p); + if(!modFixed(x, p) || !modFixed(y, p)) + return ret; + /* + const int BLEN = __builtin_clzll(p) - 1; + const LL BMSK = (1LL << BLEN) - 1; + if(x < y) + swap(x, y); + while(y > 0) { + modFixed(ret += x * (y & BMSK), p); + y >>= BLEN; + modFixed(x <<= BLEN, p); + } + // + ret = (ret + (__int128_t)x * y) % p; + */ + modFixed(ret += x * y - (LL)((long double)x * y / p + 0.5) * p, p); + return ret; +} +inline void matMulti(Matrix &ret, Matrix const &lft, Matrix const &rht, LL p) { + Matrix tmp = {}; + for(int i = 0; i < 2; ++i) + for(int j = 0; j < 2; ++j) + for(int k = 0; k < 2; ++k) + tmp[i][k] = modMulti(lft[i][j], rht[j][k], p, tmp[i][k]); + memcpy(ret, tmp, sizeof(Matrix)); +} +int main() { + int t; + scanf("%d", &t); + for(int Case = 1; Case <= t; ++Case) { + LL n, a, b, p; + scanf("%lld%lld%lld%lld", &n, &a, &b, &p); + if(p == 1) { + puts("0"); + continue; + } + Matrix ans = {}, tmp = {}; + ans[0][0] = ans[1][1] = tmp[0][1] = 1; + tmp[1][0] = modFixed(a, p); + tmp[0][0] = tmp[1][1] = modFixed(b, p); + while(n > 0) { + if(n & 1) + matMulti(ans, ans, tmp, p); + n >>= 1; + matMulti(tmp, tmp, tmp, p); + } + printf("%lld\n", ans[0][0]); + } + return 0; +} diff --git a/self_training/2019-10-25/E.cpp b/self_training/2019-10-25/E.cpp new file mode 100644 index 00000000..26a86dd7 --- /dev/null +++ b/self_training/2019-10-25/E.cpp @@ -0,0 +1,135 @@ +#include +using namespace std; +typedef long long LL; +const int maxn = (int)2e5 + 1; +int tot; +struct Node { + int fa, lch, rch; + int key, sz; + LL val, sum, tag; +} pool[maxn]; +inline int randL() { + return RAND_MAX == 0x7FFF ? rand() << 15 | rand() : rand(); +} +inline int newNode(int val) { + pool[tot].key = randL(); + pool[tot].sz = 1; + pool[tot].val = pool[tot].sum = val; + return tot++; +} +inline void upd(int rt) { + int lch = pool[rt].lch, rch = pool[rt].rch; + pool[rt].sz = pool[lch].sz + 1 + pool[rch].sz; + pool[rt].sum = pool[lch].sum + pool[rt].val + pool[rch].sum; +} +inline void push(int rt) { + LL &adt = pool[rt].tag; + if(!adt) + return; + int lch = pool[rt].lch, rch = pool[rt].rch; + pool[lch].val += adt; + pool[lch].tag += adt; + pool[lch].sum += adt * pool[lch].sz; + pool[rch].val += adt; + pool[rch].tag += adt; + pool[rch].sum += adt * pool[rch].sz; + adt = 0; +} +inline pair split(int rt, int sz) { + if(!rt) + return make_pair(0, 0); + push(rt); + int lch = pool[rt].lch, rch = pool[rt].rch; + if(pool[lch].sz + 1 > sz) { + pool[lch].fa = 0; + pair tr = split(lch, sz); + pool[rt].lch = lch = tr.second; + if(lch) + pool[lch].fa = rt; + upd(rt); + return make_pair(tr.first, rt); + } else { + pool[rch].fa = 0; + pair tr = split(rch, sz - pool[lch].sz - 1); + pool[rt].rch = rch = tr.first; + if(rch) + pool[rch].fa = rt; + upd(rt); + return make_pair(rt, tr.second); + } +} +inline int merge(int x, int y) { + if(!x || !y) + return x | y; + else if(pool[x].key < pool[y].key) { + push(x); + int rch = pool[x].rch; + pool[rch].fa = 0; + rch = merge(rch, y); + pool[x].rch = rch; + if(rch) + pool[rch].fa = x; + upd(x); + return x; + } else { + push(y); + int lch = pool[y].lch; + pool[lch].fa = 0; + lch = merge(x, lch); + pool[y].lch = lch; + if(lch) + pool[lch].fa = y; + upd(y); + return y; + } +} +void insertNode(int &rt, int pos, int val) { + int lft, rht; + tie(lft, rht) = split(rt, pos - 1); + lft = merge(lft, newNode(val)); + rt = merge(lft, rht); +} +void addValue(int &rt, int L, int R, int val) { + int lft, mid, rht; + tie(lft, rht) = split(rt, R); + tie(lft, mid) = split(lft, L - 1); + pool[mid].val += val; + pool[mid].tag += val; + pool[mid].sum += (LL)val * pool[mid].sz; + lft = merge(lft, mid); + rt = merge(lft, rht); +} +LL querySum(int &rt, int L, int R) { + int lft, mid, rht; + tie(lft, rht) = split(rt, R); + tie(lft, mid) = split(lft, L - 1); + LL ret = pool[mid].sum; + lft = merge(lft, mid); + rt = merge(lft, rht); + return ret; +} +int main() { + int n, rt = 0; + tot = 1; + scanf("%d", &n); + for(int i = 1; i <= n; ++i) { + int x; + scanf("%d", &x); + insertNode(rt, i, x); + } + scanf("%d", &n); + while(n--) { + int typ, l, r, v; + scanf("%d%d", &typ, &l); + if(typ == 1) { + insertNode(rt, l, 0); + } else if(typ == 2) { + scanf("%d%d", &r, &v); + addValue(rt, l, r, v); + } else { + scanf("%d", &r); + printf("%lld\n", querySum(rt, l, r)); + } + } + return 0; +} \ No newline at end of file diff --git a/self_training/2019-10-25/G.cpp b/self_training/2019-10-25/G.cpp new file mode 100644 index 00000000..39192098 --- /dev/null +++ b/self_training/2019-10-25/G.cpp @@ -0,0 +1,56 @@ +#include +using namespace std; +typedef long long LL; +const int maxn = (int)2e5 + 1; +int n, eve[maxn], m, que[maxn], mx, sm, bit[maxn]; +int main() { + scanf("%d", &n); + for(int i = 1; i <= n; ++i) { + int typ, &x = eve[i]; + scanf("%d%d", &typ, &x); + if(typ == 1) { + int y, z; + scanf("%d%d", &y, &z); + LL dis = (LL)x * x + (LL)y * y + (LL)z * z; + x = (int)sqrtl(dis); + for( ; x && (LL)x * x >= dis; --x); + for( ; (LL)x * x < dis; ++x); + //assert((LL)x * x >= dis); + que[++m] = x; + //assert(x >= 0); + } else { + x = -x - 1; + //assert(x < 0); + } + } + sort(que + 1, que + m + 1); + m = unique(que + 1, que + m + 1) - que - 1; + for(mx = 1; (mx << 1) <= m; mx <<= 1); + for(int i = 1; i <= n; ++i) { + int x = eve[i]; + if(x >= 0) { + int k = lower_bound(que + 1, que + m + 1, x) - que; + //assert(k <= m && que[k] == x); + for(++sm; k <= m; k += k & -k) + ++bit[k]; + } else { + int k = -x - 1; + //assert(k > 0); + if(k > sm) { + puts("-1"); + } else if(k == 0) { + puts("0"); + } else { + int x = 0; + for(int y = mx, z; y; y >>= 1) + if((z = x | y) <= m && bit[z] < k) { + x = z; + k -= bit[z]; + } + //assert(x < m); + printf("%d\n", que[x + 1]); + } + } + } + return 0; +} \ No newline at end of file diff --git a/self_training/2019-10-25/H.cpp b/self_training/2019-10-25/H.cpp new file mode 100644 index 00000000..792d4e1c --- /dev/null +++ b/self_training/2019-10-25/H.cpp @@ -0,0 +1,55 @@ +#include +using namespace std; +typedef long long LL; +const int maxn = (int)2e5 + 1; +int n, m, dsu[maxn], dsu2[maxn], st, tim[maxn]; +struct Edge { + int u, v, w; +} e[maxn]; +inline int dsuFind(int dsu[], int x) { + return dsu[x] < 0 ? x : (dsu[x] = dsuFind(dsu, dsu[x])); +} +inline bool dsuMerge(int dsu[], int u, int v) { + u = dsuFind(dsu, u); + v = dsuFind(dsu, v); + if(u == v) + return 0; + if(dsu[u] < dsu[v]) + swap(u, v); + dsu[v] -= dsu[u] == dsu[v]; + dsu[u] = v; + return 1; +} +int main() { + scanf("%d%d", &n, &m); + for(int i = 0; i < m; ++i) + scanf("%d%d%d", &e[i].u, &e[i].v, &e[i].w); + sort(e, e + m, [&](Edge const &lft, Edge const &rht) { + return lft.w < rht.w; + }); + LL ans = 0; + memset(dsu + 1, -1, n * sizeof(int)); + for(int i = 0, j; i < m; ) { + ++st; + auto enable = [&](int u) { + if(tim[u] != st) { + tim[u] = st; + dsu2[u] = -1; + } + }; + for(j = i; j < m && e[j].w == e[i].w; ++j) { + int u = dsuFind(dsu, e[j].u); + int v = dsuFind(dsu, e[j].v); + if(u == v) + continue; + enable(u); + enable(v); + if(!dsuMerge(dsu2, u, v)) + ans += e[j].w; + } + for( ; i < j; ++i) + dsuMerge(dsu, e[i].u, e[i].v); + } + printf("%lld\n", ans); + return 0; +} \ No newline at end of file diff --git a/self_training/2019-10-25/I.cpp b/self_training/2019-10-25/I.cpp new file mode 100644 index 00000000..6173642f --- /dev/null +++ b/self_training/2019-10-25/I.cpp @@ -0,0 +1,56 @@ +#include +using namespace std; +typedef long long LL; +const int maxn = (int)1e6 + 1, mod = (int)1e9 + 7; +int n, m, tot; +pair seg[maxn]; +inline void addSegment(int L, int R) { + if(tot && seg[tot - 1].second + 1 == L) { + seg[tot - 1].second = R; + } else { + seg[tot++] = make_pair(L, R); + } +} +inline int calc(int n) { + static bool enabled = 0; + static int f[maxn] = {}; + if(n < maxn) { + if(!enabled) { + int upp = min(::n + ::m + 1, maxn); + for(int i = 1; i < upp; ++i) + for(int j = i; j < upp; j += i) + (f[j] += i) >= mod && (f[j] -= mod); + for(int i = 1; i < upp; ++i) + (f[i] += f[i - 1]) >= mod && (f[i] -= mod); + enabled = 1; + } + return f[n]; + } else { + int ret = 0; + for(int i = 1, j; i <= n; i = j + 1) { + int p = n / i; + j = n / p; + ret = (ret + ((LL)i + j) * (j - i + 1) / 2 % mod * p) % mod; + } + return ret; + } +} +int main() { + scanf("%d%d", &n, &m); + if(n > m) + swap(n, m); + for(int i = 1, j; i <= m; i = j + 1) { + int p = n / i, q = m / i, r = (n + m) / (p + q + 1); + j = p ? min(n / p, m / q) : m / q; + if(i <= r) + addSegment(i, min(j, r)); + } + addSegment(m + 1, m + n); + int ans = 0; + for(int i = 0; i < tot; ++i) { + (ans += calc(seg[i].second)) >= mod && (ans -= mod); + (ans -= calc(seg[i].first - 1)) < 0 && (ans += mod); + } + printf("%d\n", ans); + return 0; +} \ No newline at end of file diff --git a/self_training/2019-10-25/J.cpp b/self_training/2019-10-25/J.cpp new file mode 100644 index 00000000..0ccfe59e --- /dev/null +++ b/self_training/2019-10-25/J.cpp @@ -0,0 +1,27 @@ +#include +using namespace std; +typedef long long LL; +const int maxn = 1 << 20 | 1; +int n; +LL f[maxn]; +int main() { + int t; + scanf("%d", &t); + n = 1; + while(t--) { + int x, v; + scanf("%d%d", &x, &v); + for( ; n <= x; n <<= 1); + f[x] += v; + } + for(int i = 1; i < n; i <<= 1) + for(int j = 0; j < n; ++j) + if(j & i) + f[j ^ i] += f[j]; + int pos = 0; + for(int i = 1; i <= n; ++i) + if(f[pos] < f[i]) + pos = i; + printf("%d %lld\n", pos, f[pos]); + return 0; +} \ No newline at end of file diff --git a/self_training/2020-04-19/A.cpp b/self_training/2020-04-19/A.cpp new file mode 100644 index 00000000..a3f76c7a --- /dev/null +++ b/self_training/2020-04-19/A.cpp @@ -0,0 +1,19 @@ +#include +using namespace std; +const int maxn = 101; +int main() { + int T; + scanf("%d", &T); + for(int Case = 1; Case <= T; ++Case) { + int N; + static int H[maxn]; + scanf("%d", &N); + for(int i = 1; i <= N; ++i) + scanf("%d", H + i); + int ans = 0; + for(int i = 2; i < N; ++i) + ans += H[i - 1] < H[i] && H[i] > H[i + 1]; + printf("Case #%d: %d\n", Case, ans); + } + return 0; +} \ No newline at end of file diff --git a/self_training/2020-04-19/B.cpp b/self_training/2020-04-19/B.cpp new file mode 100644 index 00000000..93228882 --- /dev/null +++ b/self_training/2020-04-19/B.cpp @@ -0,0 +1,21 @@ +#include +using namespace std; +typedef long long LL; +const int maxn = 1001; +int main() { + int T; + scanf("%d", &T); + for(int Case = 1; Case <= T; ++Case) { + int N; + LL D; + static LL X[maxn]; + scanf("%d%lld", &N, &D); + for(int i = 1; i <= N; ++i) + scanf("%lld", X + i); + LL ans = D; + for(int i = N; i >= 1; --i) + ans = ans / X[i] * X[i]; + printf("Case #%d: %lld\n", Case, ans); + } + return 0; +} \ No newline at end of file diff --git a/self_training/2020-04-19/C.cpp b/self_training/2020-04-19/C.cpp new file mode 100644 index 00000000..2c159321 --- /dev/null +++ b/self_training/2020-04-19/C.cpp @@ -0,0 +1,51 @@ +#include +using namespace std; +typedef long long LL; +#define x first +#define y second +const int maxn = (int)1e4 + 1, mod = (int)1e9; +inline void mod_inc(int &x, int y) { + (x += y) >= mod && (x -= mod); +} +pair solve(char pat[], int &pos) { + char ch = pat[pos++]; + if(ch >= '1' && ch <= '9') { + pair dlt = make_pair(0, 0), tmp; + assert(pat[pos++] == '('); + while(pat[pos] != ')') { + tmp = solve(pat, pos); + mod_inc(dlt.x, tmp.x); + mod_inc(dlt.y, tmp.y); + } + ++pos; + int mul = ch - '0'; + dlt.x = (LL)dlt.x * mul % mod; + dlt.y = (LL)dlt.y * mul % mod; + return dlt; + } else { + static const char *dir = "NSEW"; + static const pair sft[4] = { + {0, mod - 1}, {0, 1}, {1, 0}, {mod - 1, 0} + }; + for(int i = 0; i < 4; ++i) + if(ch == dir[i]) + return sft[i]; + assert(false); + } +} +int main() { + int T; + scanf("%d", &T); + for(int Case = 1; Case <= T; ++Case) { + static char X[maxn + 3]; + scanf("%s", X + 2); + int N = strlen(X + 2); + X[0] = '1'; + X[1] = '('; + X[N + 2] = ')'; + int pos = 0; + pair dlt = solve(X, pos); + printf("Case #%d: %d %d\n", Case, dlt.x + 1, dlt.y + 1); + } + return 0; +} diff --git a/self_training/2020-04-19/D.cpp b/self_training/2020-04-19/D.cpp new file mode 100644 index 00000000..b8cdba59 --- /dev/null +++ b/self_training/2020-04-19/D.cpp @@ -0,0 +1,60 @@ +#include +using namespace std; +typedef double DB; +const int maxn = (int)2e5 + 1; +int main() { + static DB lgF[maxn]; + lgF[0] = lgF[1] = 0; + for(int i = 2; i < maxn; ++i) + lgF[i] = lgF[i - 1] + log(i); + function lgC = [&](int n, int m) { + return m >= 0 && m <= n ? lgF[n] - lgF[m] - lgF[n - m] : -1; + }; + int T; + scanf("%d", &T); + for(int Case = 1; Case <= T; ++Case) { + int W, H, L, U, R, D; + scanf("%d%d%d%d%d%d", &W, &H, &L, &U, &R, &D); + --W; + --H; + --L; + --U; + --R; + --D; + map, DB> prob; + prob[make_pair(0, 0)] = 1; + function getProb = [&](int X, int Y) { + map, DB>::const_iterator it = prob.find(make_pair(X, Y)); + if(it != prob.end()) + return it -> second; + if(X > 0 && X < W && Y > 0 && Y < H) + return exp(lgC(X + Y, X) - lgF[2] * (X + Y)); + DB ret = 0; + if(X > 0) + ret += getProb(X - 1, Y) * (Y == H ? 1 : 0.5); + if(Y > 0) + ret += getProb(X, Y - 1) * (X == W ? 1 : 0.5); + return prob[make_pair(X, Y)] = ret; + }; + double ans = 1.0; + // top-left: (L, U) + { + double tmp = getProb(L, U); + ans -= tmp; + } + // top: (X, U) + if(U > 0) + for(int i = L + 1; i <= R; ++i) { + double tmp = getProb(i, U - 1) * (i == W ? 1 : 0.5); + ans -= tmp; + } + // left: (L, Y) + if(L > 0) + for(int i = U + 1; i <= D; ++i) { + double tmp = getProb(L - 1, i) * (i == H ? 1 : 0.5); + ans -= tmp; + } + printf("Case #%d: %.10f\n", Case, ans); + } + return 0; +} \ No newline at end of file diff --git a/self_training/2020-04-21-cf3/A.cpp b/self_training/2020-04-21-cf3/A.cpp new file mode 100644 index 00000000..d4010e9a --- /dev/null +++ b/self_training/2020-04-21-cf3/A.cpp @@ -0,0 +1,18 @@ +#include +using namespace std; +typedef long long LL; +typedef double DB; +const int maxn = (int)1e5 + 1, mod = (int)1e9 + 7; +int t, n; +int main() { + scanf("%d", &t); + while(t--) { + scanf("%d", &n); + for(int i = 2, j; (j = (1 << i) - 1) <= n; ++i) + if(n % j == 0) { + printf("%d\n", n / j); + break; + } + } + return 0; +} \ No newline at end of file diff --git a/self_training/2020-04-21-cf3/B.cpp b/self_training/2020-04-21-cf3/B.cpp new file mode 100644 index 00000000..b5a019b6 --- /dev/null +++ b/self_training/2020-04-21-cf3/B.cpp @@ -0,0 +1,26 @@ +#include +using namespace std; +typedef long long LL; +typedef double DB; +const int maxn = (int)1e5 + 1, maxv = (int)1e9; +int t, n; +int main() { + scanf("%d", &t); + while(t--) { + scanf("%d", &n); + if(n & 2) { + puts("NO"); + continue; + } + puts("YES"); + int m = n / 4, dt = maxv / 2; + for(int i = 0; i < m; ++i) { + printf("%d %d ", dt + i + i + 2, dt - i - i - 2); + } + for(int i = 0; i < m; ++i) { + printf("%d %d ", dt + i + i + 1, dt - i - i - 1); + } + puts(""); + } + return 0; +} \ No newline at end of file diff --git a/self_training/2020-04-21-cf3/C.cpp b/self_training/2020-04-21-cf3/C.cpp new file mode 100644 index 00000000..a64a9473 --- /dev/null +++ b/self_training/2020-04-21-cf3/C.cpp @@ -0,0 +1,29 @@ +#include +using namespace std; +typedef long long LL; +typedef double DB; +const int maxn = (int)2e5 + 1, mod = (int)1e9 + 7; +int t, n; +pair dp[2]; +inline void upd(pair &x, pair y) { + if(x.first < y.first || (x.first == y.first && x.second < y.second)) + x = y; +} +int main() { + scanf("%d", &t); + while(t--) { + scanf("%d", &n); + dp[0] = dp[1] = make_pair(0, 0); + for(int i = 0; i < n; ++i) { + int x; + scanf("%d", &x); + int o = x > 0; + if(dp[!o].first > 0) + upd(dp[o], make_pair(dp[!o].first + 1, dp[!o].second + x)); + upd(dp[o], make_pair(1, (LL)x)); + } + upd(dp[0], dp[1]); + printf("%lld\n", dp[0].second); + } + return 0; +} \ No newline at end of file diff --git a/self_training/2020-04-21-cf3/D.cpp b/self_training/2020-04-21-cf3/D.cpp new file mode 100644 index 00000000..018cfc6b --- /dev/null +++ b/self_training/2020-04-21-cf3/D.cpp @@ -0,0 +1,29 @@ +#include +using namespace std; +typedef long long LL; +typedef double DB; +const int maxn = (int)2e5 + 1, maxv = maxn << 1 | 1; +int t, n, m, a[maxn], c[maxv]; +int main() { + scanf("%d", &t); + while(t--) { + scanf("%d%d", &n, &m); + for(int i = 0; i < n; ++i) + scanf("%d", a + i); + memset(c, 0, (m + m + 1) * sizeof(int)); + for(int i = 0, j = n - 1; i < j; ++i, --j) { + int u = a[i], v = a[j]; + if(u > v) + swap(u, v); + ++c[u + 1]; + --c[v + m + 1]; + ++c[u + v]; + --c[u + v + 1]; + } + int mx = 0; + for(int i = 1; i <= m + m; ++i) + mx = max(mx, c[i] += c[i - 1]); + printf("%d\n", n - mx); + } + return 0; +} \ No newline at end of file diff --git a/self_training/2020-04-21-cf3/E.cpp b/self_training/2020-04-21-cf3/E.cpp new file mode 100644 index 00000000..2e312a91 --- /dev/null +++ b/self_training/2020-04-21-cf3/E.cpp @@ -0,0 +1,52 @@ +#include +using namespace std; +typedef long long LL; +const int maxn = (int)2e5 + 1; +const LL INF = ~0ULL >> 1; +int t, n, m, sta[3], dis[3][maxn]; +vector e[maxn]; +LL p[maxn]; +int main() { + scanf("%d", &t); + while(t--) { + scanf("%d%d", &n, &m); + for(int i = 0; i < 3; ++i) + scanf("%d", sta + i); + for(int i = 1; i <= m; ++i) + scanf("%lld", p + i); + sort(p + 1, p + m + 1); + for(int i = 0; i < m; ++i) { + p[i + 1] += p[i]; + int u, v; + scanf("%d%d", &u, &v); + e[u].push_back(v); + e[v].push_back(u); + } + for(int i = 0; i < 3; ++i) { + int *f = dis[i]; + static int que[maxn]; + memset(f + 1, -1, n * sizeof(int)); + f[sta[i]] = 0; + que[0] = sta[i]; + for(int j = 0, tot = 1; j < tot; ++j) { + int u = que[j]; + for(int &v : e[u]) + if(f[v] == -1) { + f[v] = f[u] + 1; + que[tot++] = v; + } + } + } + LL ans = INF; + for(int i = 1; i <= n; ++i) { + int com = dis[1][i], all = com + dis[0][i] + dis[2][i]; + LL tmp = ans; + if(all <= m) + ans = min(ans, p[com] + p[all]); + } + printf("%lld\n", ans); + for(int i = 1; i <= n; ++i) + vector().swap(e[i]); + } + return 0; +} \ No newline at end of file diff --git a/self_training/2020-04-21-cf3/F.cpp b/self_training/2020-04-21-cf3/F.cpp new file mode 100644 index 00000000..38f8683e --- /dev/null +++ b/self_training/2020-04-21-cf3/F.cpp @@ -0,0 +1,80 @@ +#include +using namespace std; +typedef long long LL; +typedef double DB; +const int maxn = 201; +int t, n, que[maxn][maxn], cnt[maxn], seq[maxn], las[maxn]; +bool vis[maxn][maxn]; +bool solve(int pos, int fir = -1) { + if(pos == 1) { + if(fir == -1) { + static bool rem[maxn]; + memset(rem + 1, 0, n * sizeof(bool)); + for(int i = 2; i <= n; ++i) + rem[seq[i]] = 1; + for(int i = 1; i <= n; ++i) + if(!rem[i]) { + fir = i; + break; + } + } + seq[1] = fir; + for(int i = 1; i < n; ++i) { + int dt = las[i] - que[i][0]; + for(int j = 1; j <= que[i][0]; ++j) + if(!vis[i][seq[dt + j]]) + return 0; + } + return 1; + } + int u = -1, v = -1; + for(int i = 1; i <= n; ++i) + if(cnt[i] == 1 && i != fir) { + assert(v == -1); + v = u; + u = i; + } + if(v != -1) { + if(solve(pos, u)) + return 1; + if(solve(pos, v)) + return 1; + return 0; + } + if(u == -1) + return 0; + seq[pos] = u; + for(int i = 1; i < n; ++i) + if(!las[i] && vis[i][u]) { + las[i] = pos; + for(int j = 1; j <= que[i][0]; ++j) + --cnt[que[i][j]]; + if(solve(pos - 1, fir)) + return 1; + for(int j = 1; j <= que[i][0]; ++j) + ++cnt[que[i][j]]; + las[i] = 0; + } + return 0; +} +int main() { + scanf("%d", &t); + while(t--) { + scanf("%d", &n); + memset(cnt + 1, 0, n * sizeof(int)); + for(int i = 1; i < n; ++i) { + scanf("%d", que[i] + 0); + memset(vis[i] + 1, 0, n * sizeof(bool)); + for(int j = 1; j <= que[i][0]; ++j) { + scanf("%d", que[i] + j); + ++cnt[que[i][j]]; + vis[i][que[i][j]] = 1; + } + } + memset(las + 1, 0, n * sizeof(int)); + assert(solve(n)); + for(int i = 1; i <= n; ++i) + printf("%d%c", seq[i], " \n"[i == n]); + } + return 0; +} diff --git a/self_training/2020-04-21-ks/A.cpp b/self_training/2020-04-21-ks/A.cpp new file mode 100644 index 00000000..658262ba --- /dev/null +++ b/self_training/2020-04-21-ks/A.cpp @@ -0,0 +1,22 @@ +#include +using namespace std; +const int maxn = (int)1e5 + 1; +int main() { + int T; + scanf("%d", &T); + for(int Case = 1; Case <= T; ++Case) { + int N, B; + static int A[maxn]; + scanf("%d%d", &N, &B); + for(int i = 0; i < N; ++i) + scanf("%d", A + i); + sort(A, A + N); + int ans = 0; + for(int i = 0; i < N; ++i) { + ans += A[i] <= B; + B -= A[i]; + } + printf("Case #%d: %d\n", Case, ans); + } + return 0; +} \ No newline at end of file diff --git a/self_training/2020-04-21-ks/B.cpp b/self_training/2020-04-21-ks/B.cpp new file mode 100644 index 00000000..eb2f603a --- /dev/null +++ b/self_training/2020-04-21-ks/B.cpp @@ -0,0 +1,26 @@ +#include +using namespace std; +const int maxn = 51, maxm = 1501; + +int main() { + int T; + scanf("%d", &T); + for(int Case = 1; Case <= T; ++Case) { + int N, K, P; + scanf("%d%d%d", &N, &K, &P); + static int a[maxn], dp[maxm]; + memset(dp, 0, (P + 1) * sizeof(int)); + for(int i = 0; i < N; ++i) { + a[0] = 0; + for(int j = 1; j <= K; ++j) { + scanf("%d", a + j); + a[j] += a[j - 1]; + } + for(int j = P; j >= 1; --j) + for(int k = min(j, K); k >= 1; --k) + dp[j] = max(dp[j], dp[j - k] + a[k]); + } + printf("Case #%d: %d\n", Case, dp[P]); + } + return 0; +} \ No newline at end of file diff --git a/self_training/2020-04-21-ks/C.cpp b/self_training/2020-04-21-ks/C.cpp new file mode 100644 index 00000000..10149e32 --- /dev/null +++ b/self_training/2020-04-21-ks/C.cpp @@ -0,0 +1,31 @@ +#include +using namespace std; +const int maxn = (int)1e5 + 1; +int main() { + int T; + scanf("%d", &T); + for(int Case = 1; Case <= T; ++Case) { + int N, K; + static int A[maxn]; + scanf("%d%d", &N, &K); + for(int i = 0; i < N; ++i) + scanf("%d", A + i); + int L = 1, R = 1; + for(int i = N - 1; i > 0; --i) { + A[i] -= A[i - 1]; + R = max(R, A[i]); + } + while(L < R) { + int M = (L + R) >> 1, cnt = 0; + for(int i = 1; i < N; ++i) + cnt += A[i] / M + (A[i] % M > 0) - 1; + if(cnt <= K) { + R = M; + } else { + L = M + 1; + } + } + printf("Case #%d: %d\n", Case, R); + } + return 0; +} \ No newline at end of file diff --git a/self_training/2020-04-21-ks/D.cpp b/self_training/2020-04-21-ks/D.cpp new file mode 100644 index 00000000..5dbf9329 --- /dev/null +++ b/self_training/2020-04-21-ks/D.cpp @@ -0,0 +1,53 @@ +#include +using namespace std; +const int maxn = (int)2e6 + 1, maxc = 26 + 1; +namespace Trie { + int tot, rt, ctr[maxn], nxt[maxn][maxc]; + int newNode() { + ctr[tot] = 0; + memset(nxt[tot], 0, sizeof nxt[0]); + return tot++; + } + void init() { + tot = 0; + rt = newNode(); + } + void parse() { + static char buf[maxn]; + scanf("%s", buf); + int pos = rt; + for(int i = 0; buf[i]; ++i) { + int ch = buf[i] - 'A'; + if(!nxt[pos][ch]) + nxt[pos][ch] = newNode(); + pos = nxt[pos][ch]; + } + ++ctr[pos]; + } + int dfs(int dep, int u, int K, int &ans) { + int cnt = ctr[u]; + for(int i = 0; i < maxc; ++i) + if(nxt[u][i]) + cnt += dfs(dep + 1, nxt[u][i], K, ans); + if(cnt >= K) { + ans += cnt / K * dep; + cnt %= K; + } + return cnt; + } +} +int main() { + int T; + scanf("%d", &T); + for(int Case = 1; Case <= T; ++Case) { + int N, K; + scanf("%d%d", &N, &K); + Trie::init(); + for(int i = 0; i < N; ++i) + Trie::parse(); + int ans = 0; + Trie::dfs(0, Trie::rt, K, ans); + printf("Case #%d: %d\n", Case, ans); + } + return 0; +} \ No newline at end of file diff --git a/self_training/2020-04-22-cf3/A.cpp b/self_training/2020-04-22-cf3/A.cpp new file mode 100644 index 00000000..85ee7ebd --- /dev/null +++ b/self_training/2020-04-22-cf3/A.cpp @@ -0,0 +1,14 @@ +#include +using namespace std; +typedef long long LL; +typedef double DB; +const int maxn = (int)1e5 + 1, mod = (int)1e9 + 7; +int t, n; +int main() { + scanf("%d", &t); + while(t--) { + scanf("%d", &n); + printf("%d\n", (n - 1) >> 1); + } + return 0; +} \ No newline at end of file diff --git a/self_training/2020-04-22-cf3/B.cpp b/self_training/2020-04-22-cf3/B.cpp new file mode 100644 index 00000000..a7ad50f0 --- /dev/null +++ b/self_training/2020-04-22-cf3/B.cpp @@ -0,0 +1,18 @@ +#include +using namespace std; +typedef long long LL; +typedef double DB; +const int maxn = (int)2e3 + 1, mod = (int)1e9 + 7; +int t, n, a, b; +char buf[maxn]; +int main() { + scanf("%d", &t); + while(t--) { + scanf("%d%d%d", &n, &a, &b); + for(int i = 0; i < n; ++i) + buf[i] = i < a ? (i < b ? 'a' + i : 'a') : buf[i - a]; + buf[n] = '\0'; + puts(buf); + } + return 0; +} \ No newline at end of file diff --git a/self_training/2020-04-22-cf3/C.cpp b/self_training/2020-04-22-cf3/C.cpp new file mode 100644 index 00000000..9484b9eb --- /dev/null +++ b/self_training/2020-04-22-cf3/C.cpp @@ -0,0 +1,29 @@ +#include +using namespace std; +typedef long long LL; +typedef double DB; +const int maxn = (int)2e5 + 1, mod = (int)1e9 + 7; +int t, n, m, c[maxn]; +int main() { + scanf("%d", &t); + while(t--) { + scanf("%d", &n); + m = 0; + memset(c + 1, 0, n * sizeof(int)); + for(int i = 0; i < n; ++i) { + int x; + scanf("%d", &x); + m += !c[x]; + ++c[x]; + } + int ans = 0; + for(int i = 1; i <= n; ++i) { + if(!c[i]) + continue; + ans = max(ans, min(c[i], m - 1)); + ans = max(ans, min(c[i] - 1, m)); + } + printf("%d\n", ans); + } + return 0; +} \ No newline at end of file diff --git a/self_training/2020-04-22-cf3/D.cpp b/self_training/2020-04-22-cf3/D.cpp new file mode 100644 index 00000000..9c6fd432 --- /dev/null +++ b/self_training/2020-04-22-cf3/D.cpp @@ -0,0 +1,22 @@ +#include +using namespace std; +typedef long long LL; +typedef double DB; +const int maxn = (int)2e5 + 1, mod = (int)1e9 + 7; +int t; +char buf[9][11]; +int main() { + scanf("%d", &t); + while(t--) { + for(int i = 0; i < 9; ++i) + scanf("%s", buf[i]); + for(int i = 0; i < 3; ++i) + for(int j = 0; j < 3; ++j) { + int x = i * 3 + j, y = j * 3 + i; + buf[x][y] = buf[x][y] == '1' ? '2' : '1'; + } + for(int i = 0; i < 9; ++i) + printf("%s\n", buf[i]); + } + return 0; +} \ No newline at end of file diff --git a/self_training/2020-04-22-cf3/E1.cpp b/self_training/2020-04-22-cf3/E1.cpp new file mode 100644 index 00000000..d83a3413 --- /dev/null +++ b/self_training/2020-04-22-cf3/E1.cpp @@ -0,0 +1,43 @@ +#include +using namespace std; +typedef long long LL; +typedef double DB; +const int maxn = (int)2e5 + 1, maxm = 201, mod = (int)1e9 + 7; +int t, n, m, que[maxm], tim[maxm]; +vector pos[maxn]; +int main() { + scanf("%d", &t); + for(int Case = 1; Case <= t; ++Case) { + scanf("%d", &n); + m = 0; + for(int i = 0; i < n; ++i) { + int x; + scanf("%d", &x); + if(tim[x] != Case) { + tim[x] = Case; + vector().swap(pos[x]); + que[m++] = x; + } + pos[x].push_back(i); + } + int ans = 0; + for(int i = 0; i < m; ++i) { + int u = que[i], uLen = (int)pos[u].size(); + ans = max(ans, uLen); + for(int j = 0; j < m; ++j) { + if(i == j) + continue; + int v = que[j], vLen = (int)pos[v].size(); + for(int uL = 0, uR = uLen - 1, vL = 0, vR = vLen - 1; uL < uR; ++uL, --uR) { + for( ; vL <= vR && pos[v][vL] < pos[u][uL]; ++vL); + for( ; vL <= vR && pos[u][uR] < pos[v][vR]; --vR); + if(vL > vR) + break; + ans = max(ans, vR - vL + 1 + (uL + 1) * 2); + } + } + } + printf("%d\n", ans); + } + return 0; +} \ No newline at end of file diff --git a/self_training/2020-04-22-cf3/E2.cpp b/self_training/2020-04-22-cf3/E2.cpp new file mode 100644 index 00000000..d83a3413 --- /dev/null +++ b/self_training/2020-04-22-cf3/E2.cpp @@ -0,0 +1,43 @@ +#include +using namespace std; +typedef long long LL; +typedef double DB; +const int maxn = (int)2e5 + 1, maxm = 201, mod = (int)1e9 + 7; +int t, n, m, que[maxm], tim[maxm]; +vector pos[maxn]; +int main() { + scanf("%d", &t); + for(int Case = 1; Case <= t; ++Case) { + scanf("%d", &n); + m = 0; + for(int i = 0; i < n; ++i) { + int x; + scanf("%d", &x); + if(tim[x] != Case) { + tim[x] = Case; + vector().swap(pos[x]); + que[m++] = x; + } + pos[x].push_back(i); + } + int ans = 0; + for(int i = 0; i < m; ++i) { + int u = que[i], uLen = (int)pos[u].size(); + ans = max(ans, uLen); + for(int j = 0; j < m; ++j) { + if(i == j) + continue; + int v = que[j], vLen = (int)pos[v].size(); + for(int uL = 0, uR = uLen - 1, vL = 0, vR = vLen - 1; uL < uR; ++uL, --uR) { + for( ; vL <= vR && pos[v][vL] < pos[u][uL]; ++vL); + for( ; vL <= vR && pos[u][uR] < pos[v][vR]; --vR); + if(vL > vR) + break; + ans = max(ans, vR - vL + 1 + (uL + 1) * 2); + } + } + } + printf("%d\n", ans); + } + return 0; +} \ No newline at end of file diff --git a/self_training/2020-04-22-cf3/F.cpp b/self_training/2020-04-22-cf3/F.cpp new file mode 100644 index 00000000..748b259c --- /dev/null +++ b/self_training/2020-04-22-cf3/F.cpp @@ -0,0 +1,65 @@ +#include +using namespace std; +typedef long long LL; +typedef double DB; +const int maxn = (int)1e6 + 1; +const char *pat = "URDL"; +const int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1}; +int t, n, r, c, nxt[maxn], pre[maxn], idx[maxn]; +char col[maxn], buf[maxn]; +bool vis[maxn], cir[maxn]; +int main() { + scanf("%d", &t); + while(t--) { + scanf("%d%d", &r, &c); + n = r * c; + for(int i = 0; i < r; ++i) + scanf("%s", col + i * c); + for(int i = 0, idx = 0; i < r; ++i) { + scanf("%s", buf); + for(int j = 0; j < c; ++j, ++idx) { + int o; + for(o = 0; pat[o] != buf[j]; ++o); + nxt[idx] = idx + dx[o] * c + dy[o]; + } + } + memset(vis, 0, n * sizeof(bool)); + memset(cir, 0, n * sizeof(bool)); + memset(idx, -1, n * sizeof(int)); + for(int i = 0; i < n; ++i) { + if(vis[i]) + continue; + int tot = 0; + static int que[maxn]; + int j; + for(j = i; !vis[j]; j = nxt[j]) { + vis[j] = 1; + que[tot++] = j; + } + if(idx[j] == -1) { + int k; + for(k = nxt[j]; idx[k] == -1; j = k, k = nxt[k]) { + cir[k] = 1; + idx[k] = k; + pre[k] = j; + } + } + for(j = tot - 1; j >= 0; --j) { + int k = que[j]; + if(idx[k] == -1) { + idx[k] = pre[idx[nxt[k]]]; + if(col[k] == '0') + col[idx[k]] = '0'; + } + } + } + int c0 = 0, c1 = 0; + for(int i = 0; i < n; ++i) + if(cir[i]) { + ++c0; + c1 += col[i] == '0'; + } + printf("%d %d\n", c0, c1); + } + return 0; +} \ No newline at end of file diff --git a/self_training/2020-04-23-cf1/2A.cpp b/self_training/2020-04-23-cf1/2A.cpp new file mode 100644 index 00000000..36abcfc0 --- /dev/null +++ b/self_training/2020-04-23-cf1/2A.cpp @@ -0,0 +1,18 @@ +#include +using namespace std; +typedef long long LL; +typedef double DB; +#define x first +#define y second +const int maxn = (int)1e5 + 1, mod = (int)1e9 + 7; + +int main() { + int t; + scanf("%d", &t); + while(t--) { + int n, a, b, c, d; + scanf("%d%d%d%d%d", &n, &a, &b, &c, &d); + puts(max((a - b) * n, c - d) <= min((a + b) * n, c + d) ? "Yes" : "No"); + } + return 0; +} \ No newline at end of file diff --git a/self_training/2020-04-23-cf1/2B.cpp b/self_training/2020-04-23-cf1/2B.cpp new file mode 100644 index 00000000..2e9423b6 --- /dev/null +++ b/self_training/2020-04-23-cf1/2B.cpp @@ -0,0 +1,33 @@ +#include +using namespace std; +typedef long long LL; +typedef double DB; +#define x first +#define y second +const int maxn = (int)2e5 + 1, mod = (int)1e9 + 7; +int n, m, a[maxn], b[maxn]; +int main() { + int t; + scanf("%d", &t); + while(t--) { + scanf("%d%d", &n, &m); + for(int i = 0; i < n; ++i) { + scanf("%d", a + i); + b[i] = 0; + } + for(int i = 1; i + 1 < n; ++i) + b[i] = a[i] > a[i - 1] && a[i] > a[i + 1]; + for(int i = 1; i < n; ++i) + b[i] += b[i - 1]; + int ans = -1, pos = -1; + for(int i = 0; i + m <= n; ++i) { + int tmp = b[i + m - 2] - b[i]; + if(ans < tmp) { + ans = tmp; + pos = i; + } + } + printf("%d %d\n", ans + 1, pos + 1); + } + return 0; +} \ No newline at end of file diff --git a/self_training/2020-04-23-cf1/A.cpp b/self_training/2020-04-23-cf1/A.cpp new file mode 100644 index 00000000..a5161d98 --- /dev/null +++ b/self_training/2020-04-23-cf1/A.cpp @@ -0,0 +1,46 @@ +#include +using namespace std; +typedef long long LL; +typedef double DB; +#define x first +#define y second +const int maxn = (int)1e5 + 3, mod = (int)1e9 + 7; +int n, p[maxn], pL[maxn], pR[maxn]; +int pos[maxn]; +int main() { + int t; + scanf("%d", &t); + while(t--) { + scanf("%d", &n); + for(int i = 1; i <= n; ++i) { + scanf("%d", p + i); + pL[i] = i - 1; + pR[i] = i + 1; + pos[p[i]] = i; + } + pR[0] = 1; + pL[n + 1] = n; + bool chk = 1; + multiset Hash; + for(int i = 1; i <= n; ++i) + Hash.insert(1); + for(int i = 1; i <= n; ++i) { + int u = pos[i], v = pL[u]; + if(u - v != *Hash.rbegin()) { + fprintf(stderr, "halted %d\n", i); + chk = 0; + break; + } + int w = pR[u]; + pR[v] = w; + pL[w] = v; + Hash.erase(Hash.find(u - v)); + if(w <= n) { + Hash.erase(Hash.find(w - u)); + Hash.insert(w - v); + } + } + puts(chk ? "Yes" : "No"); + } + return 0; +} \ No newline at end of file diff --git a/self_training/2020-04-23-cf1/B.cpp b/self_training/2020-04-23-cf1/B.cpp new file mode 100644 index 00000000..8f51966e --- /dev/null +++ b/self_training/2020-04-23-cf1/B.cpp @@ -0,0 +1,52 @@ +#include +using namespace std; +typedef long long LL; +typedef double DB; +#define x first +#define y second +const int maxn = (int)2e3 + 1, maxd = 10, mod = (int)1e9 + 7; +const char *pat[] = {"1110111", "0010010", "1011101", "1011011", "0111010", "1101011", "1101111", "1010010", "1111111", "1111011"}; +int n, m, c[maxn][maxd + 1]; +char buf[maxn]; +bitset dp[maxn]; +int main() { + int t = 1; + // scanf("%d", &t); + while(t--) { + scanf("%d%d", &n, &m); + for(int i = 0; i < n; ++i) { + scanf("%s", buf); + for(int j = 0; j < maxd; ++j) { + c[i][j] = 0; + for(int k = 0; buf[k]; ++k) + if(buf[k] > pat[j][k]) { + c[i][j] = m + 1; + break; + } else { + c[i][j] += buf[k] < pat[j][k]; + } + } + } + dp[n].reset(); + dp[n].set(0); + for(int i = n - 1; i >= 0; --i) { + dp[i].reset(); + for(int j = 0; j < maxd; ++j) + dp[i] |= dp[i + 1] << c[i][j]; + } + if(!dp[0].test(m)) { + puts("-1"); + continue; + } + for(int i = 0; i < n; ++i) + for(int j = maxd - 1; j >= 0; --j) + if(m >= c[i][j] && dp[i + 1].test(m - c[i][j])) { + buf[i] = '0' + j; + m -= c[i][j]; + break; + } + buf[n] = '\0'; + puts(buf); + } + return 0; +} \ No newline at end of file diff --git a/self_training/2020-04-23-cf1/C.cpp b/self_training/2020-04-23-cf1/C.cpp new file mode 100644 index 00000000..7d3b4af2 --- /dev/null +++ b/self_training/2020-04-23-cf1/C.cpp @@ -0,0 +1,68 @@ +#include +using namespace std; +const int maxm = (int)1e4 + 1, maxc = (int)1e3 + 1, maxs = (int)1e7 + 1, INF = 0x3f3f3f3f; +int n, m, c0, c1, a[maxm], dis[maxm], que0[maxm], que1[(maxc * maxc) << 1 | 1]; +bool vis[maxs]; +inline int idx(int u, int x) { + return u * c0 + x; +} +inline void rdx(int id, int &u, int &x) { + u = id / c0; + x = id % c0; +} +int main() { + int t = 1; + // scanf("%d", &t); + while(t--) { + scanf("%d%d", &n, &m); + for(int i = 0; i < m; ++i) + scanf("%d", a + i); + scanf("%d%d", &c0, &c1); + sort(a, a + m); + m = unique(a, a + m) - a; + int ans = INF; + memset(dis, 0x3f, m * sizeof(int)); + // memset(vis, 0, m * c0 * sizeof(bool)); + dis[0] = que0[0] = 0; + for(int i = 0, tot0 = 1; i < tot0; ++i) { + int u = que0[i], tmp; + if(a[m - 1] - a[u] <= c0) + ans = min(ans, dis[u] * (c0 + c1) + a[m - 1] - a[u]); + if(ans < INF) + continue; + vis[tmp = idx(u, 0)] = 1; + que1[0] = tmp; + for(int j = 0, tot1 = 1; j < tot1; ++j) { + tmp = que1[j]; + int v, x, w, y; + rdx(tmp, v, x); + if(v > 0 && (y = x + a[v] - a[v - 1]) <= c0) { + w = v - 1; + if(y == c0) { + if(dis[w] == INF) { + dis[w] = dis[u] + 1; + que0[tot0++] = w; + } + } else if(!vis[tmp = idx(w, y)]) { + vis[tmp] = 1; + que1[tot1++] = tmp; + } + } + if(v < m - 1 && (y = x + a[v + 1] - a[v]) <= c0) { + w = v + 1; + if(y == c0) { + if(dis[w] == INF) { + dis[w] = dis[u] + 1; + que0[tot0++] = w; + } + } else if(!vis[tmp = idx(w, y)]) { + vis[tmp] = 1; + que1[tot1++] = tmp; + } + } + } + } + printf("%d\n", ans < INF ? ans : -1); + } + return 0; +} \ No newline at end of file diff --git a/self_training/2020-04-23-cf1/D.cpp b/self_training/2020-04-23-cf1/D.cpp new file mode 100644 index 00000000..2ff17c53 --- /dev/null +++ b/self_training/2020-04-23-cf1/D.cpp @@ -0,0 +1,50 @@ +#include +using namespace std; +typedef long long LL; +typedef double DB; +#define x first +#define y second +const int maxn = (int)1e5 + 1, maxm = (int)1e6 + 1, mod = (int)1e9 + 7; +int n, m, mx; +vector e[maxn]; +pair seq[maxm]; +void dfs(int u, int p, int sta) { + int sz = (int)e[u].size() - (p > 0), tim = sta; + seq[m++] = make_pair(u, sta); + for(int v : e[u]) { + if(v == p) + continue; + if(tim == mx) { // p > 0 + tim = mx - sz - 1; + seq[m++] = make_pair(u, tim); + } + dfs(v, u, tim + 1); + seq[m++] = make_pair(u, ++tim); + } + if(p > 0 && tim != sta - 1) + seq[m++] = make_pair(u, sta - 1); +} +int main() { + int t = 1; + // scanf("%d", &t); + while(t--) { + scanf("%d", &n); + for(int i = 1; i < n; ++i) { + int u, v; + scanf("%d%d", &u, &v); + e[u].push_back(v); + e[v].push_back(u); + } + mx = 0; + for(int i = 1; i <= n; ++i) + mx = max(mx, (int)e[i].size()); + m = 0; + dfs(1, 0, 0); + printf("%d\n", m); + for(int i = 0; i < m; ++i) + printf("%d %d\n", seq[i].first, seq[i].second); + for(int i = 1; i <= n; ++i) + vector().swap(e[i]); + } + return 0; +} \ No newline at end of file diff --git a/self_training/2020-04-23-cf1/E.cpp b/self_training/2020-04-23-cf1/E.cpp new file mode 100644 index 00000000..82382ab4 --- /dev/null +++ b/self_training/2020-04-23-cf1/E.cpp @@ -0,0 +1,85 @@ +#include +using namespace std; +const int maxn = (int)5e3 + 1; +int n, p[5], e[maxn][5], dis[maxn][maxn]; +inline bool chk() { + return p[0] == p[3] || p[1] == p[3] || p[2] == p[3]; +} +inline bool upd() { + printf("%d %d %d\n", p[0], p[1], p[2]); + fflush(stdout); + if(chk()) + return 1; + scanf("%d", p + 3); + return chk(); +} +int main() { + int m; + scanf("%d%d", &n, &m); + while(m--) { + int u, v; + scanf("%d%d", &u, &v); + e[u][++e[u][0]] = v; + e[v][++e[v][0]] = u; + } + for(int i = 1; i <= n; ++i) { + int ° = e[i][0]; + sort(e[i] + 1, e[i] + deg + 1); + deg = unique(e[i] + 1, e[i] + deg + 1) - (e[i] + 1); + } + for(int i = 1; i <= n; ++i) { + int *f = dis[i]; + static int que[maxn]; + f[i] = 1; + que[1] = i; + for(int j = 1, tot = 1; j <= tot; ++j) { + int u = que[j]; + for(int k = 1; k <= e[u][0]; ++k) { + int v = e[u][k]; + if(!f[v]) { + f[v] = f[u] + 1; + que[++tot] = v; + } + } + } + } + for(int i = 0; i < 3; ++i) + p[i] = i + 1; + if(upd()) + return 0; + for(int t = 1; t <= n; ++t) { + int &rt = p[3]; + for(int i = 0; i < 3; ++i) + if(dis[rt][p[i]] == 2) { + p[i] = rt; + upd(); + return 0; + } + int low = maxn << 3, q[3]; + for(int i = 1; i <= e[rt][0]; ++i) + for(int j = 1; j <= e[rt][0]; ++j) + for(int k = 1; k <= e[rt][0]; ++k) { + int t[3] = {e[rt][i], e[rt][j], e[rt][k]}; + int tmp = dis[p[0]][t[0]] + dis[p[1]][t[1]] + dis[p[2]][t[2]] + ((i == j) + (j == k) + (i == k)) * maxn; + if(tmp < low) { + low = tmp; + q[0] = t[0]; + q[1] = t[1]; + q[2] = t[2]; + } + } + for(int i = 0; i < 3; ++i) { + int &u = p[i], v = q[i]; + for(int j = 1; j <= e[u][0]; ++j) { + int w = e[u][j]; + if(dis[v][u] == dis[v][w] + 1) { + u = w; + break; + } + } + } + if(upd()) + return 0; + } + return 0; +} \ No newline at end of file diff --git a/self_training/2020-04-23-cf1/F.cpp b/self_training/2020-04-23-cf1/F.cpp new file mode 100644 index 00000000..78b16f6a --- /dev/null +++ b/self_training/2020-04-23-cf1/F.cpp @@ -0,0 +1,200 @@ +#include +using namespace std; +typedef long long LL; +const int maxn = (int)1e5 + 1, maxd = 18 * 3 + 1; +const int mod0 = (int)1e9 + 7, seed0 = (int)1e5 + 3; // seed > maxn && seed is a primitive root and prime +const int mod1 = (int)1e9 + 403, seed1 = (int)1e5 + 19; +inline int mod_add(int x, int y, int mod) { + return (x += y) < mod ? x : (x - mod); +} +inline int mod_sub(int x, int y, int mod) { + return (x -= y) < 0 ? (x + mod) : x; +} +inline int mod_mul(int x, int y, int mod) { + return (LL)x * y % mod; +} +inline int mod_inv(int x, int mod) { + int y = mod, u = 1, v = 0; + while(x > 0) { + int q = y / x; + tie(x, y) = make_pair(y - q * x, x); + tie(u, v) = make_pair(v - q * u, u); + } + assert(y == 1); + return v < 0 ? (v + mod) : v; +} +int pw0[maxn], pw1[maxn], ivs0[maxn], ivs1[maxn]; +struct Hash { + int len, h0, h1; + bool operator == (Hash const &t) const { + return len == t.len && h0 == t.h0 && h1 == t.h1; + } + Hash operator + (Hash const &t) const { + return (Hash){ + len + t.len, + mod_add(mod_mul(h0, pw0[t.len], mod0), t.h0, mod0), + mod_add(mod_mul(h1, pw1[t.len], mod1), t.h1, mod1) + }; + } + Hash operator - (Hash const &t) const { + assert(len >= t.len); + return (Hash){ + len - t.len, + mod_mul(mod_sub(h0, t.h0, mod0), ivs0[t.len], mod0), + mod_mul(mod_sub(h1, t.h1, mod1), ivs1[t.len], mod1) + }; + } +} O = {}; +void init_hash(int len) { + auto prepare = [&](int arr[], int val, int mod) { + arr[0] = 1; + for(int i = 1; i <= len; ++i) + arr[i] = mod_mul(arr[i - 1], val, mod); + }; + prepare(pw0, seed0, mod0); + prepare(pw1, seed1, mod1); + prepare(ivs0, mod_inv(seed0, mod0), mod0); + prepare(ivs1, mod_inv(seed1, mod1), mod1); +} +struct Segment { + bool bad; + Hash hL, hR; + void assign(int v) { + assert(v != 0); + bad = 0; + hL = v > 0 ? O : (Hash){1, -v, -v}; + hR = v < 0 ? O : (Hash){1, v, v}; + } +} seg[maxn << 1 | 1]; +inline int seg_idx(int L, int R) { + return (L + R) | (L < R); +} +Hash seg_left_hash(int L, int R, int len) { + assert(len > 0); + int rt = seg_idx(L, R); + if(seg[rt].hL.len == len) + return seg[rt].hL; + int M = (L + R) >> 1, lch = seg_idx(L, M), rch = seg_idx(M + 1, R); + int lenL = seg[lch].hL.len; + if(len <= lenL) + return seg_left_hash(L, M, len); + return (seg_left_hash(M + 1, R, len - lenL + seg[lch].hR.len) - seg[lch].hR) + seg[lch].hL; +} +Hash seg_right_hash(int L, int R, int len) { + assert(len > 0); + int rt = seg_idx(L, R); + if(seg[rt].hR.len == len) + return seg[rt].hR; + int M = (L + R) >> 1, lch = seg_idx(L, M), rch = seg_idx(M + 1, R); + int lenR = seg[rch].hR.len; + if(len <= lenR) + return seg_right_hash(M + 1, R, len); + return (seg_right_hash(L, M, len - lenR + seg[rch].hL.len) - seg[rch].hL) + seg[rch].hR; +} +inline void seg_merge(int L, int R) { + int rt = seg_idx(L, R); + int M = (L + R) >> 1, lch = seg_idx(L, M), rch = seg_idx(M + 1, R); + seg[rt].bad = 1; + if(seg[lch].bad || seg[rch].bad) + return; + int lenL = seg[lch].hR.len, lenR = seg[rch].hL.len; + int com = min(lenL, lenR); + if(com > 0 && !(seg_right_hash(L, M, com) == seg_left_hash(M + 1, R, com))) + return; + seg[rt].bad = 0; + seg[rt].hL = com < lenR ? (seg[rch].hL - seg[lch].hR) + seg[lch].hL : seg[lch].hL; + seg[rt].hR = com < lenL ? (seg[lch].hR - seg[rch].hL) + seg[rch].hR : seg[rch].hR; +} +void seg_build(int L, int R) { + if(L < R) { + int M = (L + R) >> 1; + seg_build(L, M); + seg_build(M + 1, R); + seg_merge(L, R); + } else { + static int v; + scanf("%d", &v); + seg[seg_idx(L, R)].assign(v); + } +} +void seg_upd(int L, int R, int x, int v) { + if(L < R) { + int M = (L + R) >> 1; + if(x <= M) { + seg_upd(L, M, x, v); + } else { + seg_upd(M + 1, R, x, v); + } + seg_merge(L, R); + } else { + seg[seg_idx(L, R)].assign(v); + } +} +int tot; +pair que[maxd]; +void seg_que(int L, int R, int l, int r) { + if(l <= L && R <= r) { + que[tot++] = make_pair(L, R); + } else { + int M = (L + R) >> 1; + if(l <= M) + seg_que(L, M, l, r); + if(r > M) + seg_que(M + 1, R, l, r); + } +} +int main() { + int n, m; + scanf("%d%*d", &n); + init_hash(n); + seg_build(1, n); + scanf("%d", &m); + while(m--) { + int typ, x, y; + scanf("%d%d%d", &typ, &x, &y); + if(typ == 1) { + seg_upd(1, n, x, y); + } else { + tot = 0; + seg_que(1, n, x, y); + bool chk = 1; + int rem = 0; + static pair stk[maxd]; + while(tot--) { + int uL, uR; + tie(uL, uR) = que[tot]; + int u = seg_idx(uL, uR); + if(seg[u].bad) { + chk = 0; + break; + } + Hash okU = O; + while(okU.len < seg[u].hR.len && rem > 0) { + int id; + Hash okV; + tie(id, okV) = stk[--rem]; + int vL, vR; + tie(vL, vR) = que[id]; + int v = seg_idx(vL, vR); + int com = min(seg[u].hR.len - okU.len, seg[v].hL.len - okV.len); + Hash nxtU = seg_right_hash(uL, uR, len + okU.len); + Hash nxtV = seg_left_hash(vL, vR, len + okV.len); + if((nxtU - okU) != (nxtV - okV)) + break; + okU = nxtU; + if(nxtV.len < seg[v].hL.len) + stk[rem++] = make_pair(id, nxtV); + } + if(okU.len < seg[u].hR.len) { + chk = 0; + break; + } + if(seg[u].hL.len > 0) + stk[rem++] = make_pair(tot, O); + } + chk &= !rem; + puts(chk ? "Yes" : "No"); + } + } + return 0; +} \ No newline at end of file diff --git a/self_training/2020-04-23-cfe/A.cpp b/self_training/2020-04-23-cfe/A.cpp new file mode 100644 index 00000000..2b89de1e --- /dev/null +++ b/self_training/2020-04-23-cfe/A.cpp @@ -0,0 +1,22 @@ +#include +using namespace std; +typedef long long LL; +typedef double DB; +const int maxn = 101, mod = (int)1e9 + 7; +int t, n, p[maxn], c[maxn]; +int main() { + scanf("%d", &t); + while(t--) { + scanf("%d", &n); + for(int i = 0; i < n; ++i) + scanf("%d%d", p + i, c + i); + bool chk = p[0] >= c[0]; + for(int i = 1; chk && i < n; ++i) { + int dp = p[i] - p[i - 1]; + int dc = c[i] - c[i - 1]; + chk &= dp >= dc && dc >= 0; + } + puts(chk ? "YES" : "NO"); + } + return 0; +} \ No newline at end of file diff --git a/self_training/2020-04-23-cfe/B.cpp b/self_training/2020-04-23-cfe/B.cpp new file mode 100644 index 00000000..23a05e63 --- /dev/null +++ b/self_training/2020-04-23-cfe/B.cpp @@ -0,0 +1,20 @@ +#include +using namespace std; +typedef long long LL; +typedef double DB; +const int maxn = (int)1e5 + 1, mod = (int)1e9 + 7; +int t, n, m, a[maxn]; +int main() { + scanf("%d", &t); + while(t--) { + scanf("%d%d", &n, &m); + for(int i = 0; i < n; ++i) + scanf("%d", a + i); + sort(a, a + n, greater()); + int pos = 0; + LL sum = 0; + for( ; pos < n && (sum + a[pos]) >= m * (pos + 1LL); sum += a[pos++]); + printf("%d\n", pos); + } + return 0; +} \ No newline at end of file diff --git a/self_training/2020-04-23-cfe/C.cpp b/self_training/2020-04-23-cfe/C.cpp new file mode 100644 index 00000000..0e30fbf5 --- /dev/null +++ b/self_training/2020-04-23-cfe/C.cpp @@ -0,0 +1,26 @@ +#include +using namespace std; +typedef long long LL; +typedef double DB; +const int maxn = (int)3e5 + 1, mod = (int)1e9 + 7; +const LL INF = (LL)1e13; +int t, n; +LL a[maxn], b[maxn]; +int main() { + scanf("%d", &t); + while(t--) { + scanf("%d", &n); + LL sum = 0, low = INF; + for(int i = 0; i < n; ++i) { + scanf("%lld%lld", a + i, b + i); + if(i > 0) { + sum += max(a[i] - b[i - 1], 0LL); + low = min(low, min(a[i], b[i - 1])); + } + } + sum += max(a[0] - b[n - 1], 0LL); + low = min(low, min(a[0], b[n - 1])); + printf("%lld\n", sum + low); + } + return 0; +} \ No newline at end of file diff --git a/self_training/2020-04-23-cfe/D.cpp b/self_training/2020-04-23-cfe/D.cpp new file mode 100644 index 00000000..14eb1c82 --- /dev/null +++ b/self_training/2020-04-23-cfe/D.cpp @@ -0,0 +1,30 @@ +#include +using namespace std; +typedef long long LL; +typedef double DB; +const int maxn = (int)1e5 + 1; +int t, n, tot, seq[maxn]; +LL L, R; +int main() { + scanf("%d", &t); + while(t--) { + scanf("%d%lld%lld", &n, &L, &R); + tot = 0; + LL pre = 0, cur; + for(int i = 1; i < n; ++i) { + cur = pre + (n - i) * 2; // (pre, cur] + if(max(pre + 1, L) <= min(cur, R)) + for(int j = 1; j <= (n - i) * 2; ++j) + if(pre + j >= L && pre + j <= R) + seq[tot++] = j & 1 ? i : i + (j >> 1); + pre = cur; + if(pre >= R) + break; + } + if(pre < R) + seq[tot++] = 1; + for(int i = 0; i < tot; ++i) + printf("%d%c", seq[i], " \n"[i == tot - 1]); + } + return 0; +} \ No newline at end of file diff --git a/self_training/2020-04-23-cfe/E.cpp b/self_training/2020-04-23-cfe/E.cpp new file mode 100644 index 00000000..7d17fb24 --- /dev/null +++ b/self_training/2020-04-23-cfe/E.cpp @@ -0,0 +1,132 @@ +#include +using namespace std; +typedef long long LL; +inline LL mod_add(LL x, LL y, LL mod) { + return (x += y) < mod ? x : x - mod; +} +inline LL mod_mul(LL x, LL y, LL mod) { + typedef long double LD; + LL ret = x * y - (LL)((LD)x * y / mod + 0.5) * mod; + return ret < 0 ? ret + mod : ret; +} +inline LL mod_pow(LL x, LL k, LL mod) { + LL ret = mod > 1 ? 1 : 0; + for( ; k > 0; k >>= 1, x = mod_mul(x, x, mod)) + if(k & 1) + ret = mod_mul(ret, x, mod); + return ret; +} +inline LL int_pow(LL x, int k, LL upp) { + LL ret = 1, lim = (++upp) / x; + for( ; k > 0; --k) { + if(ret > lim) + return upp; + ret *= x; + } + return ret; +} +inline bool miller_rabin(LL n) { + if(n == 2) + return 1; + if(n < 2 || !(n & 1)) + return 0; + LL s, r; + for(s = 0, r = n - 1; !(r & 1); r >>= 1, ++s); + static const int ptot = 12, pr[ptot] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37}; + for(int p : pr) { + if(p >= n) + break; + LL cur = 1, nxt = p; + for(LL k = r; k > 0; k >>= 1, nxt = mod_mul(nxt, nxt, n)) + if(k & 1) + cur = mod_mul(cur, nxt, n); + for(int i = 0; i < s; ++i) { + nxt = mod_mul(cur, cur, n); + if(nxt == 1 && cur != 1 && cur != n - 1) + return 0; + cur = nxt; + } + if(cur != 1) + return 0; + } + return 1; +} +inline LL pollard_rho(LL n) { + static mt19937_64 rnd(996); + while(1) { + LL x = rnd() % (n - 3) + 2, y = x, c = rnd() % n, tim = 0, prd = 1; + for(LL i = 1, stp = 2; ; ++i) { + if(i == stp) { + y = x; + stp <<= 1; + } + if((x = mod_add(mod_mul(x, x, n), c, n)) == y) + break; + LL tmp = prd; + if((prd = mod_mul(prd, abs(y - x), n)) == 0) + return __gcd(tmp, n); + static const int maxt = 100; + if((++tim) < maxt) + continue; + if((prd = __gcd(prd, n)) > 1) + return prd; + tim = 0; + } + if(tim > 0 && (prd = __gcd(prd, n)) > 1) + return prd; + } + assert(0); +} +inline void getPrimeFactors(LL n, int &tot, LL pr[]) { + tot = 0; + queue que; + que.push(n); + while(!que.empty()) { + LL x = que.front(); + que.pop(); + for(int i = 0; i < tot; ++i) + for(LL y = pr[i]; x % y == 0; x /= y); + if(x == 1) + continue; + if(miller_rabin(x)) { + pr[tot++] = x; + } else { + LL y = pollard_rho(x); + que.push(y); + que.push(x / y); + } + } + sort(pr, pr + tot); +} +const int maxn = (int)1e5 + 1, maxp = 15, maxe = 51, mod = 998244353; +int t, tot, a[maxp], b[maxp], fct[maxe], ifct[maxe]; +LL n, pr[maxe]; +int main() { + ifct[1] = 1; + for(int i = 2; i < maxe; ++i) + ifct[i] = mod - (int)(mod / i * (LL)ifct[mod % i] % mod); + fct[0] = ifct[0] = 1; + for(int i = 1; i < maxe; ++i) { + fct[i] = (LL)fct[i - 1] * i % mod; + ifct[i] = (LL)ifct[i - 1] * ifct[i] % mod; + } + scanf("%lld%d", &n, &t); + getPrimeFactors(n, tot, pr); + while(t--) { + LL u, v; + scanf("%lld%lld", &u, &v); + int c0 = 0, c1 = 0, ans = 1; + for(int i = 0; i < tot; ++i) { + LL p = pr[i]; + for(a[i] = 0; u % p == 0; u /= p, ++a[i]); + for(b[i] = 0; v % p == 0; v /= p, ++b[i]); + int com = min(a[i], b[i]); + c0 += (a[i] -= com); + c1 += (b[i] -= com); + ans = (LL)ans * ifct[a[i]] % mod * ifct[b[i]] % mod; + } + ans = (LL)ans * fct[c0] % mod * fct[c1] % mod; + printf("%d\n", ans); + } + return 0; +} \ No newline at end of file diff --git a/self_training/2020-04-23-cfe/F.cpp b/self_training/2020-04-23-cfe/F.cpp new file mode 100644 index 00000000..5ad9c44d --- /dev/null +++ b/self_training/2020-04-23-cfe/F.cpp @@ -0,0 +1,56 @@ +#include +using namespace std; +typedef long long LL; +typedef double DB; +const int maxn = (int)5e5 + 3; +const LL INF = 0x3f3f3f3f3f3f3f3fLL; +int n, a[maxn], p[maxn], m, b[maxn], pos[maxn]; +LL bit[maxn], f[maxn], g[maxn]; +int main() { + scanf("%d", &n); + for(int i = 1; i <= n; ++i) + scanf("%d", a + i); + for(int i = 1; i <= n; ++i) + scanf("%d", p + i); + scanf("%d", &m); + for(int i = 1; i <= m; ++i) { + scanf("%d", b + i); + pos[b[i]] = i; + g[i] = INF; + } + b[0] = 0; + g[0] = INF; + for(int i = 1; i <= n; ++i) { + int j = pos[a[i]]; + f[i] = 0; + if(j > 0) { + int L = b[j - 1], R = b[j]; // (L, R] + for(int x = L; x > 0; x -= x & -x) + f[i] -= bit[x]; + for(int x = R; x > 0; x -= x & -x) + f[i] += bit[x]; + } + if(p[i] > 0) + for(int x = a[i]; x <= n; x += x & -x) + bit[x] += p[i]; + } + g[m] = 0; + LL adt = 0, neg = 0; + for(int i = n; i >= 1; --i) { + if(p[i] < 0) + neg += p[i]; + else if(a[i] > b[m]) + adt += p[i]; + int j = pos[a[i]]; + if(!pos[a[i]]) + continue; + if(p[i] < 0) + f[i] -= p[i]; + g[j - 1] = min(g[j - 1], g[j] + f[i]); + } + if(g[0] < INF) + printf("YES\n%lld\n", g[0] + adt + neg); + else + puts("NO"); + return 0; +} \ No newline at end of file diff --git a/self_training/2020-04-23-cfe/G.cpp b/self_training/2020-04-23-cfe/G.cpp new file mode 100644 index 00000000..397a7ee6 --- /dev/null +++ b/self_training/2020-04-23-cfe/G.cpp @@ -0,0 +1,77 @@ +#include +using namespace std; +typedef double DB; +const int maxn = (int)2e5 + 1, maxc = 26, maxLen = 18, maxs = 1 << maxLen | 1; +const DB pi = acos(-1.0); // double +struct Complex { + DB r, i; + Complex() { r = i = 0; } + Complex(DB r, DB i) : r(r), i(i) {} + Complex operator + (Complex const &t) const { return Complex(r + t.r, i + t.i); } + Complex operator - (Complex const &t) const { return Complex(r - t.r, i - t.i); } + Complex operator * (Complex const &t) const { return Complex(r * t.r - i * t.i, r * t.i + i * t.r); } +} w[maxs], A[maxs], B[maxs]; +void FFT_init(int len) { + register int i, j, k; + for(i = 1; i < len; i <<= 1) + for(j = 0, k = i; j < i; ++j, ++k) + w[k] = k & 1 ? Complex(cos(pi / i * j), sin(pi / i * j)) : w[k >> 1]; +} +void FFT(int n, Complex x[], int flag) { + register int i, j, k; + for(i = 1, j = 0; i < n; ++i) { + for(k = n >> 1; j >= k; j ^= k, k >>= 1); + j < k && (j ^= k); + if(i < j) std::swap(x[i], x[j]); + } + static Complex *W, t; + for(i = 1; i < n; i <<= 1) + for(j = 0, W = w + i; j < n; j += i << 1) + for(k = 0; k < i; ++k) { + t = W[k] * x[i + j + k]; + x[i + j + k] = x[j + k] - t; + x[j + k] = x[j + k] + t; + } + if(flag != -1) return; + std::reverse(x + 1, x + n); + for(i = 0; i < n; ++i) + x[i].r /= n; // x[i].i /= n; +} +int n, m, perm[maxc + 1]; +char buf[maxn], pat[maxn]; +bool ban[maxn]; +int main() { + for(int i = 0; i < maxc; ++i) { + scanf("%d", perm + i); + --perm[i]; + } + scanf("%s%s", pat, buf); + n = strlen(buf); + m = strlen(pat); + int len; + for(len = 1; len < n; len <<= 1); + FFT_init(len); + for(int i = 0; i < maxc; ++i) { + int cnt = 0; + char ch0 = 'a' + i, ch1 = 'a' + perm[i]; + for(int j = 0; j < len; ++j) { + A[j] = Complex(j < n && (buf[j] == ch0 || buf[j] == ch1) ? 1 : 0, 0); + B[j ? len - j : j] = Complex(j < m && pat[j] == ch0 ? 1 : 0, 0); + cnt += j < m && pat[j] == ch0; + } + if(!cnt) + continue; + FFT(len, A, 1); + FFT(len, B, 1); + for(int j = 0; j < len; ++j) + A[j] = A[j] * B[j]; + FFT(len, A, -1); + for(int j = 0; j <= n - m; ++j) + ban[j] |= (int)(A[j].r + 0.5) < cnt; + } + for(int i = 0; i <= n - m; ++i) + buf[i] = "10"[ban[i]]; + buf[n - m + 1] = '\0'; + puts(buf); + return 0; +} \ No newline at end of file diff --git a/self_training/2020-04-25-v/A.cpp b/self_training/2020-04-25-v/A.cpp new file mode 100644 index 00000000..591f6531 --- /dev/null +++ b/self_training/2020-04-25-v/A.cpp @@ -0,0 +1,27 @@ +#include +using namespace std; +typedef long long LL; +typedef double DB; +#define x first +#define y second +const int maxn = (int)3e5 + 1, mod = (int)1e9 + 7; +int n, a[maxn]; +int main() { + int t = 1; + // scanf("%d", &t); + while(t--) { + scanf("%d%d", &n, a + 0); + int ans = 0, cur = 0; + for(int i = 1; i < n; ++i) { + scanf("%d", a + i); + if(abs(a[i] - a[i - 1]) <= 2) { + ++cur; + } else { + cur = 0; + } + ans = max(ans, cur); + } + printf("%d\n", ans + 1); + } + return 0; +} \ No newline at end of file diff --git a/self_training/2020-04-25-v/B.cpp b/self_training/2020-04-25-v/B.cpp new file mode 100644 index 00000000..6ef86809 --- /dev/null +++ b/self_training/2020-04-25-v/B.cpp @@ -0,0 +1,33 @@ +#include +using namespace std; +typedef long long LL; +typedef double DB; +#define x first +#define y second +const int maxn = (int)2e5 + 1, mod = (int)1e9 + 7; +int n, a[maxn], pre[maxn]; +int main() { + int t = 1; + // scanf("%d", &t); + while(t--) { + int k, x; + LL mul = 1; + scanf("%d%d%d", &n, &k, &x); + for(int i = 0; i < k; ++i) + mul *= x; + pre[0] = 0; + for(int i = 0; i < n; ++i) { + scanf("%d", a + i); + pre[i + 1] = pre[i] | a[i]; + } + int suf = 0; + LL ans = 0; + for(int i = n - 1; i >= 0; --i) { + LL tmp = pre[i] | (a[i] * mul) | suf; + ans = max(ans, tmp); + suf |= a[i]; + } + printf("%lld\n", ans); + } + return 0; +} \ No newline at end of file diff --git a/self_training/2020-04-25-v/C.cpp b/self_training/2020-04-25-v/C.cpp new file mode 100644 index 00000000..ab7020e8 --- /dev/null +++ b/self_training/2020-04-25-v/C.cpp @@ -0,0 +1,26 @@ +#include +using namespace std; +typedef long long LL; +typedef double DB; +#define x first +#define y second +const int maxn = (int)1e5 + 1, maxc = 26, mod = (int)1e9 + 7; +int main() { + int t = 1; + // scanf("%d", &t); + while(t--) { + static char s[maxn], t[maxn]; + scanf("%s%s", &s, &t); + int slen = strlen(s); + int tlen = strlen(t); + static int cnt[maxc + 1]; + memset(cnt, 0, maxc * sizeof(int)); + for(int i = 0; i < tlen - 1; ++i) + ++cnt[(int)(t[i] - 'a')]; + LL ans = (LL)slen * tlen; + for(int i = 1; i < slen; ++i) + ans -= cnt[(int)(s[i] - 'a')]; + printf("%lld\n", ans); + } + return 0; +} \ No newline at end of file diff --git a/self_training/2020-04-25-v/D.cpp b/self_training/2020-04-25-v/D.cpp new file mode 100644 index 00000000..9091ac5c --- /dev/null +++ b/self_training/2020-04-25-v/D.cpp @@ -0,0 +1,38 @@ +#include +using namespace std; +typedef long long LL; +typedef double DB; +#define x first +#define y second +const int maxn = (int)2e5 + 1, maxc = 26, mod = (int)1e9 + 7; +int n, m, pos[maxn]; +char buf[maxn], pat[maxn]; +int main() { + int t = 1; + // scanf("%d", &t); + while(t--) { + scanf("%s%s", buf, pat); + n = strlen(buf); + m = strlen(pat); + for(int i = 0; i < n; ++i) { + int x; + scanf("%d", &x); + pos[--x] = i; + } + int L = 0, R = n - 1; + while(L < R) { + int M = (L + R + 1) >> 1; + int ctr = 0; + for(int i = 0; ctr < m && i < n; ++i) + if(pos[i] >= M) + ctr += buf[i] == pat[ctr]; + if(ctr == m) { + L = M; + } else { + R = M - 1; + } + } + printf("%d\n", L); + } + return 0; +} \ No newline at end of file diff --git a/self_training/2020-04-25-v/E.cpp b/self_training/2020-04-25-v/E.cpp new file mode 100644 index 00000000..63a94404 --- /dev/null +++ b/self_training/2020-04-25-v/E.cpp @@ -0,0 +1,46 @@ +#include +using namespace std; +typedef long long LL; +typedef double DB; +#define x first +#define y second +const int maxn = (int)2e5 + 1, mod = (int)1e9 + 7; +void conv(int len, int f[], int g[]) { // f = f * g + static int res[maxn]; + memset(res + 1, 0, len * sizeof(int)); + for(int i = 1; i <= len; ++i) + for(int j = i, k = 1; j <= len; j += i, ++k) + res[j] = (res[j] + (LL)f[i] * g[k]) % mod; + memcpy(f + 1, res + 1, len * sizeof(int)); +} +int n, d, f[maxn], g[maxn]; +int main() { + int t = 1; + // scanf("%d", &t); + while(t--) { + scanf("%d%d", &d, &n); + for(int i = 1; i <= n; ++i) { + f[i] = 0; + g[i] = 1; + } + bool fir = 1; + f[1] = 1; + while(d > 0) { + if(d & 1) { + if(fir) { + memcpy(f + 1, g + 1, n * sizeof(int)); + fir = 0; + } else { + conv(n, f, g); + } + } + if((d >>= 1) > 0) + conv(n, g, g); + } + int ans = 0; + for(int i = 1; i <= n; ++i) + (ans += f[i]) >= mod && (ans -= mod); + printf("%d\n", ans); + } + return 0; +} \ No newline at end of file diff --git a/self_training/2020-04-25-v/F-2.cpp b/self_training/2020-04-25-v/F-2.cpp new file mode 100644 index 00000000..75ba0081 --- /dev/null +++ b/self_training/2020-04-25-v/F-2.cpp @@ -0,0 +1,102 @@ +#include +using namespace std; +typedef long long LL; +const int maxn = (int)1e5 + 1, mod = 998244353; +map, int> Hash; +int n, m, deg[maxn]; +pair e[maxn][3]; +bool vis[maxn]; +inline void addEdge(int u, int v, int c) { + for(int i = 0; i < deg[u]; ++i) + if(e[u][i].first == v) { + e[u][i].second |= c; + return; + } + e[u][deg[u]++] = make_pair(v, c); +} +int main() { + int t = 1; + // scanf("%d", &t); + while(t--) { + int m0, m1; + scanf("%d%d%d", &n, &m0, &m1); + for(int i = 1; i <= n; ++i) + deg[i] = vis[i] = 0; + while(m0--) { + int u, v; + scanf("%d%d", &u, &v); + addEdge(u, v, 1); + addEdge(v, u, 1); + } + while(m1--) { + int u, v; + scanf("%d%d", &u, &v); + addEdge(u, v, 2); + addEdge(v, u, 2); + } + m = 0; + Hash.clear(); + for(int i = 1; i <= n; ++i) { + if(!deg[i]) { + vis[i] = 1; + ++m; + } + if(vis[i]) + continue; + vector col, rep; + auto walk = [&](int u, int las) { + while(!vis[u]) { + vis[u] = 1; + int v = -1; + for(int i = 0; i < deg[u]; ++i) + if(e[u][i].first != las) { + col.push_back(e[u][i].second); + v = e[u][i].first; + break; + } + if(v == -1) + break; + las = u; + u = v; + } + return u; + }; + if(walk(i, 0) == i) { // circle + rep.push_back((int)col.size()); + } else { // 2-circle or chain + if(deg[i] == 2) { + reverse(col.begin(), col.end()); + vis[i] = 0; + walk(i, vis[e[i][0].first] ? e[i][0].first : e[i][1].first); + } + if(col.front() == 3) { + rep.push_back(2); + } else { + int x = col.front(), y = col.back(); + if(x > y) + swap(x, y); + rep.push_back((int)col.size()); + rep.push_back(x); + rep.push_back(y); + } + } + map, int>::iterator it = Hash.find(rep); + if(it != Hash.end()) { + ++(it -> second); + } else { + Hash.insert(make_pair(rep, 1)); + } + } + int ans = 1; + for(int i = 2; i <= m; ++i) + ans = (LL)ans * i % mod; + for(auto &it : Hash) { + auto &jt = it.first; + int ways = (int)jt.size() > 1 ? (jt[1] == jt[2] ? 2 : 1) : jt[0], cnt = it.second; + for(int i = 1; i <= cnt; ++i) + ans = (LL)ans * ways % mod * i % mod; + } + printf("%d\n", ans); + } + return 0; +} diff --git a/self_training/2020-04-25-v/F.cpp b/self_training/2020-04-25-v/F.cpp new file mode 100644 index 00000000..de4a2ddc --- /dev/null +++ b/self_training/2020-04-25-v/F.cpp @@ -0,0 +1,181 @@ +#include +using namespace std; +typedef long long LL; +typedef double DB; +#define x first +#define y second +const int maxn = (int)1e5 + 1, mod = 998244353; +int Hash0; +map, pair > Hash1, Hash2; +int n, deg[maxn], typ[maxn]; +pair e[maxn][3]; +bool vis[maxn]; +inline void addEdge(int u, int v, int c) { + for(int i = 0; i < deg[u]; ++i) + if(e[u][i].first == v) { + e[u][i].second |= c; + return; + } + e[u][deg[u]++] = make_pair(v, c); +} +int main() { + int t = 1; + // scanf("%d", &t); + while(t--) { + int m0, m1; + scanf("%d%d%d", &n, &m0, &m1); + for(int i = 1; i <= n; ++i) + deg[i] = vis[i] = 0; + while(m0--) { + int u, v; + scanf("%d%d", &u, &v); + addEdge(u, v, 1); + addEdge(v, u, 1); + } + while(m1--) { + int u, v; + scanf("%d%d", &u, &v); + addEdge(u, v, 2); + addEdge(v, u, 2); + } + Hash0 = 0; + Hash1.clear(); + Hash2.clear(); + for(int i = 1; i <= n; ++i) { + if(!deg[i]) { + vis[i] = 1; + ++Hash0; + } + if(vis[i]) + continue; + vector col; + int u, las = 0; + for(u = i; !vis[u]; ) { + vis[u] = 1; + int v = -1; + for(int i = 0; i < deg[u]; ++i) { + int w = e[u][i].first; + if(w != las) { + col.push_back(e[u][i].second); + v = w; + break; + } + } + if(v == -1) + break; + las = u; + u = v; + } + auto cmp = [](vector const &u, vector const &v) { + if(u.size() != v.size()) + return (int)u.size() - (int)v.size(); + int len = (int)u.size(); + vector::const_iterator it = u.begin(), jt = v.begin(); + for(int i = 0; i < len; ++i) { + int diff = *(it++) - *(jt++); + if(diff != 0) + return diff; + } + return 0; + }; + if(u == i) { // circle + auto get_min_rep = [](vector &vec) { + int i = 0, j = 1, k, l = (int)vec.size(); + vec.push_back(-1); + while(i < l && j < l) { + for(k = 0; k < l && vec[i + k] == vec[j + k]; ++k); + if(k == l) + break; + if(vec[i + k] > vec[j + k]) { + i = i + k + 1 > j ? i + k + 1 : j + 1; + } else { + j = j + k + 1 > i ? j + k + 1 : i + 1; + } + } + if(i >= l) + i = j; + vec.pop_back(); + rotate(vec.begin(), vec.begin() + i, vec.end()); + }; + get_min_rep(col); + vector tmp(col.begin(), col.end()); + reverse(tmp.begin(), tmp.end()); + get_min_rep(tmp); + int sgn = cmp(col, tmp); + if(sgn > 0) + col.swap(tmp); + map, pair >::iterator it = Hash2.find(col); + if(it != Hash2.end()) { + ++(it -> second).second; + } else { + int len = (int)col.size(), per = len; + for(int i = 2, tmp = len; i <= tmp; ++i) { + if(i * i > tmp) + i = tmp; + if(tmp % i > 0) + continue; + for(tmp /= i; tmp % i == 0; tmp /= i); + for( ; per % i == 0; per /= i) { + bool chk = 1; + for(int u = 0, v = per / i; chk && v < len; ) + chk &= col[u++] == col[v++]; + if(!chk) + break; + } + } + Hash2.insert(make_pair(col, make_pair((len / per) << !sgn, 1))); + } + continue; + } + if(deg[i] == 2) { + reverse(col.begin(), col.end()); + vis[i] = 0; + las = e[i][0].first; + if(!vis[las]) + las = e[i][1].first; + for(u = i; !vis[u]; ) { + vis[u] = 1; + int v = -1; + for(int i = 0; i < deg[u]; ++i) { + int w = e[u][i].first; + if(w != las) { + col.push_back(e[u][i].second); + v = w; + break; + } + } + if(v == -1) + break; + las = u; + u = v; + } + } + vector tmp(col.begin(), col.end()); + reverse(tmp.begin(), tmp.end()); + int sgn = cmp(col, tmp); + if(sgn > 0) + col.swap(tmp); + map, pair >::iterator it = Hash1.find(col); + if(it != Hash1.end()) { + ++(it -> second).second; + } else { + Hash1.insert(make_pair(col, make_pair(!sgn ? 2 : 1, 1))); + } + } + int ans = 1; + for(int i = 2; i <= Hash0; ++i) + ans = (LL)ans * i % mod; + for(auto &it : Hash1) { + int ways = it.second.first, cnt = it.second.second; + for(int i = 1; i <= cnt; ++i) + ans = (LL)ans * ways % mod * i % mod; + } + for(auto &it : Hash2) { + int ways = it.second.first, cnt = it.second.second; + for(int i = 1; i <= cnt; ++i) + ans = (LL)ans * ways % mod * i % mod; + } + printf("%d\n", ans); + } + return 0; +} diff --git a/self_training/2020-04-25-v/G.cpp b/self_training/2020-04-25-v/G.cpp new file mode 100644 index 00000000..46086fce --- /dev/null +++ b/self_training/2020-04-25-v/G.cpp @@ -0,0 +1,35 @@ +#include +using namespace std; +typedef long long LL; +typedef double DB; +#define x first +#define y second +const int maxn = (int)5e3 + 1, maxc = 26, mod = (int)1e9 + 7; +int n; +double p; +DB pw[maxn], f[maxn], g[maxn]; +int main() { + int t = 1; + // scanf("%d", &t); + while(t--) { + scanf("%d%lf", &n, &p); + pw[0] = 1; + for(int i = 1; i <= n; ++i) + pw[i] = pw[i - 1] * (1 - p); + f[0] = f[1] = g[0] = 0; + g[1] = 1; + for(int i = 2; i <= n; ++i) { + f[i] = g[i] = i; + for(int j = 1; j < i; ++j) { + DB prb = (1 - pw[j]) / (1 - pw[i]); + f[i] = min(f[i], 1 + (1 - prb) * f[i - j] + prb * (f[j] + g[i - j])); + } + for(int j = 1; j <= i; ++j) { + DB prb = pw[j]; + g[i] = min(g[i], 1 + prb * g[i - j] + (1 - prb) * (f[j] + g[i - j])); + } + } + printf("%.20f\n", (double)g[n]); + } + return 0; +} \ No newline at end of file diff --git a/self_training/2020-04-25-v/H.cpp b/self_training/2020-04-25-v/H.cpp new file mode 100644 index 00000000..3f6d3807 --- /dev/null +++ b/self_training/2020-04-25-v/H.cpp @@ -0,0 +1,91 @@ +#include +using namespace std; +typedef long long LL; +typedef double DB; +#define x first +#define y second +const int maxn = (int)1e5 + 1, INF = 0x3f3f3f3f; +int n, a[maxn], m, in[maxn], out[maxn], seg[maxn << 1 | 1]; +vector e[maxn]; +inline bool cmp(int const &u, int const &v) { + int lft = a[u] ? a[u] : INF; + int rht = a[v] ? a[v] : INF; + return lft < rht || (lft == rht && u < v); +} +inline int seg_min(int u, int v) { + return cmp(u, v) ? u : v; +} +inline int seg_idx(int L, int R) { + return (L + R) | (L < R); +} +void seg_init(int L, int R) { + if(L == R) + return; + int M = (L + R) >> 1; + seg_init(L, M); + seg_init(M + 1, R); + seg[seg_idx(L, R)] = seg_min(seg[seg_idx(L, M)], seg[seg_idx(M + 1, R)]); +} +int seg_que(int L, int R, int l, int r) { + if(l <= L && R <= r) + return seg[seg_idx(L, R)]; + int M = (L + R) >> 1, ret = 0; + if(l <= M) + ret = seg_min(ret, seg_que(L, M, l, r)); + if(r > M) + ret = seg_min(ret, seg_que(M + 1, R, l, r)); + return ret; +} +void seg_upd(int L, int R, int x) { + if(L == R) + return; + int M = (L + R) >> 1; + if(x <= M) + seg_upd(L, M, x); + else + seg_upd(M + 1, R, x); + seg[seg_idx(L, R)] = seg_min(seg[seg_idx(L, M)], seg[seg_idx(M + 1, R)]); +} +void dfs(int u, int p) { + in[u] = ++m; + seg[seg_idx(m, m)] = u; + for(int v : e[u]) + if(v != p) + dfs(v, u); + out[u] = m; +} +int main() { + int t = 1; + // scanf("%d", &t); + while(t--) { + scanf("%d", &n); + a[0] = 0; + for(int i = 1; i <= n; ++i) + scanf("%d", a + i); + for(int i = 1; i < n; ++i) { + int u, v; + scanf("%d%d", &u, &v); + e[u].push_back(v); + e[v].push_back(u); + } + m = 0; + dfs(1, -1); + seg_init(1, n); + scanf("%d", &m); + while(m--) { + int rt, dt; + scanf("%d%d", &rt, &dt); + int id = seg_que(1, n, in[rt], out[rt]); + if(!a[id]) { + puts("-1"); + continue; + } + printf("%d\n", id); + a[id] = max(a[id] - dt, 0); + seg_upd(1, n, in[id]); + } + for(int i = 1; i <= n; ++i) + vector().swap(e[i]); + } + return 0; +} \ No newline at end of file diff --git a/self_training/2020-04-25-v/I.cpp b/self_training/2020-04-25-v/I.cpp new file mode 100644 index 00000000..9d98c78e --- /dev/null +++ b/self_training/2020-04-25-v/I.cpp @@ -0,0 +1,109 @@ +#include +using namespace std; +typedef long long LL; +typedef double DB; +#define x first +#define y second +const int maxn = (int)1e5 + 1, maxm = (int)5e5 + 1, mod = (int)1e9 + 7; +int n, m, val[maxn]; +vector e[maxn]; +struct Event { + static int pL, pR; + int typ, u, v; + void parse() { + scanf("%d%d", &typ, &u); + if(typ == 1) { + v = --pL; + } else if(typ == 2) { + v = pR++; + } else { + scanf("%d", &v); + } + } +} eve[maxm]; +int Event::pL, Event::pR; +bool cmp(int const &lft, int const &rht) { + return eve[lft].v < eve[rht].v; +} +int main() { + int t = 1; + // scanf("%d", &t); + while(t--) { + static int st = 0, tim[maxn] = {}; + n = Event::pL = Event::pR = 0; + ++st; + scanf("%d", &m); + for(int i = 0; i < m; ++i) { + eve[i].parse(); + int &x = eve[i].u; + if(tim[x] != st) { + tim[x] = st; + val[n++] = x; + vector().swap(e[x]); + } + e[x].push_back(i); + } + for(int i = 0; i < n; ++i) { + int &o = val[i]; + static int tot, mx, ord[maxm], rnk[maxm], bit[maxm]; + tot = 0; + for(int &id : e[o]) { + Event &cur = eve[id]; + if(cur.typ != 3) + ord[++tot] = id; + } + sort(ord + 1, ord + tot + 1, cmp); + for(int j = 1; j <= tot; ++j) { + rnk[ord[j]] = j; + bit[j] = 0; + } + for(mx = 1; (mx << 1) <= tot; mx <<= 1); + for(int &id : e[o]) { + Event &cur = eve[id]; + if(cur.typ == 3) { + int x = 0, k = cur.v; + for(int y = mx, z; y > 0; y >>= 1) + if((z = x | y) <= tot && bit[z] < k) { + x = z; + k -= bit[z]; + } + for(++x; x <= tot; x += x & -x) + --bit[x]; + } else { + for(int x = rnk[id]; x <= tot; x += x & -x) + ++bit[x]; + } + } + for(int x = 1, y; x <= tot; ++x) + if((y = x - (x & -x)) > 0) + bit[x] += bit[y]; + vector pos; + bit[0] = 0; + for(int x = tot; x > 0; --x) { + bit[x] -= bit[x - 1]; + if(bit[x] > 0) { + // assert(bit[x] == 1); + int id = ord[x]; + pos.push_back(eve[id].v); + } + } + e[o].swap(pos); + } + priority_queue > Q; + for(int i = 0; i < n; ++i) { + int &o = val[i]; + if(e[o].empty()) + continue; + Q.push(make_pair(-e[o].back(), o)); + } + while(!Q.empty()) { + int o = Q.top().second; + Q.pop(); + printf("%d\n", o); + e[o].pop_back(); + if(!e[o].empty()) + Q.push(make_pair(-e[o].back(), o)); + } + } + return 0; +} \ No newline at end of file diff --git a/self_training/2020-05-06-cf1/2A.cpp b/self_training/2020-05-06-cf1/2A.cpp new file mode 100644 index 00000000..1c80d974 --- /dev/null +++ b/self_training/2020-05-06-cf1/2A.cpp @@ -0,0 +1,13 @@ +#include +using namespace std; +int main() { + int t, n, m; + scanf("%d", &t); + while(t--) { + scanf("%d%d", &n, &m); + if(n > m) + swap(n, m); + puts(n == 1 || m == 2 ? "YES" : "NO"); + } + return 0; +} \ No newline at end of file diff --git a/self_training/2020-05-06-cf1/2B.cpp b/self_training/2020-05-06-cf1/2B.cpp new file mode 100644 index 00000000..1af5d9bc --- /dev/null +++ b/self_training/2020-05-06-cf1/2B.cpp @@ -0,0 +1,26 @@ +#include +using namespace std; +int main() { + const int maxn = (int)1e9 + 1; + vector dp = vector{0, 2}; + while(dp.back() < maxn) { + int h = (int)dp.size(); + dp.push_back(dp.back() + 3 * h - 1); + } + dp.pop_back(); + reverse(dp.begin(), dp.end()); + dp.pop_back(); + int t, n; + scanf("%d", &t); + while(t--) { + scanf("%d", &n); + int cnt = 0; + for(int x : dp) + if(n >= x) { + cnt += n / x; + n %= x; + } + printf("%d\n", cnt); + } + return 0; +} \ No newline at end of file diff --git a/self_training/2020-05-06-cf1/A.cpp b/self_training/2020-05-06-cf1/A.cpp new file mode 100644 index 00000000..9607d13e --- /dev/null +++ b/self_training/2020-05-06-cf1/A.cpp @@ -0,0 +1,32 @@ +#include +using namespace std; +typedef unsigned int UL; +typedef long long LL; +typedef unsigned long long ULL; +typedef double DB; +typedef long double LD; +#define x first +#define y second +const int maxn = (int)3e5 + 1, maxd = 26, mod = (int)1e9 + 7; + +int main() { + int t = 1; + scanf("%d", &t); + for(int Case = 1; Case <= t; ++Case) { + int n; + static bool vis[maxn]; + scanf("%d", &n); + memset(vis, 0, n * sizeof(bool)); + for(int i = 0; i < n; ++i) { + int x; + scanf("%d", &x); + (x %= n) < 0 && (x += n); + (x += i) >= n && (x -= n); + vis[x] = 1; + } + int pos; + for(pos = 0; pos < n && vis[pos]; ++pos); + puts(pos < n ? "NO" : "YES"); + } + return 0; +} \ No newline at end of file diff --git a/self_training/2020-05-06-cf1/B.cpp b/self_training/2020-05-06-cf1/B.cpp new file mode 100644 index 00000000..e0a5c078 --- /dev/null +++ b/self_training/2020-05-06-cf1/B.cpp @@ -0,0 +1,67 @@ +#include +using namespace std; +typedef unsigned int UL; +typedef long long LL; +typedef unsigned long long ULL; +typedef double DB; +typedef long double LD; +#define x first +#define y second +const int maxn = (int)1e3 + 1, maxd = 26, mod = (int)1e9 + 7; + +int main() { + int t = 1; + // scanf("%d", &t); + for(int Case = 1; Case <= t; ++Case) { + int n, m; + scanf("%d%d", &n, &m); + static char buf[maxn][maxn]; + bool chk = 1; + int cntR = 0, cntC = 0; + for(int i = 0; i < n; ++i) { + scanf("%s", buf[i]); + int L = -1, R = -1, cnt = 0; + for(int j = 0; j < m; ++j) + if(buf[i][j] == '#') { + ++cnt; + L = L < 0 ? j : L; + R = j; + } + cntR += !cnt; + chk &= !cnt || R - L + 1 == cnt; + } + for(int i = 0; chk && i < m; ++i) { + int L = -1, R = -1, cnt = 0; + for(int j = 0; j < n; ++j) + if(buf[j][i] == '#') { + ++cnt; + L = L < 0 ? j : L; + R = j; + } + cntC += !cnt; + chk &= !cnt || R - L + 1 == cnt; + } + if(!chk || (!cntR) != (!cntC)) { + puts("-1"); + continue; + } + function dfs = [&](int x, int y) { + if(x < 0 || x >= n || y < 0 || y >= m || buf[x][y] != '#') + return; + buf[x][y] = '.'; + dfs(x - 1, y); + dfs(x, y - 1); + dfs(x, y + 1); + dfs(x + 1, y); + }; + int ans = 0; + for(int i = 0; i < n; ++i) + for(int j = 0; j < m; ++j) + if(buf[i][j] == '#') { + ++ans; + dfs(i, j); + } + printf("%d\n", ans); + } + return 0; +} \ No newline at end of file diff --git a/self_training/2020-05-06-cf1/C.cpp b/self_training/2020-05-06-cf1/C.cpp new file mode 100644 index 00000000..d644ea43 --- /dev/null +++ b/self_training/2020-05-06-cf1/C.cpp @@ -0,0 +1,71 @@ +#include +using namespace std; +typedef unsigned int UL; +typedef long long LL; +typedef unsigned long long ULL; +typedef double DB; +typedef long double LD; +#define x first +#define y second +const int maxn = (int)3e5 + 1, maxd = 26, mod = (int)1e9 + 7; + +int main() { + int t = 1; + // scanf("%d", &t); + for(int Case = 1; Case <= t; ++Case) { + int n, m; + scanf("%d%d", &n, &m); + vector deg(n, 0), dsu(n, -1); + vector > e(n, vector()), re(n, vector()); + for(int i = 0; i < m; ++i) { + int u, v; + scanf("%d%d", &u, &v); + e[--u].push_back(--v); + re[v].push_back(u); + ++deg[v]; + } + queue Q; + for(int i = 0; i < n; ++i) + if(!deg[i]) + Q.push(i); + int cnt = 0; + while(!Q.empty()) { + int u = Q.front(); + ++cnt; + Q.pop(); + for(int &v : e[u]) + if(!(--deg[v])) + Q.push(v); + } + if(cnt < n) { + puts("-1"); + continue; + } + vector vis(n, 0), rvis(n, 0); + function dfs = [&](int u) { + if(vis[u]) + return; + vis[u] = 1; + for(int &v : e[u]) + dfs(v); + }; + function rdfs = [&](int u) { + if(rvis[u]) + return; + rvis[u] = 1; + for(int &v : re[u]) + rdfs(v); + }; + int ans = 0; + static char buf[maxn]; + for(int i = 0; i < n; ++i) { + buf[i] = "AE"[vis[i] || rvis[i]]; + ans += buf[i] == 'A'; + dfs(i); + rdfs(i); + } + buf[n] = '\0'; + printf("%d\n%s\n", ans, buf); + } + return 0; +} \ No newline at end of file diff --git a/self_training/2020-05-06-cf1/D.cpp b/self_training/2020-05-06-cf1/D.cpp new file mode 100644 index 00000000..9dd142f2 --- /dev/null +++ b/self_training/2020-05-06-cf1/D.cpp @@ -0,0 +1,77 @@ +#include +using namespace std; +typedef unsigned int UL; +typedef long long LL; +typedef unsigned long long ULL; +typedef double DB; +typedef long double LD; +#define x first +#define y second +const int maxn = (int)3e5 + 1, maxd = 26, mod = (int)1e9 + 7; + +int main() { + int t = 1; + // scanf("%d", &t); + for(int Case = 1; Case <= t; ++Case) { + int n; + LL k; + static int a[maxn]; + scanf("%d%lld", &n, &k); + for(int i = 0; i < n; ++i) + scanf("%d", a + i); + const LL INF = (LL)4e18; + auto calc_cost = [&](int bi, int ai) { + return bi * (ai - (LL)bi * bi); + }; + auto calc_diff = [&](int bi, int ai) { + return bi < ai ? ai - bi * (bi + 1LL) * 3 - 1 : -INF; + }; + static int b[maxn]; + auto gen = [&](LL lim) { + // printf("gen %lld\n", lim); + for(int i = 0; i < n; ++i) { + int L = 0, R = a[i]; + while(L < R) { + int M = (L + R) / 2; + if(calc_diff(M, a[i]) < lim) { + R = M; + } else { + L = M + 1; + } + } + b[i] = L; + // printf("%d%c", b[i], " \n"[i == n - 1]); + } + }; + LL L = -INF, R = INF; + while(L < R) { + LL M = L + (R - L + 1) / 2; + gen(M); + LL cnt = 0; + for(int i = 0; i < n; ++i) + cnt += b[i]; + if(cnt < k) { + R = M - 1; + } else { + L = M; + } + } + gen(L + 1); + LL rem = k; + priority_queue > Q; + for(int i = 0; i < n; ++i) { + rem -= b[i]; + if(b[i] < a[i]) + Q.push(make_pair(calc_diff(b[i], a[i]), i)); + } + for( ; rem > 0; --rem) { + int i = Q.top().second; + Q.pop(); + if((++b[i]) < a[i]) + Q.push(make_pair(calc_diff(b[i], a[i]), i)); + } + for(int i = 0; i < n; ++i) + printf("%d%c", b[i], " \n"[i == n - 1]); + } + return 0; +} \ No newline at end of file diff --git a/self_training/2020-05-06-cf1/E.cpp b/self_training/2020-05-06-cf1/E.cpp new file mode 100644 index 00000000..7473a68a --- /dev/null +++ b/self_training/2020-05-06-cf1/E.cpp @@ -0,0 +1,123 @@ +#include +using namespace std; +int main() { + int n, m; + scanf("%d%d", &n, &m); + vector > > e(n, vector >()); + for(int i = 1; i < n; ++i) { + int u, v, d; + scanf("%d%d%d", &u, &v, &d); + e[--u].push_back(make_pair(--v, d)); + } + vector > info; + vector > adt(n, set()), eve(n, set()); + for(int i = 0; i < m; ++i) { + int s, t; + scanf("%d%d", &s, &t); + info.push_back(make_pair(--s, t)); + adt[s].insert(i); + } + typedef long long LL; + vector > seg, rseg; + auto upd_seg = [&](int x, int y, LL adt, bool add = 1) { + auto tmp = make_pair(info[x].second + adt, info[y].second + adt); + if(add) { + seg.push_back(tmp); + } else { + rseg.push_back(tmp); + } + }; + vector dsu(n, -1); + function dsu_find = [&](int u) { + return dsu[u] < 0 ? u : (dsu[u] = dsu_find(dsu[u])); + }; + auto dsu_check = [&](int x, int y) { + return dsu_find(info[x].first) == dsu_find(info[y].first); + }; + function dfs = [&](int u, LL dis) { + for(auto &it : e[u]) { + int &v = it.first; + dfs(v, dis + it.second); + if(eve[u].size() < eve[v].size()) + eve[u].swap(eve[v]); + typedef set::iterator Iter; + Iter vL = eve[v].begin(); + while(vL != eve[v].end()) { + Iter uR = eve[u].lower_bound(*vL); + if(uR != eve[u].begin()) { + Iter uL = uR; + --uL; + if(uR != eve[u].end() && !dsu_check(*uL, *uR)) + upd_seg(*uL, *uR, dis, 0); + upd_seg(*uL, *vL, dis); + } + if(uR == eve[u].end()) { + eve[u].insert(vL, eve[v].end()); + vL = eve[v].end(); + } else { + int upp = *uR; + Iter vR = vL; + while(vR != eve[v].end() && *vR < upp) { + eve[u].insert(*vR); + vL = vR++; + } + upd_seg(*vL, upp, dis); + if(vR != eve[v].end() && !dsu_check(*vL, *vR)) + upd_seg(*vL, *vR, dis, 0); + ++vL; + } + } + set().swap(eve[v]); + } + if(!eve[u].empty()) { + int low = *eve[u].begin(); + if(dsu_find(info[low].first) != e[u].back().first) + seg.push_back(make_pair(0LL, info[low].second + dis)); + } + eve[u].insert(adt[u].begin(), adt[u].end()); + set().swap(adt[u]); + for(auto &it : e[u]) + dsu[it.first] = u; + }; + dfs(0, 0); + sort(seg.begin(), seg.end()); + sort(rseg.begin(), rseg.end()); + auto it = seg.begin(), jt = it, kt = rseg.begin(); + while(it != seg.end()) { + if(kt != rseg.end() && *it == *kt) { + ++kt; + } else if(it == jt) { + ++jt; + } else { + *(jt++) = *it; + } + ++it; + } + assert(kt == rseg.end()); + seg.erase(jt, seg.end()); + LL cur = 0, lim = -1; + it = seg.begin(); + priority_queue, greater > Q; + while(it != seg.end() || !Q.empty()) { + if(Q.empty() && (it -> first) > cur) + cur = it -> first; + while(it != seg.end() && (it -> first) <= cur) + Q.push((it++) -> second); + LL upp = Q.top(); + Q.pop(); + if(cur >= upp) { + lim = upp; + break; + } + ++cur; + } + int cnt = 0; + if(lim == -1) { + cnt += (int)seg.size(); + } else { + for(auto &it : seg) + cnt += it.second < lim; + } + printf("%lld %d\n", lim, cnt); + return 0; +} \ No newline at end of file diff --git a/self_training/2020-05-06-cf1/F.cpp b/self_training/2020-05-06-cf1/F.cpp new file mode 100644 index 00000000..695d59cc --- /dev/null +++ b/self_training/2020-05-06-cf1/F.cpp @@ -0,0 +1,117 @@ +#include +using namespace std; +typedef unsigned int UL; +typedef long long LL; +typedef unsigned long long ULL; +typedef double DB; +typedef long double LD; +#define x first +#define y second +const int maxn = (int)2e3 + 1, maxd = 26, mod = (int)1e9 + 7; + +int main() { + int t = 1; + // scanf("%d", &t); + for(int Case = 1; Case <= t; ++Case) { + int n, m; + scanf("%d%d", &n, &m); + const char *pat = "WRYB"; + const int trI = 9, trRY = 6, trRB = 13, trYB = 11; + auto trans = [](int lft, int rht) { + int ret = 0; + for(int i = 0; i < 4; ++i, lft >>= 1) + if(lft & 1) + ret ^= ((rht >> ((i & 1) << 1)) & 3) << (i & 2); + return ret; + }; + int row = 0, col = n << 1; // [0..col-1] | [col] + static int rep[maxn]; + static bitset mat[maxn]; + for(int i = 0; i < n; ++i) + rep[i] = trI; + while(m--) { + int cnt, pos; + static char op[5]; + scanf("%s", op); + if(op[0] == 'm') { + mat[row].reset(); + mat[row + 1].reset(); + scanf("%d", &cnt); + while(cnt--) { + scanf("%d", &pos); + --pos; + for(int i = 0, tmp = rep[pos]; i < 4; ++i, tmp >>= 1) + if(tmp & 1) + mat[row + !!(i & 2)].flip(pos << 1 | (i & 1)); + } + scanf("%s", op); + int msk; + for(int i = 0; i < 4; ++i) + if(pat[i] == op[0]) { + msk = i; + break; + } + for(int i = 0; i < 2; ++i, msk >>= 1) + if(msk & 1) + mat[row + i].flip(col); + row += 2; + } else { + int tr = op[0] == 'R' ? (op[1] == 'Y' ? trRY : trRB) : trYB; + scanf("%d", &cnt); + while(cnt--) { + scanf("%d", &pos); + --pos; + rep[pos] = trans(tr, rep[pos]); + } + } + } + // for(int i = 0; i < row; ++i) { + // static char buf[maxn]; + // for(int j = 0; j <= col; ++j) + // buf[j] = "01"[mat[i].test(j)]; + // buf[col + 1] = '\0'; + // puts(buf); + // } + int rk = 0; + for(int i = 0; i < col; ++i) { + int pivot = -1; + for(int j = rk; j < row; ++j) + if(mat[j].test(i)) { + pivot = j; + break; + } + if(pivot == -1) + continue; + swap(mat[rk], mat[pivot]); + for(int j = 0; j < row; ++j) + if(j != rk && mat[j].test(i)) + mat[j] ^= mat[rk]; + ++rk; + } + bool chk = 1; + for(int i = rk; chk && i < row; ++i) + chk &= !mat[i].test(col); + if(!chk) { + puts("NO"); + } else { + puts("YES"); + for(int i = 0; i < n; ++i) + rep[i] = 0; + for(int i = 0; i < rk; ++i) { + if(!mat[i].test(col)) + continue; + for(int j = i; j < col; ++j) + if(mat[i].test(j)) { + rep[j >> 1] |= 1 << (j & 1); + break; + } + } + static char buf[maxn]; + for(int i = 0; i < n; ++i) + buf[i] = rep[i] ? pat[rep[i]] : '.'; + buf[n] = '\0'; + puts(buf); + } + } + return 0; +} \ No newline at end of file diff --git a/self_training/2020-05-08-cft/A.cpp b/self_training/2020-05-08-cft/A.cpp new file mode 100644 index 00000000..8bbdf104 --- /dev/null +++ b/self_training/2020-05-08-cft/A.cpp @@ -0,0 +1,12 @@ +#include +using namespace std; +int main() { + int t = 1; + scanf("%d", &t); + for(int Case = 1; Case <= t; ++Case) { + int a, b; + scanf("%d%d", &a, &b); + printf("%d\n", a + b); + } + return 0; +} \ No newline at end of file diff --git a/self_training/2020-05-08-cft/B.cpp b/self_training/2020-05-08-cft/B.cpp new file mode 100644 index 00000000..690925ff --- /dev/null +++ b/self_training/2020-05-08-cft/B.cpp @@ -0,0 +1,22 @@ +#include +using namespace std; +int main() { + int t = 1; + scanf("%d", &t); + for(int Case = 1; Case <= t; ++Case) { + int a, b, c, d; + scanf("%d%d%d%d", &a, &b, &c, &d); + auto check = [&]() { + if(a != c) + swap(c, d); + return a == c && a == b + d; + }; + bool chk = check(); + if(!chk) { + swap(a, b); + chk = check(); + } + puts(chk ? "Yes" : "No"); + } + return 0; +} \ No newline at end of file diff --git a/self_training/2020-05-08-cft/C.cpp b/self_training/2020-05-08-cft/C.cpp new file mode 100644 index 00000000..25e68da7 --- /dev/null +++ b/self_training/2020-05-08-cft/C.cpp @@ -0,0 +1,29 @@ +#include +using namespace std; +const char *pat = "NWSE"; +const int maxn = (int)1e5 + 1, dt[][2] = { + {-1, 0}, {0, -1}, {1, 0}, {0, 1} +}; +int main() { + int t = 1; + scanf("%d", &t); + for(int Case = 1; Case <= t; ++Case) { + int tot = 0; + static pair seq[maxn]; + static char buf[maxn]; + scanf("%s", buf); + for(int i = 0, cx = 0, cy = 0, nx, ny; buf[i]; ++i, cx = nx, cy = ny) + for(int j = 0; pat[j]; ++j) + if(buf[i] == pat[j]) { + nx = cx + dt[j][0]; + ny = cy + dt[j][1]; + seq[tot++] = make_pair(cx + nx, cy + ny); + break; + } + int ans = tot; + sort(seq, seq + tot); + ans += (unique(seq, seq + tot) - seq) << 2; + printf("%d\n", ans); + } + return 0; +} \ No newline at end of file diff --git a/self_training/2020-05-09-cf4/A.cpp b/self_training/2020-05-09-cf4/A.cpp new file mode 100644 index 00000000..1311244d --- /dev/null +++ b/self_training/2020-05-09-cf4/A.cpp @@ -0,0 +1,27 @@ +#include +using namespace std; +typedef unsigned int UL; +typedef long long LL; +typedef unsigned long long ULL; +typedef double DB; +typedef long double LDB; +const int maxn = (int)3e5 + 1, maxd = 26, mod = (int)1e9 + 7, INF = 0x3f3f3f3f; +const LL INFLL = 0x3f3f3f3f3f3f3f3fLL; +const DB pi = acos((DB)-1); +int main() { + int T = 1; + scanf("%d", &T); + for(int Case = 1; Case <= T; ++Case) { + int n; + scanf("%d", &n); + vector vec; + for(int pw = 1; n > 0; pw *= 10, n /= 10) + if(n % 10) + vec.push_back((n % 10) * pw); + printf("%d\n", (int)vec.size()); + for(int x : vec) + printf("%d ", x); + puts(""); + } + return 0; +} \ No newline at end of file diff --git a/self_training/2020-05-09-cf4/B.cpp b/self_training/2020-05-09-cf4/B.cpp new file mode 100644 index 00000000..c22b0c9f --- /dev/null +++ b/self_training/2020-05-09-cf4/B.cpp @@ -0,0 +1,33 @@ +#include +using namespace std; +typedef unsigned int UL; +typedef long long LL; +typedef unsigned long long ULL; +typedef double DB; +typedef long double LDB; +const int maxn = (int)3e5 + 1, maxd = 26, mod = (int)1e9 + 7, INF = 0x3f3f3f3f; +const LL INFLL = 0x3f3f3f3f3f3f3f3fLL; +const DB pi = acos((DB)-1); +int main() { + int T = 1; + scanf("%d", &T); + for(int Case = 1; Case <= T; ++Case) { + int n, k; + scanf("%d%d", &n, &k); + // odd + if(n >= k && !((n - k) & 1)) { + puts("YES"); + for(int i = 0; i < k - 1; ++i) + printf("1 "); + printf("%d\n", n - (k - 1)); + } else if(n >= k + k && !(n & 1)) { + puts("YES"); + for(int i = 0; i < k - 1; ++i) + printf("2 "); + printf("%d\n", n - (k - 1) * 2); + } else { + puts("NO"); + } + } + return 0; +} \ No newline at end of file diff --git a/self_training/2020-05-09-cf4/C.cpp b/self_training/2020-05-09-cf4/C.cpp new file mode 100644 index 00000000..62a00498 --- /dev/null +++ b/self_training/2020-05-09-cf4/C.cpp @@ -0,0 +1,21 @@ +#include +using namespace std; +typedef unsigned int UL; +typedef long long LL; +typedef unsigned long long ULL; +typedef double DB; +typedef long double LDB; +const int maxn = (int)3e5 + 1, maxd = 26, mod = (int)1e9 + 7, INF = 0x3f3f3f3f; +const LL INFLL = 0x3f3f3f3f3f3f3f3fLL; +const DB pi = acos((DB)-1); +int main() { + int T = 1; + scanf("%d", &T); + for(int Case = 1; Case <= T; ++Case) { + int n, k; + scanf("%d%d", &n, &k); + int m = (k - 1) / (n - 1), r = (k - 1) % (n - 1) + 1; + printf("%d\n", m * n + r); + } + return 0; +} \ No newline at end of file diff --git a/self_training/2020-05-09-cf4/D.cpp b/self_training/2020-05-09-cf4/D.cpp new file mode 100644 index 00000000..af01b72d --- /dev/null +++ b/self_training/2020-05-09-cf4/D.cpp @@ -0,0 +1,35 @@ +#include +using namespace std; +typedef unsigned int UL; +typedef long long LL; +typedef unsigned long long ULL; +typedef double DB; +typedef long double LDB; +const int maxn = (int)3e5 + 1, maxd = 26, mod = (int)1e9 + 7, INF = 0x3f3f3f3f; +const LL INFLL = 0x3f3f3f3f3f3f3f3fLL; +const DB pi = acos((DB)-1); +int main() { + int T = 1; + scanf("%d", &T); + for(int Case = 1; Case <= T; ++Case) { + int n; + static int arr[maxn]; + scanf("%d", &n); + for(int i = 0; i < n; ++i) + scanf("%d", arr + i); + int tim = 0, cL = 0, cR = 0, las = 0; + for(int L = 0, R = n - 1; L <= R; ++tim) { + int ctr = 0; + if(tim & 1) { + for( ; L <= R && ctr <= las; ctr += arr[R--]); + cR += ctr; + } else { + for( ; L <= R && ctr <= las; ctr += arr[L++]); + cL += ctr; + } + las = ctr; + } + printf("%d %d %d\n", tim, cL, cR); + } + return 0; +} \ No newline at end of file diff --git a/self_training/2020-05-09-cf4/E.cpp b/self_training/2020-05-09-cf4/E.cpp new file mode 100644 index 00000000..5bdc1c08 --- /dev/null +++ b/self_training/2020-05-09-cf4/E.cpp @@ -0,0 +1,32 @@ +#include +using namespace std; +typedef unsigned int UL; +typedef long long LL; +typedef unsigned long long ULL; +typedef double DB; +typedef long double LDB; +const int maxn = (int)8e3 + 1, maxd = 26, mod = (int)1e9 + 7, INF = 0x3f3f3f3f; +const LL INFLL = 0x3f3f3f3f3f3f3f3fLL; +const DB pi = acos((DB)-1); +int main() { + int T = 1; + scanf("%d", &T); + for(int Case = 1; Case <= T; ++Case) { + int n; + static int arr[maxn]; + static bool vis[maxn]; + scanf("%d", &n); + for(int i = 1; i <= n; ++i) { + scanf("%d", arr + i); + vis[i] = 0; + } + for(int i = 1; i + 1 <= n; ++i) + for(int j = i + 1, sum = arr[i] + arr[j]; j <= n && sum <= n; sum += arr[++j]) + vis[sum] = 1; + int ans = 0; + for(int i = 1; i <= n; ++i) + ans += vis[arr[i]]; + printf("%d\n", ans); + } + return 0; +} \ No newline at end of file diff --git a/self_training/2020-05-09-cf4/F.cpp b/self_training/2020-05-09-cf4/F.cpp new file mode 100644 index 00000000..e1fca65c --- /dev/null +++ b/self_training/2020-05-09-cf4/F.cpp @@ -0,0 +1,65 @@ +#include +using namespace std; +typedef unsigned int UL; +typedef long long LL; +typedef unsigned long long ULL; +typedef double DB; +typedef long double LDB; +const int maxn = 101, maxd = 26, mod = (int)1e9 + 7, INF = 0x3f3f3f3f; +const LL INFLL = 0x3f3f3f3f3f3f3f3fLL; +const DB pi = acos((DB)-1); +int dp[2][maxn][maxn][maxn]; +int F(int las, int c0, int c1, int c2) { + if(!c0 && !c1 && !c2) + return 1; + if(c0 < 0 || c1 < 0 || c2 < 0) + return 0; + int &ret = dp[las][c0][c1][c2]; + if(ret == -1) { + if(!las) { + ret = F(0, c0 - 1, c1, c2) || F(1, c0, c1 - 1, c2); + } else { + ret = F(0, c0, c1 - 1, c2) || F(1, c0, c1, c2 - 1); + } + } + return ret; +} +int main() { + memset(dp, -1, sizeof dp); + int T = 1; + scanf("%d", &T); + for(int Case = 1; Case <= T; ++Case) { + int c0, c1, c2; + scanf("%d%d%d", &c0, &c1, &c2); + int tot = 0; + static int arr[maxn + 2]; + for(int i = 0; i < 2; ++i) + if(F(i, c0, c1, c2)) { + arr[tot++] = i; + int las = i; + while(c0 || c1 || c2) + if(!las) { + if(F(0, c0 - 1, c1, c2)) { + las = arr[tot++] = 0; + --c0; + } else { + las = arr[tot++] = 1; + --c1; + } + } else { + if(F(0, c0, c1 - 1, c2)) { + las = arr[tot++] = 0; + --c1; + } else { + las = arr[tot++] = 1; + --c2; + } + } + break; + } + for(int i = 0; i < tot; ++i) + printf("%d", arr[i]); + puts(""); + } + return 0; +} \ No newline at end of file diff --git a/self_training/2020-05-09-cf4/G.cpp b/self_training/2020-05-09-cf4/G.cpp new file mode 100644 index 00000000..11e3639d --- /dev/null +++ b/self_training/2020-05-09-cf4/G.cpp @@ -0,0 +1,37 @@ +#include +using namespace std; +typedef unsigned int UL; +typedef long long LL; +typedef unsigned long long ULL; +typedef double DB; +typedef long double LDB; +const int maxn = (int)3e5 + 1, maxd = 26, mod = (int)1e9 + 7, INF = 0x3f3f3f3f; +const LL INFLL = 0x3f3f3f3f3f3f3f3fLL; +const DB pi = acos((DB)-1); +int main() { + int T = 1; + scanf("%d", &T); + for(int Case = 1; Case <= T; ++Case) { + int n; + scanf("%d", &n); + if(n <= 3) { + puts("-1"); + continue; + } + int tot = 0, pos; + static int arr[maxn]; + for(int i = 1; i <= n; i += 2) + arr[tot++] = i; + pos = arr[tot - 1] == n ? tot - 1 : tot; + for(int i = n - (n & 1); i > 0; i -= 2) + arr[tot++] = i; + if(n & 1) { + swap(arr[pos + 1], arr[pos + 2]); + } else { + swap(arr[pos - 1], arr[pos - 2]); + } + for(int i = 0; i < tot; ++i) + printf("%d%c", arr[i], " \n"[i == tot - 1]); + } + return 0; +} \ No newline at end of file diff --git a/self_training/2020-05-10-cfe/A.cpp b/self_training/2020-05-10-cfe/A.cpp new file mode 100644 index 00000000..d33dead4 --- /dev/null +++ b/self_training/2020-05-10-cfe/A.cpp @@ -0,0 +1,27 @@ +#include +using namespace std; +typedef unsigned int UL; +typedef long long LL; +typedef unsigned long long ULL; +typedef double DB; +typedef long double LDB; +const int maxn = (int)3e5 + 1, maxd = 26, mod = (int)1e9 + 7, INF = 0x3f3f3f3f; +const LL INFLL = 0x3f3f3f3f3f3f3f3fLL; +const DB pi = acos((DB)-1); + +int main() { + int T = 1; + scanf("%d", &T); + for(int Case = 1; Case <= T; ++Case) { + int x, y, a, b; + scanf("%d%d%d%d", &x, &y, &a, &b); + LL ans = (LL)a * (abs(x) + abs(y)); + auto check = [&](int u, int v) { + ans = min(ans, (LL)b * abs(u) + (LL)a * abs(v - u)); + }; + check(x, y); + check(y, x); + printf("%lld\n", ans); + } + return 0; +} \ No newline at end of file diff --git a/self_training/2020-05-10-cfe/B.cpp b/self_training/2020-05-10-cfe/B.cpp new file mode 100644 index 00000000..dc8770bc --- /dev/null +++ b/self_training/2020-05-10-cfe/B.cpp @@ -0,0 +1,30 @@ +#include +using namespace std; +typedef unsigned int UL; +typedef long long LL; +typedef unsigned long long ULL; +typedef double DB; +typedef long double LDB; +const int maxn = (int)3e5 + 1, maxd = 26, mod = (int)1e9 + 7, INF = 0x3f3f3f3f; +const LL INFLL = 0x3f3f3f3f3f3f3f3fLL; +const DB pi = acos((DB)-1); + +int main() { + int T = 1; + scanf("%d", &T); + for(int Case = 1; Case <= T; ++Case) { + static char buf[maxn]; + scanf("%s", buf); + int len = strlen(buf), cnt[2] = {}; + for(int i = 0; i < len; ++i) + ++cnt[(int)(buf[i] - '0')]; + if(min(cnt[0], cnt[1]) > 0) { + len <<= 1; + for(int i = 0; i < len; ++i) + buf[i] = "01"[i & 1]; + buf[len] = '\0'; + } + puts(buf); + } + return 0; +} \ No newline at end of file diff --git a/self_training/2020-05-10-cfe/C.cpp b/self_training/2020-05-10-cfe/C.cpp new file mode 100644 index 00000000..aa59b33d --- /dev/null +++ b/self_training/2020-05-10-cfe/C.cpp @@ -0,0 +1,38 @@ +#include +using namespace std; +typedef unsigned int UL; +typedef long long LL; +typedef unsigned long long ULL; +typedef double DB; +typedef long double LDB; +const int maxn = (int)3e5 + 1, maxd = 26, mod = (int)1e9 + 7, INF = 0x3f3f3f3f; +const LL INFLL = 0x3f3f3f3f3f3f3f3fLL; +const DB pi = acos((DB)-1); + +int main() { + int T = 1; + scanf("%d", &T); + for(int Case = 1; Case <= T; ++Case) { + int a, b, q; + scanf("%d%d%d", &a, &b, &q); + int low = max(a, b), upp = a / __gcd(a, b) * b; + if(low == upp) { + while(q--) { + scanf("%*lld%*lld"); + printf("0 "); + } + puts(""); + continue; + } + while(q--) { + LL L, R; + scanf("%lld%lld", &L, &R); + LL ans = R - L + 1; + for(int i = 0; i < low; ++i) + ans -= (R >= i ? (R - i) / upp + 1 : 0) - (L > i ? (L - 1 - i) / upp + 1 : 0); + printf("%lld ", ans); + } + puts(""); + } + return 0; +} \ No newline at end of file diff --git a/self_training/2020-05-10-cfe/D.cpp b/self_training/2020-05-10-cfe/D.cpp new file mode 100644 index 00000000..999bc372 --- /dev/null +++ b/self_training/2020-05-10-cfe/D.cpp @@ -0,0 +1,50 @@ +#include +using namespace std; +typedef unsigned int UL; +typedef long long LL; +typedef unsigned long long ULL; +typedef double DB; +typedef long double LDB; +const int maxn = (int)3e5 + 1, maxd = 26, mod = (int)1e9 + 7, INF = 0x3f3f3f3f; +const LL INFLL = 0x3f3f3f3f3f3f3f3fLL; +const DB pi = acos((DB)-1); + +int main() { + int T = 1; + // scanf("%d", &T); + for(int Case = 1; Case <= T; ++Case) { + int n, m; + static int cnt[maxn], upp[maxn]; + scanf("%d%d", &n, &m); + memset(cnt + 1, 0, m * sizeof(int)); + for(int i = 1; i <= n; ++i) { + int x; + scanf("%d", &x); + ++cnt[x]; + } + for(int i = 1; i <= m; ++i) + scanf("%d", upp + i); + int tot = 0; + for(int i = m, sum = 0; i >= 1; --i) { + sum += cnt[i]; + if(sum > 0) + tot = max(tot, (sum - 1) / upp[i] + 1); + } + static vector out[maxn]; + for(int i = 0; i < tot; ++i) + vector().swap(out[i]); + for(int i = m, pos = 0; i >= 1; --i) + while(cnt[i]--) { + out[pos].push_back(i); + (++pos) == tot && (pos = 0); + } + printf("%d\n", tot); + for(int i = 0; i < tot; ++i) { + printf("%d", (int)out[i].size()); + for(int &x : out[i]) + printf(" %d", x); + puts(""); + } + } + return 0; +} \ No newline at end of file diff --git a/self_training/2020-05-10-cfe/E.cpp b/self_training/2020-05-10-cfe/E.cpp new file mode 100644 index 00000000..56054717 --- /dev/null +++ b/self_training/2020-05-10-cfe/E.cpp @@ -0,0 +1,49 @@ +#include +using namespace std; +typedef unsigned int UL; +typedef long long LL; +typedef unsigned long long ULL; +typedef double DB; +typedef long double LDB; +const int maxn = (int)2e5 + 1, maxd = 26, mod = 998244353, INF = 0x3f3f3f3f; +const LL INFLL = 0x3f3f3f3f3f3f3f3fLL; +const DB pi = acos((DB)-1); + +int main() { + static int ifact[maxn]; + ifact[1] = 1; + for(int i = 2; i < maxn; ++i) + ifact[i] = mod - (int)(mod / i * (LL)ifact[mod % i] % mod); + ifact[0] = 1; + for(int i = 1; i < maxn; ++i) + ifact[i] = (LL)ifact[i - 1] * ifact[i] % mod; + int T = 1; + // scanf("%d", &T); + for(int Case = 1; Case <= T; ++Case) { + int n; + LL k; + scanf("%d%lld", &n, &k); + if(k >= n) { + puts("0"); + continue; + } + auto mod_pow = [&mod](int x, int k) { + int ret = 1; + for( ; k > 0; x = (LL)x * x % mod, k >>= 1) + if(k & 1) + ret = (LL)ret * x % mod; + return ret; + }; + int ans = 0, m = n - k; + for(int i = 0; i <= m; ++i) { + int tmp = (LL)mod_pow(m - i, n) * ifact[m - i] % mod * ifact[i] % mod; + (ans += i & 1 ? mod - tmp : tmp) >= mod && (ans -= mod); + } + for(int i = 0; i < m; ++i) + ans = (LL)ans * (n - i) % mod; + if(m < n) + ans = (ans + ans) % mod; + printf("%d\n", ans); + } + return 0; +} \ No newline at end of file diff --git a/self_training/2020-05-10-cfe/F.cpp b/self_training/2020-05-10-cfe/F.cpp new file mode 100644 index 00000000..bc1d9c23 --- /dev/null +++ b/self_training/2020-05-10-cfe/F.cpp @@ -0,0 +1,100 @@ +#include +using namespace std; +typedef unsigned int UL; +typedef long long LL; +typedef unsigned long long ULL; +typedef double DB; +typedef long double LDB; +const int maxn = 15, maxd = 1 << 15 | 1, mod = (int)1e9 + 7, INF = 0x3f3f3f3f; +const LL INFLL = 0x3f3f3f3f3f3f3f3fLL; +const DB pi = acos((DB)-1); +int n, a[maxn], s[maxd], ord[maxd], sz[maxd][maxn]; +struct Info { + int val : 8, pos : 8, msk : 16; +} dp[maxd][maxn][maxn]; +int main() { + int T = 1; + scanf("%d", &T); + for(int Case = 1; Case <= T; ++Case) { + int n; + static int a[maxn], s[maxd], ord[maxd]; + scanf("%d", &n); + int all = (1 << n) - 1; + for(int i = 0; i < n; ++i) + scanf("%d", a + i); + s[0] = ord[0] = 0; + for(int i = 1; i <= all; ++i) { + for(int j = 0; j < n; ++j) + if((i >> j) & 1) { + s[i] = s[i ^ (1 << j)] + a[j]; + break; + } + ord[i] = i; + } + sort(ord, ord + all + 1, [&](int const &u, int const &v) { + return s[u] < s[v]; + }); + for(int i = 0; i <= all; ++i) + for(int j = 0; j < n; ++j) + sz[i][j] = 0; + for(int t = 1; t <= all; ++t) { + int msk = ord[t]; + for(int i = 0; i < n; ++i) + if(((msk >> i) & 1) && !sz[msk][i]) + dp[msk][i][sz[msk][i]++] = (Info){1, i, msk}; + int rem = all ^ msk; + for(int pre = rem; pre > 0; pre = (pre - 1) & rem) { + Info best = (Info){0, 0, msk}; + for(int i = 0; i < n; ++i) + if((msk >> i) & 1) { + int &tot = sz[msk | pre][i]; + Info *F = dp[msk | pre][i]; + if(s[best.msk] < s[msk] && (!tot || F[tot - 1].val <= best.val)) { + tot -= tot > 0 && s[F[tot - 1].msk] == s[msk]; + F[tot++] = (Info){best.val + 1, best.pos, msk}; + } + } else { + int &tot = sz[pre][i]; + Info *F = dp[pre][i]; + int idx = tot > 0 && s[F[tot - 1].msk] == s[msk] ? tot - 2 : tot - 1; + if(idx >= 0 && best.val < F[idx].val) { + best = F[idx]; + best.pos = i; + } + } + } + } + int pos = 0, rem = all; + Info best = (Info){1, 0, all}; + static int idx[maxn]; + for(int i = 0; i < n; ++i) { + int &tot = sz[all][i]; + Info *F = dp[all][i]; + if(tot > 0 && best.val < F[tot - 1].val) { + best = F[tot - 1]; + pos = i; + } + idx[i] = i; + } + printf("%d\n", n - best.val); + while(rem > 0) { + int msk = best.msk; + for(int i = 0; i < n; ++i) + if(i != pos && ((msk >> i) & 1)) { + printf("%d %d\n", idx[i] + 1, idx[pos] + 1); + for(int j = i + 1; j < n; ++j) + --idx[j]; + } + pos = best.pos; + rem ^= msk; + if(!rem) + break; + int &tot = sz[rem][pos]; + Info *F = dp[rem][pos]; + for( ; tot > 0 && s[F[tot - 1].msk] >= s[msk]; --tot); + assert(tot > 0); + best = F[tot - 1]; + } + } + return 0; +} \ No newline at end of file diff --git a/self_training/2020-05-17-abc/A.cpp b/self_training/2020-05-17-abc/A.cpp new file mode 100644 index 00000000..34d534c2 --- /dev/null +++ b/self_training/2020-05-17-abc/A.cpp @@ -0,0 +1,14 @@ +#include +using namespace std; +const char *pat = "hpb"; +const int pos[] = {1, 1, 0, 2, 0, 0, 1, 0, 1, 0}; +int main() { + int T = 1; + // scanf("%d", &T); + for(int Case = 1; Case <= T; ++Case) { + int n; + scanf("%d", &n); + printf("%con\n", pat[pos[n % 10]]); + } + return 0; +} \ No newline at end of file diff --git a/self_training/2020-05-17-abc/B.cpp b/self_training/2020-05-17-abc/B.cpp new file mode 100644 index 00000000..88b15ab8 --- /dev/null +++ b/self_training/2020-05-17-abc/B.cpp @@ -0,0 +1,23 @@ +#include +using namespace std; +typedef long long LL; +typedef double DB; +const int maxn = (int)2e5 + 1; +const DB pi = acos((DB)-1); +int main() { + int T = 1; + // scanf("%d", &T); + for(int Case = 1; Case <= T; ++Case) { + int lim; + static char buf[maxn]; + scanf("%d%s", &lim, buf); + int len = strlen(buf); + if(len > lim) { + buf[lim] = buf[lim + 1] = buf[lim + 2] = '.'; + len = lim + 3; + buf[len] = '\0'; + } + printf("%s\n", buf); + } + return 0; +} \ No newline at end of file diff --git a/self_training/2020-05-17-abc/C.cpp b/self_training/2020-05-17-abc/C.cpp new file mode 100644 index 00000000..fd84b8db --- /dev/null +++ b/self_training/2020-05-17-abc/C.cpp @@ -0,0 +1,22 @@ +#include +using namespace std; +typedef long long LL; +typedef double DB; +const int maxn = (int)2e5 + 1; +const DB pi = acos((DB)-1); +int main() { + int T = 1; + // scanf("%d", &T); + for(int Case = 1; Case <= T; ++Case) { + int a, b, hh, mm; + scanf("%d%d%d%d", &a, &b, &hh, &mm); + DB x1 = a * cos(2 * pi / 720 * (hh * 60 + mm)); + DB y1 = a * sin(2 * pi / 720 * (hh * 60 + mm)); + DB x2 = b * cos(2 * pi / 60 * mm); + DB y2 = b * sin(2 * pi / 60 * mm); +// printf("(%f, %f) (%f, %f)\n", x1, y1, x2, y2); + DB dis = sqrtl(max((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2), (DB)0)); + printf("%.20f\n", (double)dis); + } + return 0; +} \ No newline at end of file diff --git a/self_training/2020-05-17-abc/D.cpp b/self_training/2020-05-17-abc/D.cpp new file mode 100644 index 00000000..38f6f1a5 --- /dev/null +++ b/self_training/2020-05-17-abc/D.cpp @@ -0,0 +1,42 @@ +#include +using namespace std; +const int maxn = (int)1e5 + 1; +int main() { + int T = 1; + // scanf("%d", &T); + for(int Case = 1; Case <= T; ++Case) { + int n, m; + static vector e[maxn]; + static int que[maxn], par[maxn]; + scanf("%d%d", &n, &m); + for(int i = 1; i <= n; ++i) { + vector().swap(e[i]); + par[i] = 0; + } + par[1] = 1; + for(int i = 1; i <= m; ++i) { + int u, v; + scanf("%d%d", &u, &v); + e[u].push_back(v); + e[v].push_back(u); + } + int tot = 1; + que[1] = 1; + for(int i = 1; i <= tot; ++i) { + int u = que[i]; + for(int v : e[u]) + if(!par[v]) { + par[v] = u; + que[++tot] = v; + } + } + if(tot < n) { + puts("No"); + } else { + puts("Yes"); + for(int i = 2; i <= n; ++i) + printf("%d\n", par[i]); + } + } + return 0; +} \ No newline at end of file diff --git a/self_training/2020-05-17-abc/E.cpp b/self_training/2020-05-17-abc/E.cpp new file mode 100644 index 00000000..695230be --- /dev/null +++ b/self_training/2020-05-17-abc/E.cpp @@ -0,0 +1,51 @@ +#include +using namespace std; +typedef long long LL; +const int maxn = (int)2e5 + 1, mod = (int)1e9 + 7; +int main() { + int T = 1; + // scanf("%d", &T); + for(int Case = 1; Case <= T; ++Case) { + int n; + scanf("%d", &n); + static int pw[maxn]; + pw[0] = 1; + for(int i = 1; i <= n; ++i) + pw[i] = (pw[i - 1] << 1) % mod; + int zero = 0; + map, pair > ctr; + for(int i = 0; i < n; ++i) { + LL x, y; + scanf("%lld%lld", &x, &y); + if(!x && !y) { + ++zero; + continue; + } + LL com = __gcd(abs(x), abs(y)); + int pos = 0; + pair cur = make_pair(x / com, y / com), best = cur; + for(int j = 1; j < 4; ++j) { + cur = make_pair(-cur.second, cur.first); + if(cur < best) { + best = cur; + pos = j; + } + } + auto &cnt = ctr[best]; + if(pos & 1) { + ++cnt.first; + } else { + ++cnt.second; + } + } + int ans = 1; + for(auto &it : ctr) { + auto &jt = it.second; + int u = jt.first, v = jt.second; + ans = (LL)ans * (pw[u] + pw[v] - 1) % mod; + } + ans = (ans + mod - 1 + zero) % mod; + printf("%d\n", ans); + } + return 0; +} \ No newline at end of file diff --git a/self_training/2020-05-17-abc/F.cpp b/self_training/2020-05-17-abc/F.cpp new file mode 100644 index 00000000..66214bb9 --- /dev/null +++ b/self_training/2020-05-17-abc/F.cpp @@ -0,0 +1,109 @@ +#include +using namespace std; +typedef long long LL; +const int maxn = (int)3e3 + 9, INF = 0x3f3f3f3f; +int n, m, xtot, xque[maxn], ytot, yque[maxn]; +struct Line { + int L, R, pos; + void read() { + scanf("%d%d%d", &L, &R, &pos); + } +} er[maxn], ec[maxn]; +bool vis[maxn][maxn], banL[maxn][maxn], banR[maxn][maxn], banU[maxn][maxn], banD[maxn][maxn]; +int main() { + int T = 1; + // scanf("%d", &T); + for(int Case = 1; Case <= T; ++Case) { + scanf("%d%d", &n, &m); + xtot = ytot = 0; + xque[xtot++] = yque[ytot++] = -INF; + xque[xtot++] = yque[ytot++] = INF; + for(int i = 0; i < n; ++i) { + Line &cur = er[i]; + cur.read(); // xL xR y + xque[xtot++] = cur.L; + xque[xtot++] = cur.R; + yque[ytot++] = cur.pos; + } + for(int i = 0; i < m; ++i) { + Line &cur = ec[i]; + cur.read(); // x yL yR + swap(cur.pos, cur.L); // yR yL x + swap(cur.L, cur.R); // yL yR x + yque[ytot++] = cur.L; + yque[ytot++] = cur.R; + xque[xtot++] = cur.pos; + } + sort(xque, xque + xtot); + xtot = unique(xque, xque + xtot) - xque; + sort(yque, yque + ytot); + ytot = unique(yque, yque + ytot) - yque; + for(int i = 0; i < xtot - 1; ++i) + for(int j = 0; j < ytot - 1; ++j) { + vis[i][j] = 0; + banL[i][j] = j == 0; + banR[i][j] = j == ytot - 2; + banU[i][j] = i == 0; + banD[i][j] = i == xtot - 2; + } + for(int i = 0; i < n; ++i) { + Line &cur = er[i]; + int xL = lower_bound(xque, xque + xtot, cur.L) - xque; + int xR = lower_bound(xque, xque + xtot, cur.R) - xque; + int y = lower_bound(yque, yque + ytot, cur.pos) - yque; + for(int x = xL; x < xR; ++x) { + banL[x][y] = 1; + banR[x][y - 1] = 1; + } +// printf("ban [%d, %d) x %d\n", xL, xR, y); +// show(); + } + for(int i = 0; i < m; ++i) { + Line &cur = ec[i]; + int yL = lower_bound(yque, yque + ytot, cur.L) - yque; + int yR = lower_bound(yque, yque + ytot, cur.R) - yque; + int x = lower_bound(xque, xque + xtot, cur.pos) - xque; + for(int y = yL; y < yR; ++y) { + banU[x][y] = 1; + banD[x - 1][y] = 1; + } +// printf("ban %d x [%d, %d)\n", x, yL, yR); +// show(); + } + int sx = lower_bound(xque, xque + xtot, 0) - xque - 1; + int sy = lower_bound(yque, yque + ytot, 0) - yque - 1; + LL ans = 0; + bool inf = 0; + int tot = 0; + static pair que[maxn * maxn]; + auto append = [&](int x, int y) { + if(x < 0 || x >= xtot - 1 || y < 0 || y >= ytot - 1 || vis[x][y]) + return; + vis[x][y] = 1; + inf |= x == 0 || x == xtot - 2 || y == 0 || y == ytot - 2; + ans += (LL)(xque[x + 1] - xque[x]) * (yque[y + 1] - yque[y]); + que[tot++] = make_pair(x, y); + }; + append(sx, sy); + for(int i = 0; !inf && i < tot; ++i) { + int x, y; + tie(x, y) = que[i]; + if(!banL[x][y]) + append(x, y - 1); + if(!banR[x][y]) + append(x, y + 1); + if(!banU[x][y]) + append(x - 1, y); + if(!banD[x][y]) + append(x + 1, y); + } + if(inf) { + puts("INF"); + } else { + printf("%lld\n", ans); + } +// printf("%d %d\n", sx, sy); +// show(); + } + return 0; +} \ No newline at end of file diff --git a/self_training/2020-05-17-ks/A.cpp b/self_training/2020-05-17-ks/A.cpp new file mode 100644 index 00000000..cd624369 --- /dev/null +++ b/self_training/2020-05-17-ks/A.cpp @@ -0,0 +1,26 @@ +#include +using namespace std; +typedef long long LL; +const int maxn = (int)2e5 + 1; +int main() { + int T; + scanf("%d", &T); + for(int Case = 1; Case <= T; ++Case) { + int n, m; + static int a[maxn]; + scanf("%d%d", &n, &m); + for(int i = 0; i < n; ++i) + scanf("%d", a + i); + int ans = 0; + for(int i = 0; i <= n - m; ++i) { + if(a[i] != m) + continue; + bool chk = 1; + for(int j = 0; chk && j < m; ++j) + chk &= a[i + j] == m - j; + ans += chk; + } + printf("Case #%d: %d\n", Case, ans); + } + return 0; +} \ No newline at end of file diff --git a/self_training/2020-05-17-ks/B.cpp b/self_training/2020-05-17-ks/B.cpp new file mode 100644 index 00000000..4bea1a30 --- /dev/null +++ b/self_training/2020-05-17-ks/B.cpp @@ -0,0 +1,58 @@ +#include +using namespace std; +typedef long long LL; +const int maxn = 31, maxd = 26; +int main() { + int T; + scanf("%d", &T); + for(int Case = 1; Case <= T; ++Case) { + int r, c; + static char buf[maxn][maxn]; + static bool vis[maxd + 1], e[maxd + 1][maxd + 1]; + static int deg[maxd + 1]; + for(int i = 0; i < maxd; ++i) { + vis[i] = 0; + for(int j = 0; j < maxd; ++j) + e[i][j] = 0; + deg[i] = 0; + } + scanf("%d%d", &r, &c); + int cnt = 0; + for(int i = 0; i < r; ++i) { + scanf("%s", buf[i]); + for(int j = 0; j < c; ++j) { + int u = buf[i][j] - 'A'; + cnt += !vis[u]; + vis[u] = 1; + } + } + for(int i = 0; i + 1 < r; ++i) + for(int j = 0; j < c; ++j) + if(buf[i][j] != buf[i + 1][j]) { + int u = buf[i + 1][j] - 'A'; + int v = buf[i][j] - 'A'; + if(e[u][v]) + continue; + e[u][v] = 1; + ++deg[v]; + } + int tot = 0; + static char out[maxd + 1]; + for(int i = 0; i < maxd; ++i) + if(vis[i] && !deg[i]) + out[tot++] = 'A' + i; + for(int i = 0; i < tot; ++i) { + int u = out[i] - 'A'; + for(int v = 0; v < maxd; ++v) + if(e[u][v] && !(--deg[v])) + out[tot++] = 'A' + v; + } + if(tot < cnt) { + printf("Case #%d: -1\n", Case); + } else { + out[tot] = '\0'; + printf("Case #%d: %s\n", Case, out); + } + } + return 0; +} \ No newline at end of file diff --git a/self_training/2020-05-17-ks/C.cpp b/self_training/2020-05-17-ks/C.cpp new file mode 100644 index 00000000..03fc7514 --- /dev/null +++ b/self_training/2020-05-17-ks/C.cpp @@ -0,0 +1,31 @@ +#include +using namespace std; +typedef long long LL; +const int maxn = (int)1e5 + 1; +int main() { + int T; + scanf("%d", &T); + for(int Case = 1; Case <= T; ++Case) { + int n; + static int a[maxn]; + scanf("%d", &n); + int low = a[0] = 0; + LL ans = 0; + unordered_map ctr; + ctr.reserve(n << 1 | 1); + ++ctr[0]; + for(int i = 1; i <= n; ++i) { + scanf("%d", a + i); + a[i] += a[i - 1]; + for(int j = 0, k = 0; k <= a[i] - low; k += (j++) << 1 | 1) { + auto it = ctr.find(a[i] - k); + if(it != ctr.end()) + ans += it -> second; + } + ++ctr[a[i]]; + low = min(low, a[i]); + } + printf("Case #%d: %lld\n", Case, ans); + } + return 0; +} \ No newline at end of file diff --git a/self_training/2020-05-17-ks/D.cpp b/self_training/2020-05-17-ks/D.cpp new file mode 100644 index 00000000..a6b24a0f --- /dev/null +++ b/self_training/2020-05-17-ks/D.cpp @@ -0,0 +1,75 @@ +#include +using namespace std; +typedef long long LL; +const int maxn = (int)2e5 + 1; +struct Segment { + int cnt; + LL sum[2]; + void merge(Segment const &lft, Segment const &rht) { + cnt = lft.cnt + rht.cnt; + sum[0] = lft.sum[0] + (lft.cnt & 1 ? -1 : 1) * rht.sum[0]; + sum[1] = lft.sum[1] + (lft.cnt & 1 ? -1 : 1) * (lft.cnt * rht.sum[0] + rht.sum[1]); + } +} seg[maxn << 1 | 1]; +inline int seg_idx(int L, int R) { + return (L + R) | (L < R); +} +void seg_build(int L, int R) { + if(L < R) { + int M = (L + R) >> 1; + seg_build(L, M); + seg_build(M + 1, R); + seg[seg_idx(L, R)].merge(seg[seg_idx(L, M)], seg[seg_idx(M + 1, R)]); + } else { + int x; + scanf("%d", &x); + seg[seg_idx(L, R)] = (Segment){1, (LL)x, (LL)x}; + } +} +void seg_upd(int L, int R, int x, int v) { + if(L < R) { + int M = (L + R) >> 1; + if(x <= M) + seg_upd(L, M, x, v); + else + seg_upd(M + 1, R, x, v); + seg[seg_idx(L, R)].merge(seg[seg_idx(L, M)], seg[seg_idx(M + 1, R)]); + } else { + seg[seg_idx(L, R)] = (Segment){1, (LL)v, (LL)v}; + } +} +Segment seg_que(int L, int R, int l, int r) { + if(l <= L && R <= r) + return seg[seg_idx(L, R)]; + int M = (L + R) >> 1; + if(r <= M) + return seg_que(L, M, l, r); + if(l > M) + return seg_que(M + 1, R, l, r); + Segment ret; + ret.merge(seg_que(L, M, l, r), seg_que(M + 1, R, l, r)); + return ret; +} +int main() { + int T; + scanf("%d", &T); + for(int Case = 1; Case <= T; ++Case) { + int n, m; + scanf("%d%d", &n, &m); + seg_build(1, n); + LL ans = 0; + while(m--) { + char op[3]; + int u, v; + scanf("%s%d%d", op, &u, &v); + if(op[0] == 'U') { + seg_upd(1, n, u, v); + } else { + Segment ret = seg_que(1, n, u, v); + ans += ret.sum[1]; + } + } + printf("Case #%d: %lld\n", Case, ans); + } + return 0; +} \ No newline at end of file diff --git a/self_training/2020-05-23-agc/A.cpp b/self_training/2020-05-23-agc/A.cpp new file mode 100644 index 00000000..064a6f75 --- /dev/null +++ b/self_training/2020-05-23-agc/A.cpp @@ -0,0 +1,37 @@ +#include +using namespace std; +typedef long long LL; +const LL INF = ~0ULL >> 1; +const int mul[3] = {2, 3, 5}; +int cost[5]; +unordered_map Hash; +LL dfs(LL N) { + auto it = Hash.find(N); + if(it != Hash.end()) + return it -> second; + LL ret = INF; + for(int i = 0; i < 3; ++i) { + LL M = N / mul[i], R = N % mul[i]; + ret = min(ret, dfs(M) + cost[i] + R * cost[3]); + if(R > 0 && M + 1 < N) + ret = min(ret, dfs(M + 1) + cost[i] + (mul[i] - R) * cost[3]); + } + if(ret / cost[3] >= N) + ret = min(ret, N * cost[3]); + return Hash[N] = ret; +} +int main() { + int T = 1; + scanf("%d", &T); + for(int Case = 1; Case <= T; ++Case) { + LL N; + scanf("%lld", &N); + for(int i = 0; i < 4; ++i) + scanf("%d", cost + i); + Hash.clear(); + Hash[0] = 0; + Hash[1] = cost[3]; + printf("%lld\n", dfs(N)); + } + return 0; +} \ No newline at end of file diff --git a/self_training/2020-05-23-agc/B.cpp b/self_training/2020-05-23-agc/B.cpp new file mode 100644 index 00000000..db0670e7 --- /dev/null +++ b/self_training/2020-05-23-agc/B.cpp @@ -0,0 +1,52 @@ +#include +using namespace std; +const int maxn = 501, maxm = maxn * maxn; +const int d[][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}}; +int n, p[maxm], dis[maxm]; +bool vis[maxm]; +inline int idx(int x, int y) { + return x * n + y; +} +inline void rdx(int id, int &x, int &y) { + x = id / n; + y = id % n; +} +int main() { + scanf("%d", &n); + int m = n * n; + for(int i = 0; i < m; ++i) { + scanf("%d", p + i); + --p[i]; + } + for(int i = 0, id = 0; i < n; ++i) + for(int j = 0; j < n; ++j, ++id) { + dis[id] = min(min(i + 1, n - i), min(j + 1, n - j)); + vis[id] = 0; + } + int ans = 0; + for(int i = 0; i < m; ++i) { + vis[p[i]] = 1; + ans += --dis[p[i]]; + queue Q; + Q.push(p[i]); +// printf("force dis (%d, %d) <- %d\n", p[i] / n, p[i] % n, dis[p[i]]); + while(!Q.empty()) { + int u = Q.front(), ux, uy; + Q.pop(); + rdx(u, ux, uy); + for(int k = 0; k < 4; ++k) { + int vx = ux + d[k][0], vy = uy + d[k][1]; + if(vx < 0 || vx >= n || vy < 0 || vy >= n) + continue; + int v = idx(vx, vy), tmp = dis[u] + !vis[v]; + if(dis[v] > tmp) { +// printf("dis (%d, %d) <- %d\n", vx, vy, tmp); + dis[v] = tmp; + Q.push(v); + } + } + } + } + printf("%d\n", ans); + return 0; +} \ No newline at end of file diff --git a/self_training/2020-05-23-agc/C.cpp b/self_training/2020-05-23-agc/C.cpp new file mode 100644 index 00000000..0714e07e --- /dev/null +++ b/self_training/2020-05-23-agc/C.cpp @@ -0,0 +1,58 @@ +#include +using namespace std; +const int maxs = 797161, maxl = (int)2e5 + 1; +int n, tot, out[maxs]; +struct TrieNode { + int idx, tag, ch[3]; +} p[maxs]; +int main() { + static char buf[maxl]; + scanf("%d%s", &n, buf); + ++tot; + int sta = 0, pw = 1; + for(int i = 0; i < n; ++i, sta += pw, pw *= 3) + for(int j = 0; j < pw; ++j) { + TrieNode &cur = p[sta + j]; + for(int k = 0, adt = 0; k < 3; ++k, adt += pw) { + p[tot].idx = cur.idx + adt; + cur.ch[k] = tot++; + } + } + auto expand = [&](int rt) { + TrieNode &cur = p[rt]; + if(cur.tag) { + swap(cur.ch[1], cur.ch[2]); + for(int i = 0; i < 3; ++i) + p[cur.ch[i]].tag ^= 1; + cur.tag = 0; + } + }; + for(int i = 0; buf[i]; ++i) { + if(buf[i] == 'S') { + p[0].tag ^= 1; + continue; + } + for(int j = 0, rt = 0; j < n; ++j) { + expand(rt); + TrieNode &cur = p[rt]; + rotate(cur.ch, cur.ch + 2, cur.ch + 3); + rt = cur.ch[0]; + } + } + function dfs = [&](int dep, int rt, int pw, int val) { + TrieNode &cur = p[rt]; + if(dep == n) { + out[cur.idx] = val; + } else { + expand(rt); + int npw = pw * 3; + dfs(dep + 1, cur.ch[0], npw, val); + dfs(dep + 1, cur.ch[1], npw, val += pw); + dfs(dep + 1, cur.ch[2], npw, val += pw); + } + }; + dfs(0, 0, 1, 0); + for(int i = 0; i < pw; ++i) + printf("%d%c", out[i], " \n"[i == pw - 1]); + return 0; +} \ No newline at end of file diff --git a/self_training/2020-05-23-agc/D.cpp b/self_training/2020-05-23-agc/D.cpp new file mode 100644 index 00000000..c889e2fa --- /dev/null +++ b/self_training/2020-05-23-agc/D.cpp @@ -0,0 +1,101 @@ +#include +using namespace std; +const int maxn = 128; + +int ask(char buf[]) { + printf("? %s\n", buf); + fflush(stdout); + int ret; + scanf("%d", &ret); + return ret; +} +void close(char buf[]) { + printf("! %s\n", buf); + fflush(stdout); + exit(0); +} + +int tot, sum, pL[maxn + 1], pR[maxn + 1]; +char buf[maxn + 1], out[maxn + 1]; +void solve(int L, int R) { // merge -> [pL[L], pR[L]] + if(L == R) + return; + int M = (L + R) >> 1; + solve(L, M); + solve(M + 1, R); + int lft = pR[L] - pL[L] + 1; + int rht = pR[M + 1] - pL[M + 1] + 1; + int pos = 0, i, j; + static char tmp[maxn + 1]; + for(i = 0, j = 0; i < rht; ++i) { + for( ; j < lft; ++j) { + int len = 0; + for(int k = 0; k < pos; ++k) + buf[len++] = tmp[k]; + buf[len++] = out[pL[M + 1] + i]; + for(int k = j; k < lft; ++k) + buf[len++] = out[pL[L] + k]; + buf[len] = '\0'; + int ret = ask(buf); + if(ret == sum - len) + break; + tmp[pos++] = out[pL[L] + j]; + } + tmp[pos++] = out[pL[M + 1] + i]; + } + for( ; j < lft; ++j) + tmp[pos++] = out[pL[L] + j]; + pR[L] = pR[M + 1]; + for(int i = 0; i < pos; ++i) + out[pL[L] + i] = tmp[i]; +} + +int main() { + sum = 0; + for(char ch = 'a'; sum < maxn && ch <= 'z'; ++ch) { + for(int i = 0; i < maxn; ++i) + buf[i] = ch; + buf[maxn] = '\0'; + int cnt = maxn - ask(buf); + if(!cnt) + continue; + pL[tot] = tot ? pR[tot - 1] + 1 : 0; + pR[tot] = pL[tot] + cnt - 1; + for(int i = pL[tot]; i <= pR[tot]; ++i) + out[i] = ch; + ++tot; + sum += cnt; + } + for(char ch = 'A'; sum < maxn && ch <= 'Z'; ++ch) { + for(int i = 0; i < maxn; ++i) + buf[i] = ch; + buf[maxn] = '\0'; + int cnt = maxn - ask(buf); + if(!cnt) + continue; + pL[tot] = tot ? pR[tot - 1] + 1 : 0; + pR[tot] = pL[tot] + cnt - 1; + for(int i = pL[tot]; i <= pR[tot]; ++i) + out[i] = ch; + ++tot; + sum += cnt; + } + for(char ch = '0'; sum < maxn && ch <= '9'; ++ch) { + for(int i = 0; i < maxn; ++i) + buf[i] = ch; + buf[maxn] = '\0'; + int cnt = maxn - ask(buf); + if(!cnt) + continue; + pL[tot] = tot ? pR[tot - 1] + 1 : 0; + pR[tot] = pL[tot] + cnt - 1; + for(int i = pL[tot]; i <= pR[tot]; ++i) + out[i] = ch; + ++tot; + sum += cnt; + } + solve(0, tot - 1); + out[sum] = '\0'; + close(out); + return 0; +} \ No newline at end of file diff --git a/self_training/2020-05-23-agc/E.cpp b/self_training/2020-05-23-agc/E.cpp new file mode 100644 index 00000000..d7289844 --- /dev/null +++ b/self_training/2020-05-23-agc/E.cpp @@ -0,0 +1,51 @@ +#include +using namespace std; +typedef long long LL; +typedef double DB; +const int maxn = (int)2e5 + 1; +struct Point { + LL x, y; + bool operator < (Point const &t) const { + return x != t.x ? (x < t.x) : (y < t.y); + } + Point operator - (Point const &t) const { + return (Point){x - t.x, y - t.y}; + } + LL det(Point const &t) const { + return x * t.y - y * t.x; + } +} p[maxn]; +int n, b[maxn], m, stk[maxn]; +LL a[maxn], c[maxn]; +int main() { + scanf("%d", &n); + int pos = 0; + for(int i = 0; i < n; ++i) { + scanf("%lld", a + i); + if(a[pos] < a[i]) + pos = i; + } + for(int i = 0; i < n; ++i) + scanf("%d", b + i); + rotate(a, a + pos, a + n); + rotate(b, b + pos, b + n); + a[n] = a[0]; + for(int i = 0; i <= n; ++i) { + if(i > 1) + c[i] = (b[i - 1] + c[i - 1]) * 2 - c[i - 2]; + p[i] = (Point){i, a[i] - c[i]}; + for( ; m > 1 && (p[stk[m - 1]] - p[stk[m - 2]]).det(p[i] - p[stk[m - 1]]) > 0; --m); + stk[m++] = i; + } + DB ans = 0; + for(int i = 0, j = 0; i < n; ++i) { + for( ; stk[j] < i; ++j); + if(stk[j] == i) { + ans += a[i]; + } else { + ans += ((i - stk[j - 1]) * p[stk[j]].y + (stk[j] - i) * p[stk[j - 1]].y) / (DB)(stk[j] - stk[j - 1]) + c[i]; + } + } + printf("%.20f\n", ans / n); + return 0; +} \ No newline at end of file diff --git a/self_training/2020-05-23-agc/F.cpp b/self_training/2020-05-23-agc/F.cpp new file mode 100644 index 00000000..d96e1f68 --- /dev/null +++ b/self_training/2020-05-23-agc/F.cpp @@ -0,0 +1,158 @@ +#include +using namespace std; +typedef long long LL; +const int maxm = 6, maxd = 1 << 5 | 1, maxs = (int)1e3 + 1; +int dfs(int n, int tot, int upp, int sta, vector &msk) { + if((int)msk.size() == tot) { + bool chk = 1; + vector perm(n); + iota(perm.begin(), perm.end(), 0); + static int tim = 0, vis[maxd] = {}; + ++tim; + for(int x : msk) + vis[x] = tim; + while(chk && next_permutation(perm.begin(), perm.end())) { + bool same = 1; + for(int x : msk) { + int y = 0; + for(int i = 0; i < n; ++i, x >>= 1) + if(x & 1) + y |= 1 << perm[i]; + if(vis[y] != tim) { + same = 0; + break; + } + } + chk &= !same; + } + if(chk) { + // for(int x : best) { + // for(int j = 0; j < n; ++j, x >>= 1) + // printf("%d", x & 1); + // puts(""); + // } + // puts(""); + } + return chk; + } + int ret = 0; + msk.push_back(0); + for( ; sta < (1 << n) && ret < upp; ++sta) { + msk.back() = sta; + ret += dfs(n, tot, upp, sta + 1, msk); + } + msk.pop_back(); + return ret < upp ? ret : upp; +} +int solve1(LL n, int m) { + if(n < m) + return solve1(m, n); + if(n > (1LL << m) - n) + return solve1((1LL << m) - n, m); + if(m > 5) + return maxs; + int prd = 1; + for(int i = 1; i <= m; ++i) + prd *= i; + int upp = (maxs - 1) * prd + 1; + vector msk; + int ret = dfs(m, n, upp, 0, msk); + return ret < upp ? ret / prd : maxs; +} +set > Hash; +int dfs2(int n, int tot, int sta, vector &msk, bool sp) { + if((int)msk.size() == tot) { + if(!sp) + return 0; + bool chk = 1; + vector perm(n), best = msk; + iota(perm.begin(), perm.end(), 0); + while(chk && next_permutation(perm.begin(), perm.end())) { + vector tmp; + for(int x : msk) { + int y = 0; + for(int i = 0; i < n; ++i, x >>= 1) + if(x & 1) + y |= 1 << perm[i]; + tmp.push_back(y); + } + sort(tmp.begin(), tmp.end()); + chk &= msk != tmp; + best = min(best, tmp); + } + chk &= !Hash.count(best); + if(chk) { + Hash.insert(best); + // for(int x : best) { + // for(int j = 0; j < n; ++j, x >>= 1) + // printf("%d", x & 1); + // puts(""); + // } + // puts(""); + } + return chk; + } + int ret = 0; + msk.push_back(0); + for( ; sta < (1 << n) && ret < maxs; ++sta) { + msk.back() = sta; + ret += dfs2(n, tot, sta, msk, sp || ((int)msk.size() > 2 && msk[msk.size() - 2] == sta)); + } + msk.pop_back(); + return ret < maxs ? ret : maxs; +} +int solve2(int n, int m) { + // printf("dangerous (%d, %d)\n", n, m); + if(n == 4) { + /* + 1100 + 1100 + 1010 + */ + return 1; + } + if(n == 7) { + /* + 1110000 + 1110000 + 1001100 + 1101010 + + 1110000 + 1101100 + 1101100 + 1011010 + */ + return 2; + } + if(n == 8) { + return 0; + } + // unreachable + Hash.clear(); + vector msk; + return dfs2(n, m, 0, msk, 0); +} +int main() { + LL n; + int m = 0; + scanf("%lld", &n); + vector dp = {0}; + for(int i = 1; i < n; ++i) { + for(int j = 0; j < i; ++j) + if((1 << j) - i >= dp[j]) { + dp.push_back(j); + break; + } + // assert((int)dp.size() == i + 1); + if((1LL << i) - n >= dp.back()) { + m = i; + break; + } + } + int ans = solve1(n, m); + if(ans < maxs && n * 2 <= (1LL << m)) + ans += solve2(n, m); + printf("%d\n", ans < maxs ? ans : -1); + return 0; +} \ No newline at end of file diff --git a/self_training/2020-06-18-cf3/A.cpp b/self_training/2020-06-18-cf3/A.cpp new file mode 100644 index 00000000..f0b28153 --- /dev/null +++ b/self_training/2020-06-18-cf3/A.cpp @@ -0,0 +1,25 @@ +#include +using namespace std; +typedef long long LL; +typedef double DB; +const int maxn = (int)5e5 + 1; +const int mod = (int)1e9 + 7; + + +int main() { + int T = 1; + scanf("%d", &T); + for(int Case = 1; Case <= T; ++Case) { + static char buf[maxn]; + scanf("%s", buf); + int n = strlen(buf), m = 0; + for(int i = 0; i < n; i += 2) + buf[m++] = buf[i]; + buf[m++] = buf[n - 1]; + buf[m] = '\0'; + puts(buf); + // printf("Case #%d:"); + } + + return 0; +} \ No newline at end of file diff --git a/self_training/2020-06-18-cf3/B.cpp b/self_training/2020-06-18-cf3/B.cpp new file mode 100644 index 00000000..12321283 --- /dev/null +++ b/self_training/2020-06-18-cf3/B.cpp @@ -0,0 +1,26 @@ +#include +using namespace std; +typedef long long LL; +typedef double DB; +const int maxn = (int)5e5 + 1; +const int mod = (int)1e9 + 7; + + +int main() { + int T = 1; + scanf("%d", &T); + for(int Case = 1; Case <= T; ++Case) { + int n, ctr[2] = {}; + static int arr[maxn]; + scanf("%d", &n); + for(int i = 0; i < n; ++i) { + scanf("%d", arr + i); + if((arr[i] - i) & 1) + ++ctr[i & 1]; + } + printf("%d\n", ctr[0] != ctr[1] ? -1 : ctr[0]); + // printf("Case #%d:"); + } + + return 0; +} \ No newline at end of file diff --git a/self_training/2020-06-18-cf3/C.cpp b/self_training/2020-06-18-cf3/C.cpp new file mode 100644 index 00000000..95ebbca6 --- /dev/null +++ b/self_training/2020-06-18-cf3/C.cpp @@ -0,0 +1,42 @@ +#include +using namespace std; +typedef long long LL; +typedef double DB; +const int maxn = (int)5e5 + 1; +const int mod = (int)1e9 + 7; + + +int main() { + int T = 1; + scanf("%d", &T); + for(int Case = 1; Case <= T; ++Case) { + int n, k; + static char buf[maxn]; + scanf("%d%d%s", &n, &k, buf); + int las = -1, ans = 0; + auto calc = [&](int len, bool sp = false) { + if(sp) { + // x * (k + 1) <= len + ans += len / (k + 1); + } else if(len >= k) { + // x * (k + 1) + k <= len + ans += (len - k) / (k + 1); + } + }; + for(int i = 0; i < n; ++i) + if(buf[i] == '1') { + calc(i - las - 1, las == -1); + las = i; + } + if(las == -1) { + // (x - 1) * (k + 1) + 1 <= n + ans += (n - 1) / (k + 1) + 1; + } else { + calc(n - las - 1, 1); + } + printf("%d\n", ans); + // printf("Case #%d:"); + } + + return 0; +} \ No newline at end of file diff --git a/self_training/2020-06-18-cf3/D.cpp b/self_training/2020-06-18-cf3/D.cpp new file mode 100644 index 00000000..f0296b6d --- /dev/null +++ b/self_training/2020-06-18-cf3/D.cpp @@ -0,0 +1,60 @@ +#include +using namespace std; +typedef long long LL; +typedef double DB; +const int maxn = 50 + 1, maxd = 26 + 1; +const int mod = (int)1e9 + 7; + + +int main() { + int T = 1; + scanf("%d", &T); + for(int Case = 1; Case <= T; ++Case) { + int ctr[maxd] = {}; + char buf[maxn]; + scanf("%s", buf); + for(int i = 0; buf[i]; ++i) + ++ctr[(int)(buf[i] - 'a')]; + + int m, dis[maxn]; + scanf("%d", &m); + for(int i = 0; i < m; ++i) + scanf("%d", dis + i); + + int rem = m; + bool vis[maxn] = {}; + vector > seq; + while(rem > 0) { + vector pos; + for(int i = 0; i < m; ++i) + if(!vis[i] && !dis[i]) { + --rem; + vis[i] = 1; + pos.push_back(i); + } + assert(!pos.empty()); + seq.push_back(pos); + for(int i = 0; i < m; ++i) + if(!vis[i]) + for(int j : pos) + dis[i] -= abs(i - j); + } + + reverse(seq.begin(), seq.end()); + + int pos = 0; + for(auto &it : seq) { + int sz = (int)it.size(); + for( ; pos < maxd && ctr[pos] < sz; ++pos); + assert(pos < maxd); + for(int x : it) + buf[x] = 'a' + pos; + ++pos; + } + buf[m] = '\0'; + puts(buf); + // printf("Case #%d:"); + } + + return 0; +} \ No newline at end of file diff --git a/self_training/2020-06-18-cf3/E.cpp b/self_training/2020-06-18-cf3/E.cpp new file mode 100644 index 00000000..fa389fa1 --- /dev/null +++ b/self_training/2020-06-18-cf3/E.cpp @@ -0,0 +1,32 @@ +#include +using namespace std; +typedef long long LL; +typedef double DB; +const int maxn = (int)2e3 + 1, maxd = 26 + 1; +const int mod = (int)1e9 + 7; + + +int main() { + int T = 1; + scanf("%d", &T); + for(int Case = 1; Case <= T; ++Case) { + int n, k, ctr[maxd] = {}; + static char buf[maxn]; + scanf("%d%d%s", &n, &k, buf); + for(int i = 0; i < n; ++i) + ++ctr[(int)(buf[i] - 'a')]; + for(int i = n; i >= 1; --i) { + int com = i / __gcd(i, k); + int upp = 0; + for(int j = 0; j < maxd; ++j) + upp += ctr[j] / com * com; + if(i <= upp) { + printf("%d\n", i); + break; + } + } + // printf("Case #%d:"); + } + + return 0; +} \ No newline at end of file diff --git a/self_training/2020-06-18-cf3/F.cpp b/self_training/2020-06-18-cf3/F.cpp new file mode 100644 index 00000000..6bacf8e0 --- /dev/null +++ b/self_training/2020-06-18-cf3/F.cpp @@ -0,0 +1,72 @@ +#include +using namespace std; +typedef long long LL; +typedef double DB; +const int maxn = (int)2e5 + 1; +const int mod = (int)1e9 + 7; + +int main() { + int T = 1; + scanf("%d", &T); + for(int Case = 1; Case <= T; ++Case) { + int n; + static int a[maxn], ord[maxn]; + scanf("%d", &n); + for(int i = 0; i < n; ++i) { + scanf("%d", a + i); + ord[i] = i; + } + sort(ord, ord + n, [&](int const &u, int const &v) { + return a[u] < a[v] || (a[u] == a[v] && u < v); + }); + int tot = 0; + static pair seg[maxn]; + for(int i = 0; i < n; ++i) + if(!i || a[ord[i - 1]] != a[ord[i]]) { + seg[tot++] = {i, i}; + } else { + seg[tot - 1].second = i; + } + static int addL[maxn], addR[maxn]; + for(int i = 0; i < tot; ++i) { + int pL, pR, qL, qR; + tie(pL, pR) = seg[i]; + if(i > 0) { + tie(qL, qR) = seg[i - 1]; + int val = ord[pL], pos = lower_bound(ord + qL, ord + qR + 1, val) - ord; + addL[i] = pos - qL; + } else { + addL[i] = 0; + } + if(i + 1 < tot) { + tie(qL, qR) = seg[i + 1]; + int val = ord[pR], pos = lower_bound(ord + qL, ord + qR + 1, val) - ord; + addR[i] = qR - pos + 1; + } else { + addR[i] = 0; + } + } + int ans = n, cnt = 0; + for(int i = 0, j = 0; i < tot; ++i) { + for( ; j < tot && (i == j || ord[seg[j - 1].second] < ord[seg[j].first]); ++j) + cnt += seg[j].second - seg[j].first + 1; + ans = min(ans, n - cnt - addL[i] - addR[j - 1]); + cnt -= seg[i].second - seg[i].first + 1; + } + for(int i = 1; i < tot; ++i) { + int pL, pR, qL, qR; + tie(pL, pR) = seg[i]; + tie(qL, qR) = seg[i - 1]; + for(int j = pL; j <= pR; ++j) { + // [qL, pos-1] + [j, pR] + int val = ord[j], pos = lower_bound(ord + qL, ord + qR + 1, val) - ord; + int cnt = (pos - qL) + (pR - j + 1); + ans = min(ans, n - cnt); + } + } + printf("%d\n", ans); + // printf("Case #%d:"); + } + + return 0; +} \ No newline at end of file diff --git a/self_training/2020-07-19-astar/A.cpp b/self_training/2020-07-19-astar/A.cpp new file mode 100644 index 00000000..ad5ad624 --- /dev/null +++ b/self_training/2020-07-19-astar/A.cpp @@ -0,0 +1,21 @@ +#include +using namespace std; +const int maxv = 101, maxn = (int)1e4 + maxv + 10; +inline void upd_min(int &x, int y) { + x > y && (x = y); +} +int main() { + int T; + scanf("%d", &T); + while(T--) { + int n, m, ans = INT_MAX; + scanf("%d%d", &n, &m); + while(n--) { + int x, y; + scanf("%d%d", &x, &y); + upd_min(ans, ((m - 1) / x + 1) * y); + } + printf("%d\n", ans); + } + return 0; +} \ No newline at end of file diff --git a/self_training/2020-07-19-astar/B.cpp b/self_training/2020-07-19-astar/B.cpp new file mode 100644 index 00000000..b7babbf5 --- /dev/null +++ b/self_training/2020-07-19-astar/B.cpp @@ -0,0 +1,28 @@ +#include +using namespace std; +const int maxm = 5, maxn = (int)4e2 + 1, maxd = 10; +const int data[maxd + 1][2] = { + {60, 10}, {62, 17}, {65, 20}, {67, 23}, {70, 27}, + {75, 30}, {80, 33}, {85, 37}, {90, 40}, {95, 43} +}; +int dp[maxm][maxn]; +inline void upd_max(int &x, int y) { + x < y && (x = y); +} +int main() { + for(int i = 1; i < maxm; ++i) + for(int j = 0; j < maxd; ++j) { + int c = data[j][0], w = data[j][1]; + for(int k = c; k < maxn; ++k) + upd_max(dp[i][k], dp[i - 1][k - c] + w); + } + int T; + scanf("%d", &T); + while(T--) { + int n; + scanf("%d", &n); + int ans = dp[maxm - 1][n]; + printf("%d.%d\n", ans / 10, ans % 10); + } + return 0; +} \ No newline at end of file diff --git a/self_training/2020-07-19-astar/C.cpp b/self_training/2020-07-19-astar/C.cpp new file mode 100644 index 00000000..fa5460e4 --- /dev/null +++ b/self_training/2020-07-19-astar/C.cpp @@ -0,0 +1,25 @@ +#include +using namespace std; +const int maxn = (int)1e3 + 1; +int dp[maxn][maxn]; +inline void upd_max(int &x, int y) { + x < y && (x = y); +} +int main() { + for(int i = 1; i < maxn; ++i) + for(int j = 1; j < maxn; ++j) { + if(i > 1) + upd_max(dp[i][j], dp[i - 1][j]); + if(j > 1) + upd_max(dp[i][j], dp[i][j - 1]); + dp[i][j] += __gcd(i, j) == 1; + } + int T; + scanf("%d", &T); + while(T--) { + int n, m; + scanf("%d%d", &n, &m); + printf("%d\n", dp[n][m]); + } + return 0; +} \ No newline at end of file diff --git a/self_training/2020-07-19-astar/D.cpp b/self_training/2020-07-19-astar/D.cpp new file mode 100644 index 00000000..ec3b6855 --- /dev/null +++ b/self_training/2020-07-19-astar/D.cpp @@ -0,0 +1,71 @@ +#include +using namespace std; +const int maxn = 509, maxv = 9, maxd = 4; +int n, dis[maxn][maxn]; +queue > que; +inline bool check(int x, int y) { + return x >= 1 && x <= n && y >= 1 && y <= n; +} +inline void append(int x, int y, int d) { + if(!check(x, y) || dis[x][y] != -1) + return; + dis[x][y] = d; + que.push(make_pair(x, y)); +} +int main() { + static int low[maxv]; + for(int i = 1; i < maxv; ++i) + low[i] = i * i << 3; + int T; + scanf("%d", &T); + while(T--) { + int sx, sy; + static int a[maxn][maxn]; + scanf("%d%d%d", &n, &sx, &sy); + for(int i = 1; i <= n; ++i) + for(int j = 1; j <= n; ++j) + scanf("%d", a[i] + j); + + while(!que.empty()) + que.pop(); + for(int i = 1; i <= n; ++i) + memset(dis[i] + 1, -1, n * sizeof(int)); + append(sx, sy, 0); + // for(int dx = -2; dx <= 2; ++dx) + // for(int dy = -2; dy <= 2; ++dy) + // if(abs(dx) + abs(dy) <= 2) + // append(sx + dx, sy + dy, 1); + + int ans = INT_MAX; + while(!que.empty()) { + pair cur = que.front(); + int x = cur.first, y = cur.second; + que.pop(); + + int tmp = dis[x][y], ctr[maxd + 1] = {}; + for(int dx = -3; dx <= 3; ++dx) + for(int dy = -3; dy <= 3; ++dy) { + int d = abs(dx) + abs(dy); + if(d > 3 || !check(x + dx, y + dy)) + continue; + ++ctr[a[x + dx][y + dy]]; + if(d <= 2) + append(x + dx, y + dy, tmp + 1); + } + int prd = a[x][y], sum = 0; + --ctr[prd]; + for(int i = 1, j = maxd - 1; i < maxv; ++tmp) { + for(sum += prd; i < maxv && sum >= low[i]; ++i) { + for( ; j > 0 && !ctr[j]; --j); + if(j > 0) { + --ctr[j]; + prd += j; + } + } + } + ans = min(ans, tmp); + } + printf("%d\n", ans); + } + return 0; +} \ No newline at end of file diff --git a/self_training/2020-07-19-astar/E.cpp b/self_training/2020-07-19-astar/E.cpp new file mode 100644 index 00000000..d0b501f6 --- /dev/null +++ b/self_training/2020-07-19-astar/E.cpp @@ -0,0 +1,20 @@ +// p(disjoint) = (1/a[i-1] - 1/a[i]) / (2/a[i-1]) +// p(disjoint) * (a[i]/2) = (a[i] - a[i-1]) / 4 +// ans = V - E = sum(a[i]/2) - sum((1-p(disjoint))*(a[i]/2)) = (a[0] + a[n-1]) / 4 +#include +using namespace std; +typedef long long LL; +const int mod = (int)1e9 + 7, inv2 = (mod + 1) >> 1; +int main() { + int T; + scanf("%d", &T); + while(T--) { + int n, fir, las; + scanf("%d%d", &n, &fir); + las = fir; + for(int i = 1; i < n; ++i) + scanf("%d", &las); + int ans = (fir + las) / 2 * (LL)inv2 % mod; + printf("%d\n", ans); + } +} \ No newline at end of file diff --git a/self_training/2020-07-19-astar/F.cpp b/self_training/2020-07-19-astar/F.cpp new file mode 100644 index 00000000..92d3eeca --- /dev/null +++ b/self_training/2020-07-19-astar/F.cpp @@ -0,0 +1,102 @@ +#include +using namespace std; +typedef long long LL; +inline LL sum1(LL n) { // sum(i) + return n * (n + 1) / 2; +} +inline LL sum2(LL n) { // sum(i * i) + return n * (n + 1) * (n + n + 1) / 6; +} +inline LL sum3(LL n) { // sum(i * (i + 1)) + return n * (n + 1) * (n + 2) / 3; +} +LL getCnt(int u, int v) { + LL ret = sum1(u) * 4 + 1; + int u1 = u - (v + 1); + if(u1 >= 0) { + LL tmp = sum1(u1) * 4 + (u1 + 1LL) * 2; + ret -= tmp * 2; + int u2 = u1 - (v + 1); + if(u2 >= 0) { + ret += sum1(u2 + 1) * 4; + } + } + return ret; +} +LL getSum(int u, int v) { + LL ret = sum2(u) * 4; + int u1 = u - (v + 1); + if(u1 >= 0) { + LL tmp = sum1(u1) * 4 + (u1 + 1LL) * 2; + tmp = tmp * (v + 1) + sum2(u1) * 4 + sum1(u1) * 2; + ret -= tmp * 2; + int u2 = u1 - (v + 1); + if(u2 >= 0) { + LL tmp = sum1(u2 + 1) * 4; + tmp = tmp * (v + 1) * 2 + sum3(u2) * 4; + ret += tmp; + } + } + return ret; +} +const int maxd = 5, maxv = (int)2e9; +int sz[maxd]; +LL getCnt(LL upp) { + // printf("cnt %lld:", upp); + LL ret = 0; + for(int i = 1; i < maxd; ++i) { + int prd = maxd - i; + int u = (int)min(upp / prd, (LL)maxv); + // LL las = ret; + ret += getCnt(u, sz[i]); + if(i > 1) + ret -= getCnt(u, sz[i - 1]); + // printf(" %lld", ret - las); + } + // puts(""); + return ret; +} +LL getSum(LL upp) { + // printf("sum %lld:", upp); + LL ret = 0; + for(int i = 1; i < maxd; ++i) { + int prd = maxd - i; + int u = (int)min(upp / prd, (LL)maxv); + // LL las = ret; + ret += getSum(u, sz[i]) * prd; + if(i > 1) + ret -= getSum(u, sz[i - 1]) * prd; + // printf(" %lld", ret - las); + } + // puts(""); + return ret; +} +int main() { + int T; + scanf("%d", &T); + while(T--) { + sz[0] = -1; + for(int i = 1; i < maxd; ++i) + scanf("%d", sz + i); + LL cnt; + scanf("%lld", &cnt); + if(cnt <= 1) { + puts("0"); + continue; + } + LL L = 0, R = sz[maxd - 1] * 8LL; + while(L < R) { + LL M = (L + R) >> 1; + if(getCnt(M) < cnt) { + L = M + 1; + } else { + R = M; + } + } + // assert(L > 0); + LL rem = cnt - getCnt(L - 1); + LL ans = getSum(L - 1) + rem * L; + printf("%lld\n", ans); + } + return 0; +} \ No newline at end of file diff --git a/self_training/2020-07-19-astar/G.cpp b/self_training/2020-07-19-astar/G.cpp new file mode 100644 index 00000000..157fc5c7 --- /dev/null +++ b/self_training/2020-07-19-astar/G.cpp @@ -0,0 +1,54 @@ +#include +using namespace std; +const int maxn = (int)1e3 + 1, maxd = 6, maxs = 1 << maxd | 1; + +int main() { + int T; + scanf("%d", &T); + while(T--) { + int n, m, d; + static int x[maxd + 1], y[maxd + 1], upp[maxs]; + scanf("%d%d%d", &n, &m, &d); + int all = 1 << d; + memset(upp, 0, all * sizeof(int)); + for(int i = 0; i < d; ++i) { + int adt; + scanf("%d%d%d", x + i, y + i, &adt); + for(int j = 0; j < all; ++j) + if((j >> i) & 1) + upp[j] += adt; + } + if(upp[all - 1] < n * m) { + puts("-1"); + continue; + } + int L = 0, R = n + m; + while(L < R) { + int M = (L + R) >> 1; + bool chk = 1; + static int low[maxs]; + memset(low, 0, all * sizeof(int)); + for(int i = 1; i <= n; ++i) + for(int j = 1; j <= m; ++j) { + int msk = 0; + for(int k = 0; k < d; ++k) + if(abs(i - x[k]) + abs(j - y[k]) <= M) + msk |= 1 << k; + ++low[msk]; + } + for(int i = 1; i < all; i <<= 1) + for(int j = 0; j < all; ++j) + if(i & j) + low[j] += low[i ^ j]; + for(int i = 0; chk && i < all; ++i) + chk &= low[i] <= upp[i]; + if(chk) { + R = M; + } else { + L = M + 1; + } + } + printf("%d\n", L); + } + return 0; +} \ No newline at end of file diff --git a/self_training/2020-07-19-astar/H.cpp b/self_training/2020-07-19-astar/H.cpp new file mode 100644 index 00000000..e1e27e60 --- /dev/null +++ b/self_training/2020-07-19-astar/H.cpp @@ -0,0 +1,48 @@ +#include +using namespace std; +typedef long long LL; +const int maxn = (int)1e6 + 1, mod = (int)1e9 + 7, inv2 = (mod + 1) >> 1; +int f[maxn]; +int s(LL n) { + int res = 0; + for(LL i = 1, j; i <= n; i = j + 1) { + j = n / (n / i); + res = (res + n / i % mod * ((j - i + 1) % mod) % mod * ((i + j) % mod)) % mod; + } + res = (LL)res * inv2 % mod; + return res; +} +int main() { + f[1] = 1; + for(int i = 1; i < maxn; ++i) { + for(int j = i + i; j < maxn; j += i) + f[j] -= f[i]; + f[i] *= i; + } + int T; + scanf("%d", &T); + while(T--) { + LL n; + scanf("%lld", &n); + int ans = 0, lim = (int)ceil(sqrtl(n)); + for( ; (LL)lim * lim <= n; ++lim); + for( ; (LL)lim * lim > n; --lim); + LL las = 0; + int val = 0; + for(int i = 1; i <= lim; ++i) { + if(!f[i]) + continue; + LL cur = n / i / i; + if(las != cur) { + las = cur; + val = s(cur); + } + if(f[i]) + ans = (ans + (LL)f[i] * val) % mod; + } + if(ans < 0) + ans += mod; + printf("%d\n", ans); + } + return 0; +} \ No newline at end of file diff --git a/self_training/2020-08-16-fhc/A1.cpp b/self_training/2020-08-16-fhc/A1.cpp new file mode 100644 index 00000000..e1a909ff --- /dev/null +++ b/self_training/2020-08-16-fhc/A1.cpp @@ -0,0 +1,63 @@ +#include +using namespace std; +typedef long long LL; +const int maxn = (int)1e6 + 1, mod = (int)1e9 + 7; +int n, p[maxn], w, h[maxn]; +int solve() { + int ans = 1, tot = 0; + LL sum = 0; + static pair seq[maxn << 1 | 1]; + seq[tot++] = {0, 0}; + for(int i = 1, j, k; i <= n; ++i) { + for(j = tot - 1; j >= 0 && seq[j].first > p[i]; --j); + if(seq[j].first < p[i]) { + for(int k = tot - 1; k > j; --k) + seq[k + 1] = seq[k]; + seq[j + 1] = {p[i], seq[j].second}; + ++tot; + ++j; + } + assert(j > 0); + seq[tot++] = {p[i] + w, 0}; + for(k = j; j < tot; ++j) { + if(seq[j].first < p[i] + w && seq[j].second < h[i]) { + assert(j + 1 < tot); + if(!seq[j].second) + sum += (seq[j + 1].first - seq[j].first) * 2; + sum -= abs(seq[k - 1].second - seq[j].second) + abs(seq[j].second - seq[j + 1].second); + seq[j].second = h[i]; + sum += abs(seq[k - 1].second - seq[j].second) + abs(seq[j].second - seq[j + 1].second); + } + if(seq[k - 1].second != seq[j].second) + seq[k++] = seq[j]; + } + tot = k; + ans = ans * (sum % mod) % mod; + // printf("#%d: %lld\n", i, sum); + } + return ans; +} +int main() { + int T; + scanf("%d", &T); + for(int Case = 1; Case <= T; ++Case) { + int K, A, B, C, D; + scanf("%d%d%d", &n, &K, &w); + for(int i = 1; i <= K; ++i) { + scanf("%d", p + i); + assert(i == 1 || p[i] > p[i - 1]); + } + scanf("%d%d%d%d", &A, &B, &C, &D); + for(int i = K + 1; i <= n; ++i) { + p[i] = ((LL)A * p[i - 2] + (LL)B * p[i - 1] + C) % D + 1; + assert(p[i] > p[i - 1]); + } + for(int i = 1; i <= K; ++i) + scanf("%d", h + i); + scanf("%d%d%d%d", &A, &B, &C, &D); + for(int i = K + 1; i <= n; ++i) + h[i] = ((LL)A * h[i - 2] + (LL)B * h[i - 1] + C) % D + 1; + printf("Case #%d: %d\n", Case, solve()); + } + return 0; +} diff --git a/self_training/2020-08-16-fhc/A2.cpp b/self_training/2020-08-16-fhc/A2.cpp new file mode 100644 index 00000000..8b64cbdc --- /dev/null +++ b/self_training/2020-08-16-fhc/A2.cpp @@ -0,0 +1,71 @@ +#include +using namespace std; +typedef long long LL; +const int maxn = (int)1e6 + 1, maxv = (int)5e8 + 1, mod = (int)1e9 + 7; +int n, p[maxn], w[maxn], h[maxn]; +int solve() { + int ans = 1, tot = 0; + LL sum = 0; + static int pos[maxn << 1 | 1], val[maxn << 1 | 1], dsu[maxn << 1 | 1]; + pos[tot++] = 0; + for(int i = 1; i <= n; ++i) { + pos[tot++] = p[i]; + pos[tot++] = p[i] + w[i]; + } + sort(pos, pos + tot); + tot = unique(pos, pos + tot) - pos; + for(int i = 0; i < tot; ++i) { + val[i] = 0; + dsu[i] = i; + } + auto dsu_find = [&](int x) { + int rt = x; + for( ; dsu[rt] != rt; rt = dsu[rt]); + for(int tp; dsu[x] != rt; tp = dsu[x], dsu[x] = rt, x = tp); + return rt; + }; + for(int i = 1; i <= n; ++i) { + int L = lower_bound(pos, pos + tot, p[i]) - pos; + int R = lower_bound(pos, pos + tot, p[i] + w[i]) - pos; + assert(L > 0); + for(int j = dsu_find(L); j < R; j = dsu_find(j)) { + sum += (pos[j + 1] - pos[j]) * 2; + sum -= abs(val[j - 1] - val[j]) + abs(val[j] - val[j + 1]); + val[j] = h[i]; + sum += abs(val[j - 1] - val[j]) + abs(val[j] - val[j + 1]); + dsu[j] = j + 1; + } + ans = ans * (sum % mod) % mod; + // printf("#%d: %lld\n", i, sum); + } + return ans; +} +int main() { + int T; + scanf("%d", &T); + for(int Case = 1; Case <= T; ++Case) { + int K, A, B, C, D; + scanf("%d%d", &n, &K); + for(int i = 1; i <= K; ++i) + scanf("%d", p + i); + scanf("%d%d%d%d", &A, &B, &C, &D); + for(int i = K + 1; i <= n; ++i) + p[i] = ((LL)A * p[i - 2] + (LL)B * p[i - 1] + C) % D + 1; + for(int i = 1; i <= K; ++i) + scanf("%d", w + i); + scanf("%d%d%d%d", &A, &B, &C, &D); + for(int i = K + 1; i <= n; ++i) + w[i] = ((LL)A * w[i - 2] + (LL)B * w[i - 1] + C) % D + 1; + for(int i = 1; i <= K; ++i) { + scanf("%d", h + i); + assert(i == 1 || h[i] <= h[i - 1]); + } + scanf("%d%d%d%d", &A, &B, &C, &D); + for(int i = K + 1; i <= n; ++i) { + h[i] = ((LL)A * h[i - 2] + (LL)B * h[i - 1] + C) % D + 1; + assert(h[i] <= h[i - 1]); + } + printf("Case #%d: %d\n", Case, solve()); + } + return 0; +} diff --git a/self_training/2020-08-16-fhc/A3.cpp b/self_training/2020-08-16-fhc/A3.cpp new file mode 100644 index 00000000..643384c0 --- /dev/null +++ b/self_training/2020-08-16-fhc/A3.cpp @@ -0,0 +1,71 @@ +#include +using namespace std; +typedef long long LL; +const int maxn = (int)1e6 + 1, maxv = (int)5e8 + 1, mod = (int)1e9 + 7; +int n, p[maxn], w[maxn], h[maxn]; +int solve() { + static int ctr; ctr = 0; + int ans = 1, tot = 0; + LL sum = 0; + map seg; + seg[0] = seg[maxv << 1] = 0; + auto makePoint = [&](int pos) { ++ctr; + auto it = --seg.upper_bound(pos); + if((it -> first) < pos) + seg.insert({pos, it -> second}); + }; + for(int i = 1; i <= n; ++i) { + makePoint(p[i]); + makePoint(p[i] + w[i]); + int pre = (--seg.lower_bound(p[i])) -> second; + for(auto it = seg.lower_bound(p[i]); it != seg.end() && (it -> first) <= p[i] + w[i]; ) { ++ctr; + auto jt = it++; + if((jt -> first) < p[i] + w[i] && (jt -> second) < h[i]) { + if(!(jt -> second)) + sum += ((it -> first) - (jt -> first)) * 2; + sum -= abs(pre - (jt -> second)) + abs((jt -> second) - (it -> second)); + jt -> second = h[i]; + sum += abs(pre - (jt -> second)) + abs((jt -> second) - (it -> second)); + } + if(pre == (jt -> second)) { + int pos = jt -> first; + it = seg.erase(jt); + } else { + pre = jt -> second; + } + } + ans = ans * (sum % mod) % mod; + // printf("#%d: %lld\n", i, sum); + } + fprintf(stderr, "ctr: %d\t", ctr); + return ans; +} +int main() { + int T; + scanf("%d", &T); + for(int Case = 1; Case <= T; ++Case) { + int K, A, B, C, D; + scanf("%d%d", &n, &K); + for(int i = 1; i <= K; ++i) + scanf("%d", p + i); + scanf("%d%d%d%d", &A, &B, &C, &D); + for(int i = K + 1; i <= n; ++i) + p[i] = ((LL)A * p[i - 2] + (LL)B * p[i - 1] + C) % D + 1; + for(int i = 1; i <= K; ++i) + scanf("%d", w + i); + scanf("%d%d%d%d", &A, &B, &C, &D); + for(int i = K + 1; i <= n; ++i) + w[i] = ((LL)A * w[i - 2] + (LL)B * w[i - 1] + C) % D + 1; + for(int i = 1; i <= K; ++i) { + scanf("%d", h + i); + assert(i == 1 || h[i] >= h[i - 1]); + } + scanf("%d%d%d%d", &A, &B, &C, &D); + for(int i = K + 1; i <= n; ++i) { + h[i] = ((LL)A * h[i - 2] + (LL)B * h[i - 1] + C) % D + 1; + assert(h[i] >= h[i - 1]); + } + printf("Case #%d: %d\n", Case, solve()); + } + return 0; +} diff --git a/self_training/2020-08-16-fhc/B.cpp b/self_training/2020-08-16-fhc/B.cpp new file mode 100644 index 00000000..cbbafb3b --- /dev/null +++ b/self_training/2020-08-16-fhc/B.cpp @@ -0,0 +1,56 @@ +#include +using namespace std; +typedef long long LL; +const int maxn = (int)1e6 + 1, maxv = (int)5e8 + 1; +int n, m, a[maxn], b[maxn]; +bool solve(int lim) { + for(int i = 1, j = 1; i <= n; ++i) { + int pL = min(a[i], b[j]); + if(a[i] - pL > lim) + continue; + for( ; j <= m && b[j] < a[i]; ++j); + for( ; j <= m; ++j) { + int pR = b[j]; + if(pR - pL + min(a[i] - pL, pR - a[i]) > lim) + break; + } + if(j > m) + return 1; + } + return 0; +} +int solve() { + sort(a + 1, a + n + 1); + sort(b + 1, b + m + 1); + int L = 0, R = maxv << 1; + while(L < R) { + int M = (L + R) >> 1; + if(solve(M)) { + R = M; + } else { + L = M + 1; + } + } + return L; +} +int main() { + int T; + scanf("%d", &T); + for(int Case = 1; Case <= T; ++Case) { + int K, S, A, B, C, D; + scanf("%d%d%d%d", &n, &m, &K, &S); + assert(!S); + for(int i = 1; i <= K; ++i) + scanf("%d", a + i); + scanf("%d%d%d%d", &A, &B, &C, &D); + for(int i = K + 1; i <= n; ++i) + a[i] = ((LL)A * a[i - 2] + (LL)B * a[i - 1] + C) % D + 1; + for(int i = 1; i <= K; ++i) + scanf("%d", b + i); + scanf("%d%d%d%d", &A, &B, &C, &D); + for(int i = K + 1; i <= m; ++i) + b[i] = ((LL)A * b[i - 2] + (LL)B * b[i - 1] + C) % D + 1; + printf("Case #%d: %d\n", Case, solve()); + } + return 0; +} \ No newline at end of file diff --git a/self_training/2020-08-16-fhc/C.cpp b/self_training/2020-08-16-fhc/C.cpp new file mode 100644 index 00000000..9c13d123 --- /dev/null +++ b/self_training/2020-08-16-fhc/C.cpp @@ -0,0 +1,195 @@ +#include +using namespace std; +typedef long long LL; +const int maxn = (int)1e6 + 1; +int n, p[maxn], sz[maxn], up[maxn], cnt[maxn]; +int m, in[maxn], out[maxn], seq[maxn]; +char typ[maxn + 2]; +vector e[maxn]; +struct Info { + LL sum; + pair f[2]; + pair > get() const { + return make_pair(sum, f[0].second > 0 ? f[0] : make_pair(0, 0)); + } + void check() const { + assert(f[0].first >= 0 && f[1].first >= 0); + assert(f[0].second >= 0 && f[1].second >= 0); + if(!f[0].second) + assert(!f[0].first); + if(f[1].second > 0) { + assert(f[0].second > 0); + assert(f[0].first > f[1].first); + } else { + assert(!f[1].first); + } + } + void add(Info const &t) { + sum += t.sum; + for(int i = 0, j = 0; i < 2; ++i) { + if(!t.f[i].second) + break; + for( ; j < 2 && f[j].second > 0 && f[j].first > t.f[i].first; ++j); + if(j == 2) + break; + if(t.f[i].first == f[j].first) { + f[j].second += t.f[i].second; + } else { + for(int k = 1; k > j; --k) + f[k] = f[k - 1]; + f[j] = t.f[i]; + } + } + check(); + } + void add(int x) { + sum += x * (x - 1LL) / 2; + if(!f[0].second || x > f[0].first) { + f[1] = f[0]; + f[0] = {x, 1}; + } else if(x == f[0].first) { + ++f[0].second; + } else if(!f[1].second || x > f[1].first) { + f[1] = {x, 1}; + } else if(x == f[1].first) { + ++f[1].second; + } + check(); + } + void sub(int x) { + sum -= x * (x - 1LL) / 2; + if(f[0].second > 0 && x == f[0].first) { + --f[0].second; + if(!f[0].second) { + f[0] = f[1]; + f[1] = {}; + } + } else if(f[1].second > 0 && x == f[1].first) { + --f[1].second; + if(!f[1].second) + f[1] = {}; + } + check(); + } +} seg[maxn << 1 | 1]; +inline int seg_idx(int L, int R) { + return (L + R) | (L < R); +} +void seg_build(int L, int R) { + int rt = seg_idx(L, R); + if(L < R) { + int M = (L + R) >> 1; + seg_build(L, M); + seg_build(M + 1, R); + seg[rt] = seg[seg_idx(L, M)]; + seg[rt].add(seg[seg_idx(M + 1, R)]); + } else { + seg[rt] = {}; + seg[rt].add(cnt[seq[L]]); + } +} +Info seg_que(int L, int R, int l, int r) { + if(l <= L && R <= r) + return seg[seg_idx(L, R)]; + int M = (L + R) >> 1; + Info ret = {}; + if(l <= M) + ret.add(seg_que(L, M, l, r)); + if(r > M) + ret.add(seg_que(M + 1, R, l, r)); + return ret; +} +void dfs(int u) { + in[u] = m + 1; + if(typ[u] == '*' && typ[p[u]] == '*') { + up[u] = up[p[u]]; + } else { + up[u] = u; + seq[++m] = u; + } + sz[u] = 1; + cnt[u] = typ[u] == '*'; + for(int v : e[u]) { + dfs(v); + sz[u] += sz[v]; + if(up[v] != v) + cnt[u] += cnt[v]; + } + out[u] = m; +} +pair solve() { + typ[0] = '\0'; + p[1] = 0; + for(int i = 2; i <= n; ++i) + e[p[i]].push_back(i); + m = 0; + dfs(1); + seg_build(1, m); + LL ans = 0, ways = 0; + for(int i = 2; i <= n; ++i) { + Info u = {}, v = {}; + if(in[i] <= out[i]) { + u = seg_que(1, m, in[i], out[i]); + if(in[i] > 1) + v.add(seg_que(1, m, 1, in[i] - 1)); + if(out[i] < m) + v.add(seg_que(1, m, out[i] + 1, m)); + } else { + v = seg_que(1, m, 1, m); + } + if(typ[i] == '*' && typ[p[i]] == '*') { + u.add(cnt[i]); + v.sub(cnt[up[i]]); + v.add(cnt[up[i]] - cnt[i]); + } + pair tp; + int ux, uc, vx, vc; + LL us, vs; + tie(us, tp) = u.get(); + tie(ux, uc) = tp; + tie(vs, tp) = v.get(); + tie(vx, vc) = tp; + assert(uc > 0 && vc > 0); + LL tmp = us + vs, adt; + if(ux > 0 && vx > 0) { + tmp -= ux * (ux - 1LL) / 2; + tmp -= vx * (vx - 1LL) / 2; + int all = ux + vx; + tmp += all * (all - 1LL) / 2; + adt = (LL)uc * vc * ux * vx; + } else { + adt = (LL)sz[i] * (n - sz[i]); + } + if(ans < tmp) { + ans = tmp; + ways = 0; + } + if(ans == tmp) + ways += adt; + } + for(int i = 1; i <= n; ++i) + vector().swap(e[i]); + return {ans, ways}; +} +int main() { + int size = 512 << 20; // 512MB + char *ptr = (char*)malloc(size) + size; + __asm__("movq %0, %%rsp\n" :: "r"(ptr)); + + int T; + scanf("%d", &T); + for(int Case = 1; Case <= T; ++Case) { + int K, A, B, C; + scanf("%d%d%s", &n, &K, typ + 1); + for(int i = 2; i <= K + 1; ++i) + scanf("%d", p + i); + scanf("%d%d%d", &A, &B, &C); + for(int i = K + 2; i <= n; ++i) + p[i] = ((LL)A * p[i - 2] + (LL)B * p[i - 1] + C) % (i - 1) + 1; + pair ans = solve(); + printf("Case #%d: %lld %lld\n", Case, ans.first, ans.second); + } + + exit(0); + // return 0; +}